We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problem with PHQL

I have the following Phql query:

$arrRs = $this->modelsManager->executeQuery("SELECT * FROM Phrase p inner join  User u  on p.user = u.id  where (p.phrase like '% :search: %' or    u.codeLink2me = ':search:')", array('search' => $searchInput));

When I run it, it gives me an $arrRs->valid() as a false. But when i ran SQL version of the Phql query, its return the expected results

$sql = "SELECT * FROM phrases p inner join users u on p.phrase_id = u.user_id WHERE (p.str_phrase like '%$searchInput%' or u.str_cod_link = '$searchInput')";
$user = new User();
$links = new Phalcon\Mvc\Model\Resultset\Simple(null, $user, $user->getReadConnection()->query($sql, ''));

Am I missing something?



98.9k
Accepted
answer

You can't enclose a placeholder in simple/double quotes because it will handle the placeholder as a string, you have to do:

$arrRs = $this->modelsManager->executeQuery("SELECT * FROM Phrase p inner join User u on p.user = u.id where (p.phrase like :search: or u.codeLink2me = :search:)", array('search' => '%' . $searchInput . '%'));


4.0k

Thank you very much.