group is not working in CakePHP - 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)
);

Related

group after order

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

Cakephp: Find order by related model

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

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

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.

Deep association relating to top-level model?

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

Resources