开发者

I need to convert a mysql Query to CakePHP conditions

I am having trouble understanding how to turn this into a native cakephp query using the find function. any help would be greatly appreciated.

SELECT * FROM `seo_web_directories` 
LEFT JOIN (SELECT * FROM `seo_site_statuses` 
  WHER开发者_如何学JAVAE site_id = $queryData) as t1 
ON `seo_web_directories`.id = t1.web_directory_id

The variable is a dynamic value that is set when the function is called


Instead of using a suboptimal ORM , you should find a way to using this query directly with models. Try looking at Model::query() method [source].

ORMs ( especially ones implementing ActiveRecord ) should not be used for joins. They end up doing loops for each new table you add to the JOIN.


$this->WebDirectory->find('all', array('conditions' => array('verified' => 0),'joins' => array(
  array(
    'table' => 'seo_site_statuses',
    'type' => 'left',
    'alias' => 't1',
    'conditions' => array('WebDirectory.id' => ' t1.web_directory_id', 't1.site_id =' . $id)))));

alright here is the converted query i was able to figure out... maybe one more example will help someone else figure out how to do this...

However I ended up using a direct query because the above find was requiring too much memory and time for the recursion as mentioned by the above answer.

function paginate($id, $recursive = null, $extra = array(), $limit = 50, $page = 1){
  $page = ($page * $limit) - $limit;

  if(!empty($extra)){
    $OrderBy = "ORDER BY ";
    foreach ($extra as $key => $value) {

      $OrderBy .= $key . " " . $value;
    }
  }else{
    $OrderBy = "ORDER BY t2.page_rank DESC";
  }

  return $this->query('SELECT * FROM `seo_web_directories` as t2 
  LEFT JOIN (SELECT * FROM `seo_site_statuses` WHERE site_id = ' . $id['Site.id'] . ') as t1
  ON `t2`.id = t1.web_directory_id WHERE t2.`verified` = 0 ' . $OrderBy . ' LIMIT ' . $page . ', ' . $limit);

 // return $this->WebDirectory->find('all', compact('conditions', 'joins', 'limit', 'page', 'recursive'));
}

function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
return $this->WebDirectory->find('count', array('conditions' => array('verified' => 0)));
}


This will help also. Enter your Mysql and convert. This will automatically convert query to cakephp format and you can understand that easily.

http://dogmatic69.com/sql-to-cakephp-find-converter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