find value in array cakephp - arrays

Hey guys I am trying to get a value from an array in my events_controller.php file.
Event belongsTo Entity and Entity hasMany Event. I need this value to perform some other logic but im really stuck and i know it should be an easy thing to do.
I am trying to get the value of Entity.user_id from this array.
Array
(
[Event] => Array
(
[id] => 19
[entity_id] => 8
[name] => new event
[time_start] => 2011-02-26 19:09:00
[time_end] => 2011-02-26 19:09:00
[dateStart] => 0000-00-00
[dateEnd] => 0000-00-00
[description] => jgiuguygo
[ageRange] => 67
)
[Entity] => Array
(
[id] => 8
[user_id] => 14
[location_id] => 15
[type] => EVENT
)
[Eventfeedback] => Array
(
)
)
the above matrix i obtained with this code:
$value = $this->Event->read();
pr($value);
Now this is as close as I can get...
Array
(
[Entity] => Array
(
[user_id] => 14
)
[Event] => Array
(
[id] => 19
)
[Eventfeedback] => Array
(
)
)
with this code
$value = $this->Event->read('Entity.user_id');
pr($value);
An last try i got this array
Array
(
[Entity] => Array
(
[id] => 1
[user_id] => 11
[location_id] => 8
[type] => sdfsdfdsf
)
[User] => Array
(
[id] => 11
[firstName] => luis
[lastName] => pooya
[username] => admin
[password] => 94c882c8506497a9f031ca5a4db6d0143c97fe45
[role] => admin
[email] => some
)
[Location] => Array
(
[id] => 8
[name] => First Nation University of Canada
[xCoordinate] => 0
[yCoordinate] => 0
)
[Establishment] => Array
(
)
[Event] => Array
(
)
[Vmachine] => Array
(
)
)
with this code
$value = $this->Event->Entity->find('user_id');
pr($value);
Hope someone can help me out. Thanks in advance.Luis

I'm not sure I understood you correctly. But to get user_id in your examples would be like
$value = $this->Event->read('Entity.user_id');
pr($value['Entity']['user_id']);

$event = $this->Event->read();
$userId = $event['Entity']['user_id'];

Related

array splice multidimensional array

