I have two models: Event and Date. A Event hasMany Date. I want to order the events by the most recent dates. For example:
[0] => Array
(
[Event] => Array
(
[id] => 1
[name] => Event 1
)
[Date] => Array
(
[0] => Array
(
[id] => 1
[date] => 2015-02-05
[start_time] => 06:00:00
[end_time] => 17:00:00
[evento_id] => 1
)
[1] => Array
(
[id] => 2
[date] => 2015-02-06
[start_time] => 06:00:00
[end_time] => 17:00:00
[evento_id] => 1
)
)
)
[1] => Array
(
[Event] => Array
(
[id] => 2
[name] => Event 2
)
[Date] => Array
(
[0] => Array
(
[id] => 3
[date] => 2015-02-11
[start_time] => 06:00:00
[end_time] => 17:00:00
[event_id] => 2
)
)
)
[2] => Array
(
[Event] => Array
(
[id] => 3
[name] => Event 3
)
[Date] => Array
(
[0] => Array
(
[id] => 5
[date] => 2015-02-02
[start_time] => 06:00:00
[end_time] => 17:00:00
[event_id] => 3
)
[1] => Array
(
[id] => 6
[date] => 2015-02-03
[start_time] => 06:00:00
[end_time] => 17:00:00
[event_id] => 3
)
)
)
So, the order I want to retrieve the events are: Event 3, Event 1, Event 2.
This is how I'm calling the find method:
$this->Event->find('all', array('contain' => array('Date' => array('conditions' => array('Date.date >=' => date('Y-m-d'))))));
It's possible to do that with Cakephp find method? Thanks.
Something like this should do it
$this->Event->find('all', array(
'contain' => array(
'Date' => array(
'conditions' => array('
Date.date >=' => date('Y-m-d')
)
)
),
order => array(
'COUNT(Date.id)' => 'DESC'
)
));
Related
In cakephp 2 I want to group table after descending order
always returned the first record of table
but I want returned last record by ordering
my data, code and result in here:
My data in table:
Code in controller:
$books = $this->Book->find('all', array(
'order' => array('age' => 'DESC'),
'group' => 'name'
));
print_r($books);
Result after the code above:
Array
(
[0] => Array
(
[Book] => Array
(
[id] => 1
[name] => ramin
[family] => zardoshti
[age] => 32
[city] => semnan
)
)
[1] => Array
(
[Book] => Array
(
[id] => 2
[name] => esmaeil
[family] => seydi
[age] => 30
[city] => semnan
)
)
)
But I want this result:
Array
(
[0] => Array
(
[Book] => Array
(
[id] => 1
[name] => ramin
[family] => zardoshti
[age] => 42
[city] => tehran
)
)
[1] => Array
(
[Book] => Array
(
[id] => 2
[name] => esmaeil
[family] => seydi
[age] => 30
[city] => semnan
)
)
)
Add a virtual field on the fly to your Book model
$this->Book->virtualFields = array(
'max_age' => sprintf('max(%s.age)', $this->Book->alias)
);
$books = $this->Book->find('all', array(
'order' => array('age' => 'DESC'),
'group' => 'name'
));
print_r($books);
The virtual field will help to get the rows with the maximum age value.
Your SQL query will look like :
SELECT
`Book`.`id`, `Book`.`name`, `Book`.`family`, `Book`.`age`,
`Book`.`city`, (max(`Book`.`age`)) AS `Book__max_age`
FROM
`test`.`books` AS `Book`
WHERE
1 = 1
GROUP BY
name
ORDER BY
`age` DESC
I am working on Reporting of the Postal service Project and i wanted to display data with the help of pagination . i know we can overwrite pagination function in model . i have tried it but didn't work for me.
I have BillReport Model where i have created function. Below is my custom query which is showing Multiple Bills against one payment means Payment hasMany Bills. I can paginate Payment but i wanted to paginate Bill wise.
I have mapped the bills and payments in Billpaymentmap table and from that table i am getting array of below query.
I want to paginate related model which is associated with Payment . Payment Hasmany Bill.
$reversal = $this->query("SELECT Pay.*,C.id,C.name,U.id,U.username,Bill.account_no,Bill.id, Pay.*,PMap.data,PMap.paid_amount FROM dbo_payments As Pay INNER JOIN dbo_reverse_payments As RPay ON RPay.payment_id = Pay.id INNER JOIN dbo_payment_bill_maps As PMap ON PMap.payment_id = RPay.payment_id INNER JOIN dbo_users U ON U.id = Pay.cashier_id INNER JOIN dbo_clients As C ON C.id = U.client_id INNER JOIN dbo_bills As Bill ON Bill.id = PMap.bill_id WHERE Pay.reversal = 1 and $conditions ");
Array
(
[0] => Array
(
[Pay] => Array
(
[id] => 10
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 58130
[pay_date] => 2015-02-18 12:42:28
[amount_paid] => 3715
[total_paid] => 3715
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 1910009
[id] => 1037079
)
[RPay] => Array
(
[id] => 1
[payment_id] => 10
[user_id] => 5
[reason] => LIC
[reversal_fee] =>
[created] => 2015-02-18 13:06:28
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 3215.00
)
)
[1] => Array
(
[Pay] => Array
(
[id] => 10
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 58130
[pay_date] => 2015-02-18 12:42:28
[amount_paid] => 3715
[total_paid] => 3715
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 546865468
[id] => 1037162
)
[RPay] => Array
(
[id] => 1
[payment_id] => 10
[user_id] => 5
[reason] => LIC
[reversal_fee] =>
[created] => 2015-02-18 13:06:28
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:38:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 500.00
)
)
[2] => Array
(
[Pay] => Array
(
[id] => 7
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 82053
[pay_date] => 2015-02-18 12:29:21
[amount_paid] => 8380
[total_paid] => 8380
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 1910001
[id] => 1037071
)
[RPay] => Array
(
[id] => 2
[payment_id] => 7
[user_id] => 5
[reason] => LF
[reversal_fee] =>
[created] => 2015-02-18 13:06:43
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:6:"Tushar";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 4665.00
)
)
[3] => Array
(
[Pay] => Array
(
[id] => 7
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 82053
[pay_date] => 2015-02-18 12:29:21
[amount_paid] => 8380
[total_paid] => 8380
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 1910009
[id] => 1037079
)
[RPay] => Array
(
[id] => 2
[payment_id] => 7
[user_id] => 5
[reason] => LF
[reversal_fee] =>
[created] => 2015-02-18 13:06:43
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 3215.00
)
)
[4] => Array
(
[Pay] => Array
(
[id] => 7
[location_id] => 133
[cashier_id] => 5
[client_id] => 2
[dealer_id] => 31
[receipt_no] => 82053
[pay_date] => 2015-02-18 12:29:21
[amount_paid] => 8380
[total_paid] => 8380
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
)
[C] => Array
(
[id] => 31
[name] => GAMPOST
)
[U] => Array
(
[id] => 5
[username] => admin
)
[Bill] => Array
(
[account_no] => 546865468
[id] => 1037162
)
[RPay] => Array
(
[id] => 2
[payment_id] => 7
[user_id] => 5
[reason] => LF
[reversal_fee] =>
[created] => 2015-02-18 13:06:43
)
[PMap] => Array
(
[data] => a:3:{s:4:"name";s:38:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
[paid_amount] => 500.00
)
)
)
I have googled but i am not getting my answer. Can anyone please help me on this.
Thanks for reply.
I went through below link . i have given limit => 3 . it is giving me data upto 3 for Payments Not for Bills which are multiple against one Payment.
I need to paginate 'PaymentBillMap' data .
public $paginate = array('limit'=>'3',
'order'=>'Payment.pay_date',
'fields'=>array('Payment.id','Payment.location_id','Payment.dealer_id'));
public function trans_list_service(){
$params = $this->__commonopr();
$this->loadModel('Payment');
$this->loadModel('PaymentBillMap');
$conditions = array('Payment.location_id ' => 133);
// apply the custom conditions to the pagination
$payments = $this->paginate('Payment',$conditions);
$this->set('payments', $payments);
pr($payments);
}
Custom Query Pagination Cakephp
Array
(
[0] => Array
(
[Payment] => Array
(
[id] => 1
[location_id] => 133
[dealer_id] => 31
)
[PaymentBillMap] => Array
(
[0] => Array
(
[payment_id] => 1
[bill_id] => 1037153
[paid_amount] => 4656.00
[payment_type] => BILL
[trans_no] => 24862
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:7:"Dholpur";s:11:"paymentType";s:4:"BILL";}
)
[1] => Array
(
[payment_id] => 1
[bill_id] => 1037154
[paid_amount] => 4665.00
[payment_type] => BILL
[trans_no] => 48280
[data] => a:3:{s:4:"name";s:5:"AMITA";s:7:"address";s:10:"Ganganagar";s:11:"paymentType";s:4:"BILL";}
)
[2] => Array
(
[payment_id] => 1
[bill_id] => 1037155
[paid_amount] => 5787.00
[payment_type] => BILL
[trans_no] => 45996
[data] => a:3:{s:4:"name";s:9:"duryodhan";s:7:"address";s:5:"Jalor";s:11:"paymentType";s:4:"BILL";}
)
[3] => Array
(
[payment_id] => 1
[bill_id] => 1037156
[paid_amount] => 6688.00
[payment_type] => BILL
[trans_no] => 64019
[data] => a:3:{s:4:"name";s:6:"Dharaj";s:7:"address";s:8:"Jhalawar";s:11:"paymentType";s:4:"BILL";}
)
[4] => Array
(
[payment_id] => 1
[bill_id] => 1037157
[paid_amount] => 8795.00
[payment_type] => BILL
[trans_no] => 21308
[data] => a:3:{s:4:"name";s:4:"Amit";s:7:"address";s:9:"Jhunjhunu";s:11:"paymentType";s:4:"BILL";}
)
[5] => Array
(
[payment_id] => 1
[bill_id] => 1037158
[paid_amount] => 5654.00
[payment_type] => BILL
[trans_no] => 10980
[data] => a:3:{s:4:"name";s:5:"Gaesh";s:7:"address";s:4:"Pali";s:11:"paymentType";s:4:"BILL";}
)
[6] => Array
(
[payment_id] => 1
[bill_id] => 1037159
[paid_amount] => 3456.00
[payment_type] => BILL
[trans_no] => 79988
[data] => a:3:{s:4:"name";s:4:"Jitu";s:7:"address";s:14:"Sawai Madhopur";s:11:"paymentType";s:4:"BILL";}
)
[7] => Array
(
[payment_id] => 1
[bill_id] => 1037160
[paid_amount] => 4664.00
[payment_type] => BILL
[trans_no] => 26314
[data] => a:3:{s:4:"name";s:6:"Rajesh";s:7:"address";s:6:"Sirohi";s:11:"paymentType";s:4:"BILL";}
)
[8] => Array
(
[payment_id] => 1
[bill_id] => 1037161
[paid_amount] => 3565.00
[payment_type] => BILL
[trans_no] => 40148
[data] => a:3:{s:4:"name";s:4:"Gani";s:7:"address";s:5:"Baran";s:11:"paymentType";s:4:"BILL";}
)
[9] => Array
(
[payment_id] => 1
[bill_id] => 1037148
[paid_amount] => 3442.00
[payment_type] => BILL
[trans_no] => 48235
[data] => a:3:{s:4:"name";s:4:"Jitu";s:7:"address";s:5:"Ajmer";s:11:"paymentType";s:4:"BILL";}
)
[10] => Array
(
[payment_id] => 1
[bill_id] => 1037149
[paid_amount] => 4335.00
[payment_type] => BILL
[trans_no] => 71496
[data] => a:3:{s:4:"name";s:4:"amer";s:7:"address";s:5:"Alwar";s:11:"paymentType";s:4:"BILL";}
)
[11] => Array
(
[payment_id] => 1
[bill_id] => 1037150
[paid_amount] => 3535.00
[payment_type] => BILL
[trans_no] => 93135
[data] => a:3:{s:4:"name";s:6:"Dilip[";s:7:"address";s:9:"Bharatpur";s:11:"paymentType";s:4:"BILL";}
)
[12] => Array
(
[payment_id] => 1
[bill_id] => 1037151
[paid_amount] => 6453.00
[payment_type] => BILL
[trans_no] => 46714
[data] => a:3:{s:4:"name";s:7:"Suvarna";s:7:"address";s:11:"Chittorgarh";s:11:"paymentType";s:4:"BILL";}
)
[13] => Array
(
[payment_id] => 1
[bill_id] => 1037152
[paid_amount] => 5323.00
[payment_type] => BILL
[trans_no] => 43007
[data] => a:3:{s:4:"name";s:5:"Datta";s:7:"address";s:5:"Churu";s:11:"paymentType";s:4:"BILL";}
)
[14] => Array
(
[payment_id] => 1
[bill_id] => 1037136
[paid_amount] => 6456.00
[payment_type] => BILL
[trans_no] => 27701
[data] => a:3:{s:4:"name";s:6:"Faizan";s:7:"address";s:9:"Dungarpur";s:11:"paymentType";s:4:"BILL";}
)
[15] => Array
(
[payment_id] => 1
[bill_id] => 1037137
[paid_amount] => 7657.00
[payment_type] => BILL
[trans_no] => 29162
[data] => a:3:{s:4:"name";s:6:"Shamli";s:7:"address";s:7:"Jodhpur";s:11:"paymentType";s:4:"BILL";}
)
[16] => Array
(
[payment_id] => 1
[bill_id] => 1037138
[paid_amount] => 8688.00
[payment_type] => BILL
[trans_no] => 77181
[data] => a:3:{s:4:"name";s:6:"Tushat";s:7:"address";s:4:"Kota";s:11:"paymentType";s:4:"BILL";}
)
[17] => Array
(
[payment_id] => 1
[bill_id] => 1037139
[paid_amount] => 4657.00
[payment_type] => BILL
[trans_no] => 75049
[data] => a:3:{s:4:"name";s:5:"Danav";s:7:"address";s:7:"Udaipur";s:11:"paymentType";s:4:"BILL";}
)
[18] => Array
(
[payment_id] => 1
[bill_id] => 1037140
[paid_amount] => 6886.00
[payment_type] => BILL
[trans_no] => 48850
[data] => a:3:{s:4:"name";s:6:"Kailas";s:7:"address";s:7:"Bikaner";s:11:"paymentType";s:4:"BILL";}
)
[19] => Array
(
[payment_id] => 1
[bill_id] => 1037141
[paid_amount] => 7965.00
[payment_type] => BILL
[trans_no] => 11684
[data] => a:3:{s:4:"name";s:7:"Paravat";s:7:"address";s:5:"Dausa";s:11:"paymentType";s:4:"BILL";}
)
)
)
[1] => Array
(
[Payment] => Array
(
[id] => 2
[location_id] => 133
[dealer_id] => 31
)
[PaymentBillMap] => Array
(
[0] => Array
(
[payment_id] => 2
[bill_id] => 1037162
[paid_amount] => 500.00
[payment_type] => BILL
[trans_no] => 62066
[data] => a:3:{s:4:"name";s:37:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
)
[1] => Array
(
[payment_id] => 2
[bill_id] => 1037079
[paid_amount] => 3215.00
[payment_type] => BILL
[trans_no] => 41810
[data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
)
)
)
[2] => Array
(
[Payment] => Array
(
[id] => 3
[location_id] => 133
[dealer_id] => 31
)
[PaymentBillMap] => Array
(
[0] => Array
(
[payment_id] => 3
[bill_id] => 1037162
[paid_amount] => 500.00
[payment_type] => BILL
[trans_no] => 74439
[data] => a:3:{s:4:"name";s:38:"Amit Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
)
)
)
)
I am running following query in CakePHP:
$options = array('conditions' => array('Patient.' . $this->Patient->primaryKey => $id),array('recursive'=>2,'group'=>array('group_id')));
$this->set('patient', $this->Patient->PatientTest->find('all', $options));
But my group is not working as needed. It gives me output as follows:
Array
(
[0] => Array
(
[PatientTest] => Array
(
[patient_id] => 2
[test_id] => 1
[date] => 2014-03-27 17:44:17
[result] => 55
[group_id] => 1
)
[Patient] => Array
(
[id] => 2
[name] => Test Patient
[age] => 44
[gender] => 0
[email] => emailid#gmail.com
[contact] => 789654123
[date] => 0000-00-00 00:00:00
)
[Test] => Array
(
[id] => 1
[name] => Hb
[group_id] => 1
[normal] => 12 - 16 gma%
)
[Group] => Array
(
[id] => 1
[name] => Haematology
)
)
[1] => Array
(
[PatientTest] => Array
(
[patient_id] => 2
[test_id] => 2
[date] => 2014-03-27 17:44:17
[result] => 55
[group_id] => 1
)
[Patient] => Array
(
[id] => 2
[name] => Test Patient
[age] => 44
[gender] => 0
[email] => emailid#gmail.com
[contact] => 789654123
[date] => 0000-00-00 00:00:00
)
[Test] => Array
(
[id] => 2
[name] => PCV
[group_id] => 1
[normal] => 35-50%
)
[Group] => Array
(
[id] => 1
[name] => Haematology
)
)
)
I need them group by either group_id or by Group.id.
I am unable to find why it is not grouping.
not
$options = array
(
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
array(
'recursive'=>2,
'group'=>array('group_id')
)
);
but
$options = array
(
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
'recursive'=>2,
'group'=>array('group_id')
);
recursive, conditions and group are at the same level
Simple and sweet code :
$groupBy = 'Patient.group_id';
$options = array (
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
'group'=>array($groupBy)
);
I have a weird error when using contain to limit my data to only what I need. Here is my contain
$this->Movie->contain(array(
'Review' => array(
'fields' => array('id', 'title', 'spoilers', 'body', 'created'),
'User' => array(
'fields' => array('id', 'username'),
'Profile' => array('fields' => array('id', 'image_thumb_small'))
),
'order' => 'Review.created DESC',
'limit' => 2,
),
'Movieupdates' => array(
'User' => array('Profile' => array('fields' => 'image_thumb_small')),
'order' => 'Movieupdates.created DESC',
'limit' => 2
),
'Actor' => array(
'fields' => array('name', 'slug', 'image_thumb'),
),
'Genre' => array(
'fields' => array('name', 'id'),
)
));
The problem is that Cakephp throws me two errors:
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]
Warning (2): array_unique() expects parameter 1 to be array, null given [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]
I think the problem has to do with the following line (in contain: Review -> User):
'fields' => array('id', 'username'),
If I remove that everything is fine, but then again I dont want all the data from User, especially not the passwords so I have to limit that.
What is wrong with my Contain? The error suggest that I should be using an array somewhere, but I have no idea where.
Edit added query outcome
Array
(
[Movie] => Array
(
[id] => 27
[user_id] =>
[title] => The Amazing Spider-Man
[slug] => The_Amazing_Spider_Man_2012
[year] => 2012
[summary] => Peter Parker finds a clue that might help him understand why his parents disappeared when he was young.
[poster] => /files/movies/53cb8be658a2e04a21909e288cdcb8.jpg
[poster_thumb] => /files/movies/53cb8be658a2e04a21909e288cdcb8_resized_101x150.jpg
[backdrop] => /files/movies/backdrop/amazing_spider_man.jpg
[rating] => 7.4285714285715
[like_count] => 5
[review_count] => 7
[see_count] => 7
[rating_count] => 14
)
[Movieupdates] => Array
(
[0] => Array
(
[id] => 20
[user_id] => 4
[movie_id] => 27
[type] => 4
[info] => 26
[created] => 2012-12-08 11:06:18
[User] => Array
(
[id] => 4
[username] => Ceriksen
[email] => skdji#gmail.com
[password] => ---
[validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
[group_id] => 3
[created] => 2012-11-27 11:23:57
[modified] => 2012-11-27 11:23:57
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
[1] => Array
(
[id] => 19
[user_id] => 4
[movie_id] => 27
[type] => 3
[info] => 10
[created] => 2012-12-08 11:06:08
[User] => Array
(
[id] => 4
[username] => Ceriksen
[email] => skdji#gmail.com
[password] => ---
[validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
[group_id] => 3
[created] => 2012-11-27 11:23:57
[modified] => 2012-11-27 11:23:57
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
)
[Genre] => Array
(
[0] => Array
(
[name] => Action
[id] => 11
[MoviesGenre] => Array
(
[id] => 56
[movie_id] => 27
[genre_id] => 11
)
)
[1] => Array
(
[name] => Adventure
[id] => 12
[MoviesGenre] => Array
(
[id] => 57
[movie_id] => 27
[genre_id] => 12
)
)
[2] => Array
(
[name] => Fantasy
[id] => 18
[MoviesGenre] => Array
(
[id] => 58
[movie_id] => 27
[genre_id] => 18
)
)
)
[Actor] => Array
(
[0] => Array
(
[name] => Emma
[slug] => Emma_Stone
[image_thumb] => /files/actors/d59efc7614151acb7ea5d08da47112_resized_30x30.jpg
[MoviesActor] => Array
(
[id] => 1
[movie_id] => 27
[actor_id] => 8
)
)
[1] => Array
(
[name] => Andrew
[slug] => Andrew_Garfield
[image_thumb] => /files/actors/0a9354d741328f4cf3e2954641e4eb_resized_25x30.jpg
[MoviesActor] => Array
(
[id] => 2
[movie_id] => 27
[actor_id] => 9
)
)
[2] => Array
(
[name] => Martin
[slug] => Martin_Sheen
[image_thumb] => /files/actors/2383fc87054b2e8b4249fc7e3ffba5_resized_30x27.jpg
[MoviesActor] => Array
(
[id] => 3
[movie_id] => 27
[actor_id] => 10
)
)
)
[Review] => Array
(
[0] => Array
(
[id] => 26
[title] => W00t
[spoilers] => 0
[created] => 2012-12-08 11:06:18
[user_id] => 4
[MoviesReview] => Array
(
[id] => 25
[movie_id] => 27
[review_id] => 26
)
[User] => Array
(
[id] => 4
[username] => Ceriksen
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
[1] => Array
(
[id] => 25
[title] => Testing the update
[spoilers] => 1
[created] => 2012-12-08 11:04:44
[user_id] => 4
[MoviesReview] => Array
(
[id] => 24
[movie_id] => 27
[review_id] => 25
)
[User] => Array
(
[id] => 4
[username] => Ceriksen
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
)
)
Solved it, I had an error in my contain. Stupid one.
'User' => array('Profile' => array('fields' => 'image_thumb_small')),
Should have been:
'User' => array('Profile' => array('fields' => array('image_thumb_small'))),
I have the following models:
Kin, which you can think of as an animal--a pet.
Species hasAndBelongsToMany Kin
GrowthStage hasAndBelongsToMany Species
Kin hasAndBelongsToMany GrowthStage
and KinGrowthStage, which defines the relationship between a kin and its growth stages (it contains its related images and such for the kin at that growth stage).
So in my controller I'm trying to get the following array to come out the other end. In this scenario the Species => GrowthStage => KinGrowthStages are related to the Kin id:
Array
(
[Kin] => Array
(
[id] => 4
[name] => Hobbes
[generation] => 1
[current_growth_stage_id] => 4
[species_id] => 1
)
[Species] => Array
(
[id] => 1
[name] => Tiger
[GrowthStage] => Array
(
[0] => Array
(
[id] => 5
[name] => cub
[order] => 1
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 4
[image] => /img/hobbes/cub.png
)
)
)
[1] => Array
(
[id] => 2
[name] => juvenile
[order] => 2
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 4
[image] => /img/hobbes/juvenile.png
)
)
)
[2] => Array
(
[id] => 9
[name] => adult
[order] => 3
[KinGrowthStage] => Array
(
// Nothing here because Hobbes hasn't reached this growth stage yet
)
)
)
)
)
What I'm getting is this. The Species => GrowthStage => KinGrowthStages is joining on the GrowthStage id rather than the Kin id:
Array
(
[Kin] => Array
(
[id] => 4
[name] => Hobbes
[generation] => 1
[current_growth_stage_id] => 4
[species_id] => 1
)
[Species] => Array
(
[id] => 1
[name] => Tiger
[GrowthStage] => Array
(
[0] => Array
(
[id] => 5
[name] => cub
[order] => 1
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 1
[image] => /img/tony/cub.png
)
)
)
[1] => Array
(
[id] => 2
[name] => juvenile
[order] => 2
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 2
[kin_id] => 1
[image] => /img/tony/juvenile.png
)
)
)
[2] => Array
(
[id] => 9
[name] => adult
[order] => 3
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 3
[kin_id] => 1
[image] => /img/tony/adult.png
)
)
)
)
)
)
I understand why this happening, I just want to know if there's any way to modify the relationship 'on the fly' and join it on the kin id. I've tried adding the following conditions array to my contain array:
$this->Kin->contain(
array(
'Species' => array(
'GrowthStage' => array(
'KinGrowthStage' => array(
'conditions' => array('Kin.id = KinGrowthStage.kin_id'),
),
'order' => 'GrowthStage.order ASC'
)
)
)
);
But I get an error about 'unknown table Kin'.
Use the Linkable Behavior https://github.com/Terr/linkable
$this->Kin->find('all', array(
'link' => array(
'Species' => array(
'GrowthStage' => array(
'KinGrowthStage' => array(
'conditions' => 'Kin.id = KinGrowthStage.kin_id',
)
)
)
),
'order' => 'GrowthStage.order ASC'
));