Print arrays by fields Separate - arrays

i have arrays like this :
Array
(
[477] => Array
(
[nid] => 477
[changed] => 1510506071
[created] => 1510506071
[title] => mother
[status] => 1
)
[480] => Array
(
[nid] => 480
[changed] => 1510506071
[created] => 1510506071
[title] => hello
[status] => 1
)
[481] => Array
(
[nid] => 481
[changed] => 1510506071
[created] => 1510506071
[title] => name
[status] => 1
)
i need to render arrays by field Separate and use print_r($result[''][nid]) OR print_r($result[]nid) but dont work !
Any help on doing this?

foreach($yourArray as $array){
print_r(array['nid']);
}

Related

How to use pagination with custom query in cakephp

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

group is not working in CakePHP

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

How to display the field when the behaviour is containable?

i have containable behavior in my model I have an array like that
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => First article
[content] => aaa
[created] => 2008-05-18 00:00:00
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[post_id] => 1
[author] => Daniel
[email] => dan#example.com
[website] => http://example.com
[comment] => First comment
[created] => 2008-05-18 00:00:00
)
[1] => Array
(
[id] => 2
[post_id] => 1
[author] => Sam
[email] => sam#example.net
[website] => http://example.net
[comment] => Second comment
[created] => 2008-05-18 00:00:00
)
)
[Tag] => Array
(
[0] => Array
(
[id] => 1
[name] => Awesome
)
[1] => Array
(
[id] => 2
[name] => Baking
)
)
)
How can I display the value of fields in my view? like I want to display the value of
[Post][title]
[Comment][post_id]
[Comment][author]
how can I do this, please help me to do that I m new in cakephp, I don't know how to do that, thanks in advance.
Try this
foreach($posts as $post){
foreach($post['Comment'] as $comment){
/*Display start here*/
$post['Post']['title'];
$comment['post_id'];
$comment['author'];
}
}
Hope this help...

Cakephp 2.0 weird array_merge error when using contain

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'))),

How to get total price from estimate to show up in index.ctp using CakePHP

