SQL query

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

SELECT products.* FROM products WHERE id IN (SELECT productlang.productid 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 ?



3.2k

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();