Rudy sibaja
Rudy sibaja

Reputation: 9

how to create a inverse like cakephp

I need create a inverse like query, something like this

SELECT * FROM identities where 'keyword' like concat('%',identities.name,'%')

keyword = something the user type

identities.name = the table column

but I neet to create using a cakephp syntax

$this->Indentity->find('all', array('conditions' => array('here something to help me'));

a normal LIKE using cakephp not working because i neet inverse like

this query not working because is normal LIKE

$this->Indentity->find('all', array('conditions' => array('Indentity.name LIKE' => $keyword));

this query not working to because concat('%',identities.name,'%') uses like string not a function

$this->Indentity->find('all', array('conditions' => array($keyword . ' LIKE' => "concat('%',`identities`.`name`,'%')"));

Upvotes: 1

Views: 119

Answers (1)

mark
mark

Reputation: 21743

a) You can always use virtual fields for such specific querys

b) In this case it is probably easiest to just write it in a single row:

$conditions = array($keyword . " LIKE CONCAT('%', `identities`.`name`, '%')";
$result = $this->Indentity->find('all', array('conditions' => $conditions));

Note the missing => key value operator in this case to signalize the ORM to not escape the statement.

NOTE: Bear in mind that you will have to manually secure (escape, sanitize, ...) the input in a way that people cannot injection vulerabilities into the query, e.g. using the datasources value() method.

Upvotes: 0

Related Questions