A Purchase Modal which has $hasMany relation. I can't hide $hasMany from Modal because it is needed.
Problem : When sometime i am using Custom Query that time i don't want $hasMany relation data.
How to prevent get $hasMany relation data in CakePHP model
What to do in Custom Query to prevent get $hasMany relation data.
Purchase Modal
class Purchase extends AppModel {
/*
Modal Related Code
*/
public $hasMany = array(
'PurchaseProduct' => array(
'className' => 'PurchaseProduct',
'order' => 'PurchaseProduct.created DESC'
)
);
);
Purchase Controller : using a Custom Query here to get only Dates
$PurchaseDate= $this->Purchase->find('all',
array('order' => array('Purchase.date_generated ASC'),
'fields' => array('DISTINCT Purchase.date_generated','Purchase.id')));
Coming Result
Array
(
[0] => Array
(
[Purchase] => Array
(
[date_generated] => 1970-01-01
[id] => 9
)
[PurchaseProduct] => Array
(
[0] => Array
(
[id] => 15
[Purchase_id] => 9
[product_id] => 5
[product_name] => ABC
[created] => 2014-08-15 18:51:49
)
)
)
)
Required Result
Array
(
[0] => Array
(
[Purchase] => Array
(
[date_generated] => 1970-01-01
[id] => 9
)
)
)
One way to prevent hasMany relation data with custom query is to use
$this->recursive = -1;
you need to define this in model before your query code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.