Jan Beeck Jan Beeck - 6 months ago 179
PHP Question

How to call the SQL LIKE function in Yii2?

Receiving NULL as value when I called this SQL statement (the record exists in the MySQL DB). How can use the LIKE function in Yii2? Thanks.

$command = $connection->createCommand("SELECT user_id
FROM User
WHERE name LIKE '%:_username%'");

$command->bindParam(':_username',$this->username);
$this->id_user = $command->queryScalar();
$command->execute();

Answer

In Yii2 you can use also a activeQuery Notation this way

  $query = new Query;
  $query->select('userid')
      ->from('user')
      ->where(['like', 'user', $this->username );

  $command = $query->createCommand();

  $this->id_user = $command->queryScalar();