3

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();
1
  • Have you looked into using the Query Builder instead of creating the command yourself? It might be more flexible for this type of call. It also has the benefit of handling where cases in different ways. Commented May 10, 2016 at 23:56

6 Answers 6

1

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

It works modifying the following line: ->where(['like', 'user','%'.$this->username.'%', false]);
1

Just wrap "%value%" in bindValue

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

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

Correct Way to bind value with %% is below

 $command->bindParam(':_username',"%".$this->username."%");

just like as PDO we use

Comments

0

I think the issue here is with the binding.

Try, using this instead.

$command->bindParam('_username',$this->username);

Also not so sure what's the acceptable syntax for the variables, so if it's still not working try removing the _ and use username on binding... and :username on the query.

Comments

0

Binding needs to be done on the entire data not just part of it.

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

$user = '%' . $this->username . '%';

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

2 Comments

I have the following error after that statement: PHP Fatal Error – yii\base\ErrorException Cannot pass parameter 2 by reference
You might need to turn it into a string first: eg. $user = '%' . $this->username . '%'; $command->bindParam(':username',$user);
0

I'm using bindValue. You can use like this.. I have a query in my model, and calls the functions on my Controller.

Inside Model:

$query = "SELECT u.id as idUser
FROM vagas_x_candidato vxc
INNER JOIN usuario u ON(u.id = vxc.id_candidato)
WHERE u.nome LIKE :nameUser";

$connection = \Yii::$app->db;
$model = $connection->createCommand($query);
$model->bindValue(":nameUser", '%' . $filter->nameU . '%');
return $model->queryAll();

Inside Controller:

$vxc = new VagasXCandidatoSearch();
$vxc = $vxc->candidatosPesquisaAvancada(); //calls my method on model.

Comments

0

I'm not sure, but try to get the first % before the single quote and the last % after the last single quote.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.