i need to create an select with alias, like this:
SELECT*, (adresses.v1 + adresses.v2) AS total FROM adresses ORDER BY distance DESC
How can i insert this query in this code below?
$objects = $this->Property->find('all', array(
'recursive' => 2,
'reformat' => true,
'conditions' => $conditions,
'order' => $order,
'joins' => $joins,
));
Try:
$objects = $this->Property->find('all', array(
'recursive' => 2,
'reformat' => true,
'conditions' => $conditions,
'order' => $order,
'joins' => $joins,
'fields' => array('*', '(adresses.v1 + adresses.v2) AS total'),
));
Related
At the moment i query my db with this query below and it works great , it joins with another table.
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'order' => array('Tapplicant.AppDate' => 'DESC'),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'limit' => 15
)
);
I have setup a Pagenator :
public $paginate = array (
'order' => array('Tapplicant.AppID' => 'desc'),
'limit' => 15,
);
What i need to know is how do i :
Add Paginator
Using on the current date CURDATE()
I jsut dont know where to add it in.
In your controller:-
$this->paginate = array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'conditions' => array(
'Tapplicant.AppDate' => date('Y-m-d')
),
'order' => array('Tapplicant.AppDate' => 'DESC'),
'limit' => 15
);
$this->set('tapplicant', $this->paginate());
You can do this right in the find:
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'limit' => 15,
'order' => array('Tapplicant.AppID' => 'desc'),
)
);
And leave paginate like this:
public $paginate = array('limit' => 15);
If you only want the posts from the current day, just add this to the find:
'conditions' => array('Tapplicant.created' => date('Y-m-d')),
I want to get last_deadline and the count of instalments of all instalments but obviously this query will show me 1 order and the last_deadline.
$orders = $this->find('all', array(
'fields' => array(
'Order.order_id', 'Order.summary', 'Order.fee',
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline'
),
'conditions' => $conditions,
'joins' => array(
array(
'table' => $this->getTableName('default', 'brief_instalments'),
'alias' => 'BriefInstalment',
'type' => 'RIGHT',
'conditions' => array(
'Order.order_id = BriefInstalment.order_id',
'BriefInstalment.deleted' => 0,
),
'order' => 'BriefInstalment.deadline ASC',
)
),
'order' => 'BriefInstalment.deadline ASC'
));
I have tried 'contain' and doesn't work.
'contain' => array(
'BriefInstalment' => array(
'fields' => 'BriefInstalment.id',
'fields' => array(
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline', 'COUNT(`BriefInstalment`.`id`) AS total_instalments'
),
'conditions' => array(
'BriefInstalment.deleted' => 0
)
)
),
By the way I don't want to use loop to get last_intalments and cout brief_instalments. e.g.
// Determine deadlines
foreach ($orders as $i => $order) {
$deadline = $this->BriefInstalment->getLastDeadline($order['Order']['order_id']);
$orders[$i] += array(
...
'last-deadline' => $deadline,
'total-instalments' => count($order['BriefInstalment'])
);
}
The reason is it decrease the speed of loading.
Any help plz
Here is how to create custom query
$conditionsSubQuery['"User2"."status"'] = 'B';
$db = $this->User->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('"User2"."id"'),
'table' => $db->fullTableName($this->User),
'alias' => 'User2',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->User
);
$subQuery = ' "User"."id" NOT IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$this->User->find('all', compact('conditions'));
Reference:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries
User has many User_Questionaries. I want paginate users that have particular questionnaire. I used following pagination for it.
$paginate = array(
'conditions' => array(
'User.role' => IWOA,
'UserQuestionary.questionary_id' => $id
),
'recursive' => 1,
'limit' => 10,
'order' => array(
'name' => 'asc'
),
'contain' => array('UserQuestionary')
);
But it is not create join query. It is showing Unknown column UserQuestionary.questionary_id' in 'where clause'
What is the issue? How can i do it?
Finally I used join query for do this.
$paginate = array(
'conditions' => array(
'Iwoa.role' => IWOA,
),
'joins' => array(
array(
'alias' => 'UserQuestionary',
'table' => 'user_questionaries',
'type' => 'LEFT',
'conditions' => 'UserQuestionary.user_id = Iwoa.id AND UserQuestionary.questionary_id = ' . $id
)
),
'limit' => 10,
'order' => array(
'name' => 'asc'
),
);
I am trying to get the employees and their posting details with largest posting_from date.
My query is:
$emp = $this->EmployeePersonal ->find(
'all',
array(
'fields' => array('EmployeePersonal.*', 'PermanentDist.name','PresentDist.name','EmployeePosting.*','Designation.name','Department.name','Office.name' ),
'conditions' => $condition,
//'order' => array('Designation.id'),
'group' => 'EmployeePersonal.id',
'order' => 'EmployeePosting.posting_from DESC',
'recursive' => -1,
'joins' => array(
array(
'table' => 'employee_postings',
'alias' => 'EmployeePosting',
'type' => 'LEFT',
'conditions' => array(
'EmployeePosting.employee_personal_id = EmployeePersonal.id',
)
),
)
)
);
But the above query shows the lowest posting_from value. Why isn't the order working in my case?
some commas and will be ready:
$emp = $this->EmployeePersonal ->find(
'all',
array(
'joins' => array(
array(
'table' => 'employee_postings',
'alias' => 'EmployeePosting',
'type' => 'LEFT',
'conditions' => array(
'EmployeePosting.employee_personal_id = EmployeePersonal.id'
)
)
),
'fields' => array('EmployeePersonal.*', 'PermanentDist.name','PresentDist.name','EmployeePosting.*','Designation.name','Department.name','Office.name'),
'conditions' => $condition,
//'order' => array('Designation.id'),
'group' => 'EmployeePersonal.id',
'order' => array(
'EmployeePosting.posting_from' => 'DESC'
),
'recursive' => -1
)
);
How can I group by fields and Order by DESC in cakephp?
$this->set('messages', $this->Message->find('all',array('conditions' => array('Message.receiver_id'=>$this->Session->read('Auth.User.id')),'group' => array('Message.user_id'),'order' => array('max(Message.created) ASC'))));
Records are fetched in cakePhp depend on a model is
$records = $this->Model->find('all', array(
'conditions' => array(conditons),
'fields' => array(fieldlist),
'group' => array('Model.column'),
'order' => array('Model.column', 'Model.column2 DESC')
));
From your code, you can write as
$data = $this->Message->find('all', array(
'conditions' => array( 'Message.receiver_id'=>$this->Session->read('Auth.User.id')),
'group' => array('Message.user_id'),
'order' => array('max(Message.created) ASC')
));
$this->set('messages', $data);
$records = $this->Model->find('all', array(
'conditions' => array(conditons),
'recursive' => -1,
'fields' => array(fieldlist),
'group' => array('Model.column'),
'order' => array('Model.column', 'Model.column2 DESC')
));
If you want to order by latest Message inserted then instead of sort by created use sort by id like this Model.id DESC'