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

problem with createbuilder

Hi everyone,

i did smt like below :

$select = $this->modelsManager->createBuilder()
    ->columns("st.id,st.title,st.text")
    ->addFrom("ShopTexts","st")
    ->where("st.active = 1");

    if(isset($options['category_id']))
    {
        $select->andWhere('st.category_id = '.$options['category_id']);
    }
    if(isset($options['brand_id']))
    {
        $select->andWhere('st.brand_id = '.$options['brand_id']);
    }
    if(isset($options['discount']))
    {
        $select->andWhere('st.discount = '.$options['discount']);
    }
    if(isset($options['new']))
    {
        $select->andWhere('st.new = '.$options['new']);
    }
    if(isset($options['price']))
    {
        $select->andWhere('st.price = '.$options['price']);
    }
    if(isset($options['color']))
    {
        $select->andWhere('st.color = '.$options['color']);
    }
    if(isset($options['shop_id']))
    {
        $select->andWhere('st.shop_id = '.$options['shop_id']);
    }

    if(isset($options['parent_id']) && $options['parent_id'] >= 0)
    {
        $select->andWhere('parent_id = '.$options['parent_id']);
    }

    $select->orderBy('st.list_order DESC');

$result = $select->getQuery()->execute();

When i want to run it, returns that :

Syntax error, unexpected token ), near to ') AND (parent_id = 0) ORDER BY st.list_order DESC', when parsing: SELECT st.id,st.title,st.text FROM [ShopTexts] AS [st] WHERE (((((((st.active = 1) AND (st.brand_id = 0)) AND (st.discount = 0)) AND (st.new = 0)) AND (st.price = 0)) AND (st.color = 0)) AND (st.shop_id = )) AND (parent_id = 0) ORDER BY st.list_order DESC (255)

how can i solve it ?

isset does not check if the string is not empty, see the sql comment below

also, HOLY SQL INJECTION! Use placeholders please :)

SELECT st.id,st.title,st.text 
FROM [ShopTexts] AS [st] 
WHERE (((((((st.active = 1) 
AND (st.brand_id = 0)) 
AND (st.discount = 0)) 
AND (st.new = 0)) 
AND (st.price = 0)) 
AND (st.color = 0)) 
AND (st.shop_id = )) /* <--- here */
AND (parent_id = 0) 
ORDER BY st.list_order DESC (255)