We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

SQL query

Hi, i want to get some data from the database, my sql code is :

SELECT products. FROM products WHERE id IN (SELECT product_lang.product_id FROM product_lang WHERE name LIKE "%{$val}%") UNION SELECT products. FROM products WHERE product_id = {$val}

is there anyway that I can do this on phalcon returning a models/Products instance ?



7.1k

Use Raw SQL for this for example in a function which return your query result :

class Product extends \Phalcon\Mvc\Model
{
        public static function get($params)
        {
                $sql = 'SELECT 
                            products.* 
                        FROM products 
                        WHERE id IN (SELECT productlang.productid FROM product_lang WHERE name LIKE "%' . $params['name'] . '%") 
                        UNION SELECT products.* FROM products WHERE product_id = ' . $params['id'];

                $product = new Product();  // Product Model

                return new \Phalcon\Mvc\Model\Resultset\Simple(
                    null,
                    $product,
                    $product->getReadConnection()->query($sql)
                );
        }

to use it :

$result = Product::get();