I have Estimates hasMany EstimateDetails hasMany Items. What I'm trying to do is get a total on my estimates Index view.
The Debug of $estimates on index.ctp:
Array
(
[0] => Array
(
[Estimate] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
[Employee] => Array
(
[id] => 1
[first_name] => Edward
[last_name] => Ramon
[phone] => 2106959672
[hourly_pay] => 0.00
[position_id] => 2
[created] => 2011-07-12 17:56:42
[modified] => 2011-07-12 17:56:42
[fullname] => Edward Ramon
[Position] => Array
(
[id] => 2
[name] => Estimator
)
[Estimate] => Array
(
[0] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
)
[Job] => Array
(
)
)
[EstimateDetail] => Array
(
[0] => Array
(
[id] => 1
[estimate_id] => 1
[qty] => 10
[item_id] => 1
[feet] => 10
[inches] => 2
[adjustment] => -0.20
[Estimate] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
[Item] => Array
(
[id] => 1
[type] => W
[eje] => W
[description] => 44x335
[price unit] => perpound
[weight] => 335
[price] => 0.80
[fulldesc] => W44x335
)
)
[1] => Array
(
[id] => 3
[estimate_id] => 1
[qty] => 3
[item_id] => 1
[feet] => 4
[inches] => 5
[adjustment] => 0.00
[Estimate] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
[Item] => Array
(
[id] => 1
[type] => W
[eje] => W
[description] => 44x335
[price unit] => perpound
[weight] => 335
[price] => 0.80
[fulldesc] => W44x335
)
)
[2] => Array
(
[id] => 4
[estimate_id] => 1
[qty] => 10
[item_id] => 1
[feet] => 10
[inches] => 10
[adjustment] => 0.00
[Estimate] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
[Item] => Array
(
[id] => 1
[type] => W
[eje] => W
[description] => 44x335
[price unit] => perpound
[weight] => 335
[price] => 0.80
[fulldesc] => W44x335
)
)
[3] => Array
(
[id] => 5
[estimate_id] => 1
[qty] => 10
[item_id] => 3
[feet] => 10
[inches] => 10
[adjustment] => 0.00
[Estimate] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
[Item] => Array
(
[id] => 3
[type] => W
[eje] => W
[description] => 44x290
[price unit] => perpound
[weight] => 290
[price] => 0.80
[fulldesc] => W44x290
)
)
[4] => Array
(
[id] => 6
[estimate_id] => 1
[qty] => 10
[item_id] => 6
[feet] => 10
[inches] => 2
[adjustment] => 0.00
[Estimate] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
[Item] => Array
(
[id] => 6
[type] => PL
[eje] => PL
[description] => 4X3
[price unit] => ea
[weight] => 1
[price] => 0.30
[fulldesc] => PL4X3
)
)
)
[Job] => Array
(
[0] => Array
(
[id] => 1
[job_number] => 2353
[name] => City Base Cinema 10
[company_id] => 1
[estimate_id] => 1
[contract_amount] => 253000
[employee_id] => 2
[location_id] => 1
[created] => 2011-07-12 18:17:59
[modified] => 2011-07-16 12:17:55
[Company] => Array
(
[id] => 1
[name] => Search Construction
[address] => 123 street
[city] => San Antonio
[state] => TX
[zip] => 78023
[company_type_id] => 1
[created] => 2011-07-12 18:16:37
[modified] => 2011-07-12 18:16:37
)
[Estimate] => Array
(
[id] => 1
[name] => City Base Cinema 10
[employee_id] => 1
[created] => 2011-07-12 18:08:37
[modified] => 2011-07-12 18:08:37
)
[Employee] => Array
(
[id] => 2
[first_name] => Adam
[last_name] => Morales
[phone] =>
[hourly_pay] => 20.00
[position_id] => 1
[created] => 2011-07-16 12:16:49
[modified] => 2011-07-16 12:16:49
[fullname] => Adam Morales
)
[Location] => Array
(
[id] => 1
[address] => 456 street
[city] => Helotes
[state] => TX
[zip] => 78023
[location_type_id] => 1
[full_local] => 456 street Helotes, TX 78023
)
)
)
)
)
I tried setting a variableField on the Estimate Model:
var $virtualFields = array('total' => 'SUM(EstimateDetail.qty*EstimateDetail.Item.weight*EstimateDetail.Item.price)');
My Index.ctp:
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('name');?></th>
<th><?php echo $this->Paginator->sort('employee');?></th>
<th><?php echo $this->Paginator->sort('created');?></th>
<th><?php echo $this->Paginator->sort('modified');?></th>
<th class="actions"><?php __('Actions');?></th>
</tr>
<?php
$i = 0;
foreach ($estimates as $estimate):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class;?>>
<td><?php echo $estimate['Estimate']['id']; ?> </td>
<td><?php echo $estimate['Estimate']['name']; ?> </td>
<td>
<?php echo $this->Html->link($estimate['Employee']['fullname'], array('controller' => 'employees', 'action' => 'view', $estimate['Employee']['id'])); ?>
</td>
<td><?php echo $estimate['Estimate']['created']; ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View', true), array('action' => 'view', $estimate['Estimate']['id'])); ?>
<?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $estimate['Estimate']['id'])); ?>
<?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $estimate['Estimate']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $estimate['Estimate']['id'])); ?>
</td>
</tr>
and I get an error.
The error I get is:
Warning (2): Invalid argument supplied for foreach() [APP/views/estimates/index.ctp, line 15]
and the Sql:
1054: Unknown column 'EstimateDetail.qty' in 'field list
Please Help.
Short answer would be - don't try to do what you're doing with SQL/virtualFields (if you want, I can elaborate on details why).
Just iterate over your results via foreach($estimates) and calculate sum yourself in PHP.
UPDATE: You can't (easily) use aggregate (SUM) in find() in your example because:
for this, find should be done in one query - i.e. using JOINs - and Cake won't use JOINs willingly on hasMany relationships (it does many simple SELECTs instead), which means you'd have to do them manually (http://book.cakephp.org/view/1047/Joining-tables).
query with SUM would return just one row (grouped), meaning that you'd have to do un-trivial subquery to get both SUM AND your results.

Resources