Issue
I am trying to query the database using CDbCriteria
in Yii 1
. What I am doing is:
$criteria->addInCondition('t.id', $dealIdArr);
$criteria->order = "FIELD(t.id, $dealIdArr)";
And this is the $dealIdArr
and it's not empty:
Array ( [0] => 3 [1] => 2 )
But when run the script, it returns the error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'order clause'.
I'm unable to understand what is wrong with this? Any help?
Solution
You're currently adding an array to both conditions. I'm not too familiar with Yii, but I'm quite sure that it expects a string for the order - and not an array.
You can implode()
it to convert it to a string.
$dealIdStr = implode(", ", $dealIdArr);
$criteria->addInCondition('t.id', $dealIdArr); // You can use array here
$criteria->order = "FIELD(t.id, $dealIdStr)"; // This expects a string
As your array currently only contains integers (3 and 2), you don't need to quote that - but if the array for any reason contains strings, you need to have them quoted in MySQL by doing
$dealIdStr = implode("', '", $dealIdArr);
$criteria->order = "FIELD(t.id, '$dealIdStr')";
Notice the added single-quotes.
When you try to use an array as a string, PHP will simply print "Array", which is what you're seeing now. And since its not quoted, MySQL think its a column, and not a string.
Answered By - Qirel
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.