J
J
jehord2015-01-10 19:01:34
Yii
jehord, 2015-01-10 19:01:34

How to write insert via QueryBuilder >_

I am trying to make a banal insert according to the documentation
Here is my piece of code

$query = \Yii::$app->db->queryBuilder;
$query->insert('poll_vote', [
      'choice_id' => $choiceID,
      'poll_id' => $pollID,
      'user_id' => empty($userID) ? NULL : $userID,
      'ip' => $ip,
      'timestamp' => time()
    ], $params);

I can't figure out what should be in the $params variable.
Please tell me how to correctly compose a banal insert...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Zelenin, 2015-01-11
@zelenin

the content received from the user must be bound using prepared statements

$query = new Query();
    $query->createCommand()->insert(self::tableName(),[
      'choice_id' => ':choiceID'
...], [
':choiceID' => $choiceID
])->execute();

J
jehord, 2015-01-10
@jehord

Basically, this is what I did.

$query = new Query();
    $query->createCommand()->insert(self::tableName(),[
      'choice_id' => $choiceID,
      'poll_id' => $pollID,
      'user_id' => empty($userID) ? NULL : $userID,
      'ip_address' => $ip,
      'timestamp' => time()])->execute();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question