1

I have array like this

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

And using $conditions array in this method.

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

I want convert the $conditions array to normal sql query. I want use

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

instead of

$this->Post->find('first', array('conditions' => $conditions));
2
  • Just because? Or do you have a goal you're not describing? Commented Aug 25, 2012 at 6:19
  • I have 10 cakephp query. in certain condition ,i want combine this query and execute the combined query in mysqli Commented Aug 25, 2012 at 6:22

2 Answers 2

1
 $null=null;
 echo  $this->getDataSource()->generateAssociationQuery($this, NULL, NULL, NULL, NULL, $query_array, false,$null);
Sign up to request clarification or add additional context in comments.

Comments

0

To do what you want you could do two things:

1) Combine your $conditions arrays and let CakePHP build your new query so you can simply use $this->Model->find() again.

2) Use this. It's an expansion for the mysql datasource that adds the option to do $this->Model->find('sql', array('conditions' => $conditions)) which will return the SQL-query. This option might cause trouble, because for some find calls (especially when you're fetching associated models) CakePHP uses multiple queryies to fetch the associated models (especially in case of hasMany-associations).

If at all possible, option 1 will probably cause the least trouble. Another problem with going with 2 is that if you're trying to combine two queries with conflicting conditions (like 'name = Hansel' in query 1 and 'name = Gretel' in query 2) you will just find nothing unless you plan on writing extra code to parse the resulting queries and look for conflicts.. Going with 1 will probably be a lot simpler and will probably avoid lots of problems.

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.