ChaseHardin
ChaseHardin

Reputation: 2269

Query in Yii - Adding Condition

I want to add a condition to my query, which will remove the product_id that is equal to 999. Currently, I get the following error: Property "CDbCriteria.where" is not defined. Any suggestions?

Query in my PHP Controller

$products = Product::model()->findAll(array(
                    "condition" => $condition,
                    "where" => 'product_id != 999',
                    'order' => 'product_name ASC'
                ));

Updated Code This still doesn't remove product_id 999, but it doesn't throw an error.

$products = Product::model()->findAll(array(
                        "condition" => $condition + 'where product_id != 999',
                        'order' => 'product_name ASC'
                    ));

Upvotes: 0

Views: 206

Answers (2)

ChaseHardin
ChaseHardin

Reputation: 2269

My original code wouldn't work because I was passing an array of CDbCriteria. It also doesn't allow the where statement. I ended up concatenating my condition with $condition using the . and changed my where to an AND statement

This is my final output:

$products = Product::model()->findAll(array(
            "condition" => $condition . ' AND product_id != 999',
            'order' => 'product_name ASC'
      ));

Upvotes: 1

Rafay Zia Mir
Rafay Zia Mir

Reputation: 2116

Because what you are doing here is passing an array of CDbCriteria. Now documentation shows that CDbCriteria does not have property named "where". You can use where in your $condition by using concatenation.

DELETE FROM table_name
WHERE product_id = 999

Only CDbCommand has property where

Upvotes: 0

Related Questions