I need to find all the records from an array of id ( $user_id = array(); ) to be saved into a table of notifications, to tell these users that their accounts are activated.
After executed these,
$x = $this->User->find('all',array('conditions'=>array('User.id'=>$user_id)));
$find = Set::extract('/User', $x);
I get this result:
Array(
[0] => Array
(
[User] => Array
(
[id] => 2
[name] => joe
[age] => 13
[class] => D
)
)
[1] => Array
(
[User] => Array
(
[id] => 3
[name] => lambert
[age] => 14
[class] => E
)
)
)
I need to achive the array below by extracting the one above
Array
{
[id] => 2
[name] => joe
[age] => 13
[class] => D
}
Array
{
[id] => 3
[name] => lambert
[age] => 14
[class] => E
}
How to make this happen using CakePHP?
Then, inside my view, how to send multiple records based on the extracted array above?
Is it possible to make a foreach loop inside view?
echo $html->link(__('Send',true),array('controller'=>'users','action'=>'notifications',$user_id),array('class'=>'button'));
Obviously, putting the link inside the loop will result in two links with different ids ($user_id = 2 and $user_id = 3). I don't want that to happen. I want a single link that will submit these ids in one go.
The answer for your first question
$data_final =array();
foreach($findas $d)
{
$data_final[] =$d['User'];
}
then you wil achive
Array
(
[0] => Array
(
[id] => 2
[name] => joe
[age] => 13
[class] => D
)
[1] => Array
(
[id] => 4
[name] => lamber
[age] => 23
[class] => E
)
)
The answer to second question
the way you are doing with url is not a good practice. you should post the records
Related
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' );
I have an activity table with 3 $belongsTo reference keys. I need the join information from these id's i.e. to show the commenttext - I don't want to store the text twice... (same for post and topic).
Activity table: id | post_id | comment_id | topic_id
In each row only post_id OR comment_id OR topic_id is set, the other two id fields are NULL.
So i.e. if post_id = 55, comment_id = NULL, topic_id = NULL I get this:
Array
(
[0] => Array
(
[Activity] => Array
(
[id] => 1
[post_id] => 55
[comment_id] =>
[topic_id] =>
)
[Post] => Array
(
[id] => 55
[name] => Post #1
[description] => This is Post #1.
...
)
[Comment] => Array
(
[id] =>
[post_id] =>
[titel] =>
[description] =>
...
[created] =>
[modified] =>
)
[Topic] => Array
(
[id] =>
...
[created] =>
[modified] =>
)
)
[1] => Array
(
...
Is there a way to join only if the reference id is NOT NULL? I don't want to kill the empty arrays after the find with a php for-each loop.
Another idea was this database table: id | activitytype_id | refid to join with dynamic binding the necessary table depending on the activitytype_id. - That didn't work as well...
That's what I want - is that possible?
Array
(
[0] => Array
(
[Activity] => Array
(
[id] => 1
[post_id] => 55
[comment_id] =>
[topic_id] =>
)
[Post] => Array
(
[id] => 55
[name] => Post #1
[description] => This is Post #1.
...
)
)
[1] => Array
(
[Activity] => Array
(
[id] => 2
[post_id] =>
[comment_id] => 2
[topic_id] =>
)
[Comment] => Array
(
[id] => 2
[post_id] => 4
[titel] => Blabla
[description] => This is the comment description
...
[created] => 2011-01-01 01:30:00
[modified] => 2011-01-01 01:30:00
)
)
[2] => Array
(
...
Thanks in advance! :-)
You would need to query the database to find out which IDs are null and then query the database a second time to grab the related data.
$activity = $this->Activity->read(null, 1);
// some logic to find foreign key with non-null value
$activity[$model] = $this->{$model}->read(null, $id);
I wouldn't waste your time writing two queries; let CakePHP get all the results in a single query. :)
$activity = $this->Activity->findById(1);
Just add this to your model to filter out empty values from results:
public function afterFind($results, $primary = false) {
return Hash::filter($results);
}
You can always make the joins manually. What you want is to do INNER JOIN instead of the LEFT JOIN done by cake. You can also do an afterFind() to delete this something like this.
in your model where you are using the find method
function afterFind($results){
foreach($results as $k => $result){
if (empty($result['Post']['id'])){
unset($results[$k]['Post']);
}
if (empty($result['Comment']['id'])){
unset($results[$k]['Comment']);
}
if (empty($result['Topic']['id'])){
unset($results[$k]['Topic']);
}
}
}
The join is a more direct solution though.
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);
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'];
I have the output array from a $Model->find() query which also pulls data from a hasMany relationship:
Array(
[Parent] => Array
(
[id] => 1
)
[Child] => Array
(
[0] => Array
(
[id] => aaa
[score] => 3
[src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
[parent_id] => 1
)
[1] => Array
(
[id] => bbb
[score] => 5
[src] => stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
[parent_id] => 1
)
[2] => Array
(
[id] => ccc
[score] => 2
[src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
[parent_id] => 1
)
)
)
I'd like to transform this output into something like this, where the child id is the key to additional child attributes:
Array(
[aaa] => Array
(
[score] => 3
[src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
)
[bbb] => Array
(
[score] => 5
[src] => stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
)
[ccc] => Array
(
[score] => 2
[src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
)
}
Is there an easy way to use Set::extract, Set::combine, Set::insert, etc. to do this efficiently? I cannot figure it out.
I've used Set::combine to do this a few times, but I can't make an argument for efficiency. As an example, I had a list of volunteers and each of those volunteers belonged to a group. I wanted to retrieve each committee and its volunteers, but as an associative array keyed by the committee name. Here's the code I used to do that:
$volunteers = Set::combine (
$this->Volunteer->Committee->find (
'all',
array ( 'order' => 'display_order' )
),
'{n}.Committee.title',
'{n}.Volunteer'
);
Hopefully that helps. The documentation for Set::combine may help elaborate on the details.
As Rob says, Set::combine is the answer to your question. Here's a sample solution, using the XPath 2.0 syntax:
$parents = $this->Parent->find('all');
$assocChildren = Set::combine( $parents, '/Child/id', '/Child' );