How do I scoop just the '$reference_cart_id' variable from this session_cart_array so i can query db for other matching id's that belong to this customer? When i loop back through an undefined error comes up for reference_id & reference_cart_id.
cartOutput --Array
(
[0] => Array
(
[item_id] => 133
[quantity] => 1
[size] => MED
[color] => Black
[username] => medallion9
[reference_cart_id] => DU3ZWRjNTkwNGYy
[reference_id] => 6cefe27
)
[1] => Array
(
[item_id] => 134
[quantity] => 1
[size] => LG
[color] => Camel-501
[username] => medallion9
[reference_cart_id] => DU3ZWRjNTkwNGYy
[reference_id] => 6cefe27
)
)
You can use array_map (http://php.net/manual/en/function.array-map.php):
$idArray = array_map(function($a){ return $a['reference_cart_id']; },$cartArray);

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

cakephp multiple records data array fit in request data

I need to get multiple records from model, then put them into request->data for a view to render into a form with multiple input fieldsets for example name='data[Applicant][0][display_name]'. name='data[Applicant][1][display_name]'...data value goes in for each applicant.
Actually I've already done what i want, but i do not think it is a good method to do so.
Appreciate if anyone can guide me
foreach ($this->Applicant->data['Applicant'] as $key=>$item){
$data['Applicant'][] = $item['Applicant'];
}
$this->request->data = $data;//set Model to data
$this->set('data' , $this->Applicant->data);
$this->Applicant->data is the following:
Array
(
[Applicant] => Array
(
[0] => Array
(
[Applicant] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
)
[1] => Array
(
[Applicant] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
the following is the desired output (less one level):
Array
(
[Applicant] => Array
(
[0] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
[1] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
thanks
This should suffice:
$this->request->data['Applicant'] = Hash::extract( $this->Applicant->data, 'Applicant.{n}.Applicant' );

Cakephp Model Assosiation Joins

well, in my cakephp project, i have 6 models, they are {User,Property,Category,Status,Comments,Attachable}
Property Model : $belongsTo = {User,Category,Status} . AND $hasMany = {Comment,Attachable} ..
Properties Controller Index Method ..
public function index() {
$this->Property->recursive = 0;
$this->set('properties', $this->paginate());
$properties= $this->paginate();
//pr($properties);
//exit;
}
which outputs :
Array
(
[0] => Array
(
[Property] => Array
(
[id] => 1
[user_id] => 1
[category_id] => 1
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
[userLevel] => 1
)
[Category] => Array
(
[id] => 1
[name] => villa
[property_count] => 0
)
[Status] => Array
(
[id] => 1
[name] => New
[property_count] => 0
)
)
[1] => Array
(
[Property] => Array
(
[id] => 2
[user_id] => 1
[phone] => 78666
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
)
[Category] => Array
(
[id] => 1
[name] => villa
[description] => villas are iby a wealthy person.
[property_count] => 0
)
[Status] => Array
(
[id] => 1
[name] => New
[description] => New Property
[property_count] => 0
)
)
[2] => Array
(
[Property] => Array
(
[id] => 3
[user_id] => 1
[category_id] => 1
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
[email] => admin#realty.com
[userLevel] => 1
)
[Category] => Array
(
[id] => 1
[name] => villa
[property_count] => 0
)
[Status] => Array
(
[id] => 1
[name] => New
[property_count] => 0
)
)
)
Well the above method retrieves data only from the models in $belongsTo variable. What I want is to combine the related Comments and Attachable Models with the same
query. This can be manually queried with LEFT JOIN to comments and attachable MODEL.
this is what the query looks like
function index() {
$properties = $this->Property->query("SELECT `Property`.`id`, `Property`.`user_id`, `Property`.`category_id`, `Property`.`status_id`, `Property`.`state_id`, `User`.`id`, `User`.`name`, `User`.`username`,`Category`.`id`, `Category`.`name`, `Category`.`description`, `Status`.`name`, `Status`.`description``Comment`.`id`,`Comment`.`name`,`Comment`.`comment`,`Attachment`.`id`,`Attachment`.`AttachmentName` FROM `properties` AS `Property`
LEFT JOIN `users` AS `User` ON (`Property`.`user_id` = `User`.`id`)
LEFT JOIN `categories` AS `Category` ON (`Property`.`category_id` = `Category`.`id`)
LEFT JOIN `statuses` AS `Status` ON (`Property`.`status_id` = `Status`.`id`)
LEFT JOIN `cities` AS `City` ON (`Property`.`city_id` = `City`.`id`)
LEFT JOIN `comments` AS `Comment` ON (`Property`.`id` = `Comment`.`id`)
LEFT JOIN `attachments` AS `Attachment` ON (`Property`.`id` = `Attachment`.`id`) WHERE 1 = 1 LIMIT 20");
//pr($properties);
//exit;
$this->set('properties',$properties);
}
which outputs :
Array
(
[0] => Array
(
[Property] => Array
(
[id] => 1
[user_id] => 1
[category_id] => 1
[status_id] => 1
)
[User] => Array
(
[id] => 1
[name] => za
[username] => admin
)
[Category] => Array
(
[id] => 1
[name] => villa
[description] => villa by a wealthy person.
)
[Status] => Array
(
[name] => New
)
[Comment] => Array
(
[id] => 1
[name] => A
[comment] => hello
)
[Attachment] => Array
(
[id] => 1
[AttachmentName] => 1342009083_4c2380.jpg
)
)
[1] => Array
(
[Property] => Array
(
[id] => 2
[user_id] => 1
[category_id] => 1
[status_id] => 1
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
)
[Category] => Array
(
[id] => 1
[name] => villa
[description] => villas a
)
[Status] => Array
(
[name] => New
)
[Comment] => Array
(
[id] => 2
[name] => asdasd
[comment] => asdasdas
)
[Attachment] => Array
(
[id] => 2
[AttachmentName] => 92f2e3c067d731a3823762.jpg
)
)
)
I am sure the latter way is not the right way to do it. Or
Suppose I used the cakephp's default index method in controller, And in VIEW i can do nested foreach loops inside the main foreach loop .. i .e
<?php foreach ($properties as $property) { ?>
$id = $property['Property']['id'];
$comments = ..... //here to query for the associated comments passing the id value ..
?>
can this be sorted out this way ? or how ? please guide .. thanks ..
Two things:
You can probably solve your problem by using the Containable behaviour. It's very flexible and can be used with pagination.
Doing an inner join on one-to-many relation is probably not a good idea.

CakePHP Set::extract parent array based on element condition

say I have an array like this:
[Option] => Array
(
[0] => Array
(
[id] => 30606
[option_description_id] => 1
[product_id] => 101512
[price] => 0
[OptionDescription] => Array
(
[id] => 1
[option_type_id] => 1
[name] => Factory
[short_name] =>
[sort_order] => 1
[OptionType] => Array
(
[id] => 1
[name] => Warranty
[seo_url] => warranty
)
)
)
[1] => Array
(
[id] => 30607
[option_description_id] => 2
[product_id] => 101512
[price] => 44.99
[OptionDescription] => Array
(
[id] => 2
[option_type_id] => 1
[name] => +2 Year Extended
[short_name] =>
[sort_order] => 2
[OptionType] => Array
(
[id] => 1
[name] => Warranty
[seo_url] => warranty
)
)
)
I want to extract the option whose OptionDescription's option_type_id ID is 1
Is there a way to do this easily with Set::extract?
Well, of course, two minutes after I post my question (after hours of trying to figure it out) I realize how easy it is.
In order to extract a parent element, you use /.. in your path syntax.
So my line for all the Options whose type_ids are 2 would be:
Set::extract('/Option/OptionDescription[option_type_id=2]/..', $product);

Resources