Skip to content

Latest commit

 

History

History
108 lines (90 loc) · 2.16 KB

File metadata and controls

108 lines (90 loc) · 2.16 KB

Query

setup

  • create Query Folder at /app/Model
  • create AppQuery.php at /app/Model/Query

Or just copy Sugar/code_snippets/Model/Query 到 /app/Model

AppQuery.php

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{
}

call Query in Repository

/*
 /app/Model/PostRepository.php
 */
 public function getComment01($id){
        $obj = $this->getQueryObject();

         $obj->q('Post.join_comments')
            ->q('Post.getLastPost');
     ...

Query

<?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',
        )
    ));
    
      
    }
}

Query default Methods

Query::fields()

  $obj->q('Comment.fields','*');
  //get array('Comment.*')  
  $obj->q('Post.fields','id,body,title');
  //get array('Post.id,Post.body,Post.title')

Query::where()

  $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"));

Query::limit()

  $obj->q('Comment.limit',3);

Query::offset()

  $obj->q('Comment.offset',3);

Query::group()

  $obj->q('Comment.group','title');

Query::order()

  $obj->q('Comment.order','title');

Query::joins()

  $obj->q('Comment.joins', array(
        'table' => 'channels',
        'alias' => 'Channel',
        'type' => 'LEFT',
        'conditions' => array(
            'Channel.id = Item.channel_id',
        )
    ));

Query Attributes