2014-12-23 48 views
1

一个逆我需要创建一个像查询逆,像这样如何创建这样的CakePHP

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

关键字=一些用户类型

identities.name =表列

但我neet创建使用cakephp语法

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

正常使用LIKE cakephp中不工作,因为我NEET逆像

此查询不工作,因为是正常LIKE

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

此查询不工作,因为的concat( '%',identitiesname,“%”)使用了如字符串不是一个函数

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

您应该始终提及您正在使用的确切cakephp版本。我怀疑你正在使用CakePHP2.5。 – mark

回答

0

a)你可以随时使用这些特定的querys

B)的虚拟域在这种情况下,它可能是最简单的只是把它写在单行:

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

注意,在这种情况下,缺少=>键值运营商以突出的ORM不逃逸的声明。

注意:请记住,您将不得不手动确保(转义,消毒,...)输入的方式,使人们无法在查询中注入多种功能,例如使用datasources的value()方法。

+0

我敢肯定,如果没有示例显示如何手动转义,使用此代码段的人中有99%会在其应用程序中引入SQL注入缓冲功能,但人们很懒,因此他们不会阅读文档除非你把他们击倒他们的喉咙:) – ndm

+0

好点。我在这里添加了一个评论。 – mark