I am using a $this->find('threaded') in my CakePHP application and am trying to pull in an associated model within the find (HABTM). I have tried several methods, such as 'joins', 'recursive' and 'contains' all with no luck. I am using CakePHP 2.3.6
Here is my (working) code.
class EventsController extends AppController {
public function promoters($id = null) {
$options = array('conditions' => array('Event.id' => $id));
$event = $this->Event->find('first', $options);
$this->set('event', $event);
$this->loadModel('EventsPromoter');
$treelistConditions = array(
'conditions' => array(
'event_id' => $id
),
);
$promoterTree = $this->EventsPromoter->find('threaded', $treelistConditions);
$this->set('promoters', $promoterTree);
}
}
This results in the following array output
Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 1
[promoter_id] => 1
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 10
[promoter_id] => 4
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 13
[promoter_id] => 6
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 2
[promoter_id] => 2
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 11
[promoter_id] => 5
[event_id] => 1
[parent_id] => 2
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 6
[promoter_id] => 3
[event_id] => 1
[parent_id] => 11
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 14
[promoter_id] => 7
[event_id] => 1
[parent_id] => 2
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
)
)
)
)
)
What I would like my code to output is the following
Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 1
[promoter_id] => 1
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 1
[first_name] => 'Bob'
[last_name] => 'Smith'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 10
[promoter_id] => 4
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 4
[first_name] => 'Sally'
[last_name] => 'Sue'
)
[children] => Array
(
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 13
[promoter_id] => 6
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 6
[first_name] => 'Ben'
[last_name] => 'King'
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 2
[promoter_id] => 2
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 2
[first_name] => 'Jack'
[last_name] => 'Sparrow'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 11
[promoter_id] => 5
[event_id] => 1
[parent_id] => 2
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 5
[first_name] => 'Jane'
[last_name] => 'Doe'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 6
[promoter_id] => 3
[event_id] => 1
[parent_id] => 11
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 3
[first_name] => 'Mike'
[last_name] => 'Jones'
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 14
[promoter_id] => 7
[event_id] => 1
[parent_id] => 2
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 7
[first_name] => 'Spider'
[last_name] => 'Man'
)
[children] => Array
(
)
)
)
)
)
Nothing I have been trying is working, and this seems like something Cake should be able to handle fairly easily. Thanks in advance for any help!
I was taking a slightly wrong approach for what I was trying to accomplish. While I still think this should be possible from my EventsController class, I elected to make it in the EventsPromotersController HABTM class. The code is much simpler using this method
class EventsPromotersController extends AppController {
public function event($event_id = null) {
$options = array('conditions' => array('event_id' => $event_id));
$promoters = $this->EventsPromoter->find('threaded', $options);
$this->set('promoters', $promoters);
}
}
Related
I have two arrays first array is main comment and second array is sub comment.
1st Array
Array
(
[0] => Array
(
[id] => 1
[comment] => Nice Blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
[cnt] =>
)
[1] => Array
(
[id] => 2
[comment] => Awesome blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
[cnt] => 2
)
)
2nd Array
Array
(
[0] => Array
(
[id] => 3
[comment] => Yes, Really good
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
)
[1] => Array
(
[id] => 4
[comment] => Thank you
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
)
)
Now we need an array like this with a single array based on "parent_id" and "id":
I want new array like this :
Array
(
[0] => Array
(
[id] => 1
[comment] => Nice Blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
[cnt] =>
)
[1] => Array
(
[id] => 2
[comment] => Awesome blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
[cnt] => 2
[sub_comment] => Array
(
[0] => Array
(
[id] => 3
[comment] => Yes, Really good
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
)
[1] => Array
(
[id] => 4
[comment] => Thank you
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
)
)
)
)
So I can easily know this comment have sub comment and display based on new array.
Would you please let me know is it possible or not ?
Thanks in advance.
// Suppose $array1 is firts array;
// $array2 is second array
foreach($array2 as $subary){
$cnt=0;
foreach($array1 as $key=> $mainary){
if($subary['parent_id']==$mainary['id']){
$array1[$key]['sub_comment']=$subary;
}
}
}
print_r($array1);
I'm writing this query in yii2 -
$senderInfo[] = Customers::find()->where(['cust_id' => $resShipment['ship_shipper_id'], 'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all();
and getting this as a result -
Array ( [0] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( [0] => common\models\Customers Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => abcdxyz#gmail.com [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 02881 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => abcdxyz#gmail.com [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 413714 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) ) )
But, because of the empty arrays in the start of this array, I can't access the elements. I've to populate it in a for(strictly) loop, after it gets accessible.
If you want the result in array you can use ActiveRecord function toArray()
$myModel = Customers::find()->
where(['cust_id' => $resShipment['ship_shipper_id'],
'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all();
$senderInfo = myModel->toArray();
I want to change an array format according to my needs. When I fetch data from a database using cakephp find('all') method, it returns something that is not in the format that I expected.
My resultant array is:
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-20 12:25:34
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
[zip_code_id] => 42
[created] => 2015-05-20 12:37:12
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 42
[created] => 2015-05-20 12:37:28
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[3] => Array
(
[DriverLocation] => Array
(
[id] => 26
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 43
[created] => 2015-05-20 12:43:59
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[4] => Array
(
[DriverLocation] => Array
(
[id] => 41
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 6
[created] => 2015-05-21 05:23:53
)
[ZipCode] => Array
(
[id] => 6
[province_id] => 3846
[city] => Whittier
[zip_code] => 90607
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
)
And I want to convert it into something like this:
Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 15478
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
[1] => 15478
[2] => 90607
)
[city] => Array
(
[0] => Rohtak
[1] => Rohtak
[2] => Whittier
)
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
Filter according to DriverLocation->driver_id
Seems that these arrays are exactly the same, except that you put every "record" of DriverLocation in its own variable. The idea here is that you receive the data from the DriverLocation Model in the fictional controller and put it in a variable; for example:
$allDriverLocations = $this->DriverLocation->find("all");
Now $allDriverLocations contains the first one of your mentioned arrays.
Next step is to pass it to the view:
$this->set(compact("allDriverLocations"));
And now to create a table in the associated view you should iterate through $allDriverLocations:
<table>
<tbody>
<?php
// Loop through the array with a foreach
foreach($allDriverLocations as $driverLocation){
// Create the table row here using the HtmlHelper.
// If you want to reach "Rohtak" for example, you use $driverLocation["Zipcode"]["city"] to print it.
}
?>
</tbody>
</table>
I wouldn't know why you should not follow Cake's easy conventions...
I have a array looks like this:
Array
(
[0] => Array
(
[Project] => Array
(
[id] => 3
[title] => Title001
[created] => 0000-00-00 00:00:00
[modified] => 2013-08-05 17:39:07
)
[Keyword] => Array
(
[0] => Array
(
[id] => 1
[project_id] => 3
[title] => Num1
[demand] => 50000000000
[competition] => 37889.56700
[cpc] => 676.50
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:25
)
[1] => Array
(
[id] => 13
[project_id] => 3
[title] => test
[demand] => 314
[competition] => 2341.00000
[cpc] => 9999.99
[created] => 2013-09-16 11:05:12
[modified] => 2013-09-16 11:05:12
)
)
)
[1] => Array
(
[Project] => Array
(
[id] => 4
[title] => Erdmann
[created] => 0000-00-00 00:00:00
[modified] =>
)
[Keyword] => Array
(
[0] => Array
(
[id] => 3
[project_id] => 4
[title] => Num5
[demand] => 76534000000
[competition] => 5555.55560
[cpc] => 99.34
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:36
)
[1] => Array
(
[id] => 4
[project_id] => 4
[title] => Num anything
[demand] => 84000
[competition] => 8765.62340
[cpc] => 543.83
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-11 12:08:50
)
)
)
[2] => Array
(
[Project] => Array
(
[id] => 5
[title] => Value
[created] => 2013-09-11 11:18:22
[modified] => 2013-09-11 11:18:22
)
[Keyword] => Array
(
[0] => Array
(
[id] => 2
[project_id] => 5
[title] => foo
[demand] => 4500000000
[competition] => 876.78500
[cpc] => 66.67
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:32
)
[1] => Array
(
[id] => 5
[project_id] => 5
[title] => bar
[demand] => 5000568
[competition] => 667.56543
[cpc] => 667.60
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-11 12:08:09
)
)
)
[3] => Array
(
[Project] => Array
(
[id] => 6
[title] => wolrd.net
[created] => 2013-09-16 11:23:42
[modified] => 2013-09-16 11:23:42
)
[Keyword] => Array
(
[0] => Array
(
[id] => 15
[project_id] => 6
[title] => Earth
[demand] => 5363
[competition] => 93479.34000
[cpc] => 5599.99
[created] => 2013-09-16 11:25:15
[modified] => 2013-09-16 11:26:15
)
)
)
)
What is the right way to access Title001 with all Keywords?
I want to be able to group SpecificationCategory.name as parent category and Specifications under as children so that SpecificationCategory.name does not get repeated in the array.
Is there a way to do this in Cake?
Array
(
[0] => Array
(
[Specification] => Array
(
[id] => 15
[name] => AMD Phenom
[specification_category_id] => 1
[subsubcategory_id] => 1
[created] => 2012-10-16 08:18:27
[modified] => 2012-10-16 08:18:27
)
[SpecificationCategory] => Array
(
[id] => 1
[name] => Processor Type
[created] => 2012-10-15 13:50:03
[modified] => 2012-10-15 13:50:03
)
[Subsubcategory] => Array
(
[id] => 1
[name] => Laptops
[subcategory_id] => 1
[created] => 2012-06-24 02:23:40
[modified] => 2012-06-24 02:23:40
)
)
[1] => Array
(
[Specification] => Array
(
[id] => 12
[name] => AMD E350
[specification_category_id] => 1
[subsubcategory_id] => 1
[created] => 2012-10-16 08:18:02
[modified] => 2012-10-16 08:18:02
)
[SpecificationCategory] => Array
(
[id] => 1
[name] => Processor Type
[created] => 2012-10-15 13:50:03
[modified] => 2012-10-15 13:50:03
)
[Subsubcategory] => Array
(
[id] => 1
[name] => Laptops
[subcategory_id] => 1
[created] => 2012-06-24 02:23:40
[modified] => 2012-06-24 02:23:40
)
)
)
If your just trying to get a listing by SpecificationCategory I would just do the find from that model and the array would list all Specifications under each category. This assumes Specifications hasMany SpecificationCategory.