- create Query Folder at /app/Model
- create AppQuery.php at /app/Model/Query
Or just copy Sugar/code_snippets/Model/Query 到 /app/Model
the AppQuery class is the parent class to all of your Query. AppQuery itself extends the SuperAppQuery class included in the Sugar/Vendor/
<?php
App::uses('SuperAppQuery', 'Sugar.Vendor');
class AppQuery extends SuperAppQuery{
}/*
/app/Model/PostRepository.php
*/
public function getComment01($id){
$obj = $this->getQueryObject();
$obj->q('Post.join_comments')
->q('Post.getLastPost');
...<?php
App::uses('AppQuery', 'Model/Query');
class PostQuery extends AppQuery {
public function somefunction(QueryObject $obj){ //first param is QueryObject
//Query logic goes here..like below
$obj->where(array('Post.id'=>1));
$obj->limit(1);
$obj->order('Post.id desc');
$obj->fields('Post.id ,Post.title');
$obj->offset(3);
$obj->joins(array(
'table' => 'channels',
'alias' => 'Channel',
'type' => 'LEFT',
'conditions' => array(
'Channel.id = Item.channel_id',
)
));
}
} $obj->q('Comment.fields','*');
//get array('Comment.*')
$obj->q('Post.fields','id,body,title');
//get array('Post.id,Post.body,Post.title') $obj->q('Comment.where',array('Post.id'=>1));
$obj->q('Comment.where',array('Post.id = 1'));
$obj->q('Comment.where',array('Post.id = 1','Post.title"=> "joe")); $obj->q('Comment.limit',3); $obj->q('Comment.offset',3); $obj->q('Comment.group','title'); $obj->q('Comment.order','title'); $obj->q('Comment.joins', array(
'table' => 'channels',
'alias' => 'Channel',
'type' => 'LEFT',
'conditions' => array(
'Channel.id = Item.channel_id',
)
));