I have a code
$command = Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => $status_id)
)
->join('item i', 'q.item_id = i.item_id')
->where('IN', 'queue_id', $ids);
after I call $command->buildQuery() I get an error:
CDbCommand failed to execute the SQL statement: Invalid parameter number: parameter was not defined. The SQL statement executed was: UPDATE queue q SET i.status_id=:i.status_id
The impression is that it does not see the join and where commands.
What the problem?
Your code is valid with the newest Yii version. This MySQL-specific functionality has been added as of 1.1.14: https://github.com/yiisoft/yii/commit/ed49b77ca059c0895be17df5813ee1e83d4c916d.
The where clause should be in the update() function like this
Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => $status_id),array('in', 'queue_id', $ids)
);
And regarding the JOIN part there is a open bug at https://github.com/yiisoft/yii/issues/124 (Im not sure. Correct me if Im wrong). Please let me know if there is a workaround.
You have to bind the parameters:
$command = Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => ':status_id'),
array('in', 'queue_id', $ids),
array(':status_id' => $status_id),
)
->join('item i', 'q.item_id = i.item_id');
Having come across this problem a few times in my projects I have come-up with the following Yii work-around using CDbCriteria which is a little hacky, but gives the security of param count matching.
When applied to your example my code would be (guessing a little bit of your structure):
$ids = array(1,2,3,4,5);
$criteria = new CDbCriteria();
$criteria->addInCondition('i.queue_id',$ids);
$sql = '
UPDATE queue q
JOIN item i
ON q.item_id = i.item_id
SET i.status_id = :status
WHERE '.$criteria->condition;
$command = Yii::app()->db->createCommand($sql);
$command->bindValue('status',$status);
$command->bindValues($criteria->params);
$rows = $command->execute();
Related
I'm using Yii 1.1.15 and am trying to create a query. but when i do i get a no parameters where bound error This is my code below. From the doc it looks like i'm doing everything right.
$user = Yii::app()->db->createCommand()
->select()
->from('ABC')
->where('id=:id0 AND id=:id1 AND id=:id2', array(':id0'=> '07Q00G', ':id1'=>'07Q01A', ':id2'=>'07Q02A'))
->execute();
CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. The SQL statement executed was: SELECT *
FROM `ABC`
WHERE id=:id0 AND id=:id1 AND id=:id2
also when i used ->query() instead of ->execute() it echo's this. and doesnt replace the variables
CDbDataReader Object ( [_statement:CDbDataReader:private] => PDOStatement Object ( [queryString] => SELECT * FROM `ABC` WHERE id=:id0 AND id=:id1 AND id=:id2 ) [_closed:CDbDataReader:private] => [_row:CDbDataReader:private] => [_index:CDbDataReader:private] => -1 [_e:CComponent:private] => [_m:CComponent:private] => )
Any idea what i'm missing here?
Instead of execute() you should do queryAll()
so It should be
$user = Yii::app()->db->createCommand()
->select()
->from('ABC')
->where('id=:id0 AND id=:id1 AND id=:id2', array(':id0'=> '07Q00G', ':id1'=>'07Q01A', ':id2'=>'07Q02A'))
->queryAll();
I was trying to figure how to use Update from Zend\Db\Sql using a Join in Zend Framework 2.
In the documentation they say that the only allowed method for Update are just where() and set(), so I would like to know if there are some alternative ways to get the same results.
You could do something like this (untested):
$db = new DbAdapter(
array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'username' => 'root',
'password' => '',
)
);
$sql = 'UPDATE t1 JOIN t2 ON t1.id = t2.id SET t1.atr = 1';
$sql_result = $db->createStatement($sql )->execute();
if($sql_result->count() > 0){
echo "DONE";
}
I don't believe this is possible with the 'update()' method provided by the Zend Db Adapter.
You can, however, run the query manually using the adapter. Something like:
// $adapter is an instance of Zend_Db_Adapter
$adapter->query(YOUR QUERY HERE);
This seems to be a duplicate of Update with join using Zend-framework
Seems to be no way to do with this with Zend.
I have the following SQL
SELECT entry_subject.id, entry_subject.subject_id
FROM entry_subject
INNER JOIN subject
ON entry_subject.subject_id = subject.id
WHERE subject.id = 71
I have arrived at the following which is an almost carbon copy of a number of examples, however I'm getting 1054 Unknown column 'EntrySubject.subject_id' in 'field list'.
$subjectEntries = new CDbCriteria();
$subjectEntries->alias = 'EntrySubject';
$subjectEntries->select = 'EntrySubject.id, EntrySubject.subject_id';
$subjectEntries->join = 'INNER JOIN Subject ON Subject.id = EntrySubject.subject_id';
$subjectEntries->condition = 'Subject.id=71';
$subjectEntriesModel=Subject::model()->findAll($subjectEntries);
My relationships are as follows
return array(
'entrySubject' => array(self::HAS_MANY, 'EntrySubject', 'subject_id'),
'phase' => array(self::BELONGS_TO, 'Phase', 'phase_id'),
'subjectType' => array(self::BELONGS_TO, 'SubjectType', 'subject_type'),
);
What am I doing wrong?
Many thanks
I assume you are trying to get subject entries for subject #71, then you should simply try the following :
$subject = Subject::model()->findByPk(71);
$subjectEntriesModel = $subject->entrySubject;
EDIT : you should, usually, avoid eager loading (as described in miog and Burhan Çetin answers) for a HAS_MANY relation
From soju's answer above
I assume you are trying to get subject entries for subject #71, then
you should simply try the following :
$subject = Subject::model()->findByPk(71);
$subjectEntriesModel = $subject->entrySubject;
I would suggest an addition to this.
$subject = Subject::model()->with('entrySubject')->findByPk(71);
$subjectEntriesModel = $subject->entrySubject;
->with('entrySubject') was added
This will allow yii to use one query instead if it is possible.
$query = Subject::model()->with('entrySubject')->findbypk($id);
The ORM in FuelPHP has an update example that looks like this:
$entry = Model_Article::find(4);
$entry->title = 'My first edit';
$entry->author = 'Total n00b';
$entry->save();
I'm wondering if there is an ability for me to update w/ something like this:
$values = array(
'this' => $that
);
Model_Article::find(4)->save($values);
The insert ability allows for passing arrays of values:
$props = array('property' => 'something');
$new = Model_Example::forge($props)->save();
But I see no example of doing the same thing w/ the update ability.
EDIT: It appears Model_Example::find(4)->values($array)->save(); is what I'm looking for.
It appears Model_Example::find(4)->values($array)->save(); is what I'm looking for.
What's wrong with this statement? An error is occurring, it is not reading the second line
(($var = array('tab.order' => 'tab.order+1');))
$db->update('tab', $form->getValues(), array('id =?' => $id));
$var = array('tab.order' => 'tab.order+1');
$var2 = array('tab.order >= ' . $form->getValue('order'));
$db->update('tab', $var, $var2);
Your problem is likely happening when Zend_Db does it's escaping of values in $var, and the value becomes
`tab.order+1`
You'll need to do
$var = array('tab.order' => new Zend_Db_Expr('tab.order + 1'));
to get around this.