2012-08-25 36 views
1

我有数组这样如何从cake查询数组中获取正常的SQL查询?

$conditions = array("Post.title" => "This is a post"); 

而且使用$条件,这种方法阵列。

$this->Post->find('first', array('conditions' => $conditions)); 

我想将$ conditions数组转换为正常的sql查询。 我想用

$this->Post->query($converted_query); 

,而不是

$this->Post->find('first', array('conditions' => $conditions)); 
+0

只是因为?或者你有没有你没有描述的目标? – tadman

+0

我有10个cakephp查询。在某些情况下,我想结合这个查询并在mysqli中执行组合查询 – EbinPaulose

回答

0

,做你想做,你可以做两件事情是什么:

1)结合您的$conditions阵列,让CakePHP的建设新的查询,所以你可以简单地再次使用$this->Model->find()

2)使用this。这是对mysql数据源的扩展,它增加了选项$this->Model->find('sql', array('conditions' => $conditions)),它将返回SQL查询。此选项可能可能会造成麻烦,因为对于某些查找调用(特别是在您提取相关模型时),CakePHP使用多个查询来获取关联模型(特别是在hasMany-关联的情况下)。

如果可能的话,选项1可能会造成最小的麻烦。使用2的另一个问题是,如果您试图将两个具有冲突条件的查询结合起来(如查询1中的'name = Hansel'和查询2中的'name = Gretel'),除非您计划编写任何内容额外的代码来解析结果查询并寻找冲突。 与1一起使用可能会简单得多,并且可能会避免很多问题。

1
$null=null; 
echo $this->getDataSource()->generateAssociationQuery($this, NULL, NULL, NULL, NULL, $query_array, false,$null);