Order related element in the view Cakephp - cakephp

I have an Album and Picture Models. Album HasMany Pictures. Picture BelongsTo Album.
I click VIEW on an album, and it shows The Album name, genere, year etc.
And below it shows Related Pictures. So what i want to do is to find a way to sort related picture according a specific column (sort).
albums id | name | genere | year
pictures id| albim_id | picture | sort .
This is the find() statement:
public function view($id = null) {
if (!$this->Album->exists($id)) {
throw new NotFoundException(__('Album not Valid!'));
}
$options = array(
'conditions' =>array(
'Album.'. $this->Album->primaryKey => $id
),
'contain' => array(
'Picture' => array(
'order' => array(
'Picture.sort' => 'ASC'
)
)
)
);
$this->set('album', $this->Album->find('first', $options));
}
but still it doesen't order the picture on ascending order on album view.
when i do a debug($albums); i get this:
'AlbumPictures' => array(
(int) 0 => array(
'id' => '162',
'album_id' => '80',
'pic_path' => '14022988892.jpg',
'sort' => '7',
),
(int) 1 => array(
'id' => '163',
'album_id' => '80',
'pic_path' => '140229888922sYSpcukDw.jpg',
'sort' => '1',
),
(int) 2 => array(
'id' => '164',
'album_id' => '80',
'pic_path' => '1402298889facebook-default-no-profile-pic.jpg',
'sort' => '4',
),
(int) 3 => array(
'id' => '165',
'album_id' => '80',
'pic_path' => '1402298889holder.png',
'sort' => '5',
),
(int) 4 => array(
'id' => '167',
'album_id' => '80',
'pic_path' => '1402298890profile.jpg',
'sort' => '6',
),
(int) 5 => array(
'id' => '170',
'album_id' => '80',
'pic_path' => '1402298890Untitled-15.png',
'sort' => '2',
),
(int) 6 => array(
'id' => '171',
'album_id' => '80',
'pic_path' => '1402298890Untitled-17.png',
'sort' => '3',
)
)
As you see sort column is not in order.

Well In Your Album Model, where your associations are, you can specify the order of related pictures like this.
app/Model/Album.php
public $hasMany = array(
'Pictures' => array(
'className' => 'Pictures',
'foreignKey' => 'album_id',
'order' => array('sort' => 'asc' ),
)
);
hope this helps ;-)

My way is more easy custumize for other case:
$this->Album->recursive=-1;
$album= $this->Album->find('first',
array('joins'=>array(array('type'=>'INNER','table'=>'pictures','alias'=>'Picture',
'conditions'=>array('Album.id=Picture.album_id','Album.id='.$id))),
'order'=>'Picture.sort ASC'));

Related

cant get required records from habtm in cakephp

In Cakephp I need to get the tutors who teach a subject 'primary English'. Instead I get all the tutors with any subject so the condition gets ignored with no error. There is a habtm relationship between tutors and subjects they teach.
$this->Tutor->Behaviors->load('Containable');
$tutors=$this->Tutor->find('all',array(
'contain' => array('Subject',array( 'conditions'=> array('Subject.name' => 'Primary English'))),
'contain' => array('Subject'),
'recursive' =>-1,
// 'order'=> $orderoptions,
'fields'=>array('Tutor.last_name', 'Tutor.first_name','Tutor.id' ),
));
debug( $tutors);
array(
(int) 0 => array(
'Tutor' => array(
'last_name' => 'Wyers',
'first_name' => 'Adele',
'id' => '13'
),
'Subject' => array()
),
(int) 1 => array(
'Tutor' => array(
'last_name' => 'Payet',
'first_name' => 'Allison',
'id' => '7'
),
'Subject' => array(
(int) 0 => array(
'id' => '4',
'name' => 'English - Year 11',
'TutorsSubject' => array(
'id' => '30',
'tutor_id' => '7',
'subject_id' => '4'
)
),
Remove 'recursive' => -1. This prevents it from selecting relationships recursively. If this still doesn't work then put 'recursive' => 2 in.

CakePHP: why does format of data retrieved depend on which associated model is called

I have a site with the usual sort of sellers, items and images. The models look like this:
class Seller extends AppModel {
$hasMany = array('Item');
...
}
class Item extends AppModel {
$belongsTo = array('Seller')
$hasMany = array('Image');
...
}
class Image extends AppModel {
$belongsTo = array('Item');
...
}
My problem is that when I retrieve a list of Items, the data comes out in a different format depending on whether I am getting a list of all Items (to display to the public) in the ItemsController, or the details of a Seller and their Items in the SellersController.
Briefly Item->find('all') retrieves an array of
array( 'Item' => array( <item stuff> ),
'Images' => array of array( <image stuff> ) );
whereas the Seller->find('first', ...) retrieves the Items as
array( 'Seller' => array( <seller stuff> ),
'Item' => array of array ( <item stuff>
'Images' => array of array( <image stuff> ) );
In the 2nd case the Images element is nested within the Item part. Having the data formatted so differently makes it harder to re-use View code. Also my Item afterFind() won't work properly has problems when the Images bit isn't where it expects it to be.
QUESTION
Is there a simple way to prevent the 'Images' part being nested within the 'Item' part in the 2nd situation?
I am using Cake 2.4
Formats in More Detail
All items:
// in ItemsController.php
debug($this->Item->find('all'));
produces this:
array(
(int) 0 => array(
'Item' => array(
'id' => '1',
'title' => 'tom item 1',
'seller_id' => '1'
),
'Seller' => array(
'id' => '1',
'name' => 'tom'
),
'Images' => array(
(int) 0 => array(
'id' => '1',
'item_id' => '1',
'path' => 'tom_1_1'
),
(int) 1 => array(
'id' => '2',
'item_id' => '1',
'path' => 'tom_1_2'
)
)
),
(int) 1 => array(
'Item' => array(
'id' => '2',
'title' => 'tom item 2',
'seller_id' => '1'
),
'Seller' => array(
'id' => '1',
'name' => 'tom'
),
'Images' => array(
(int) 0 => array(
'id' => '3',
'item_id' => '2',
'path' => 'tom_2_1'
),
(int) 1 => array(
'id' => '4',
'item_id' => '2',
'path' => 'tom_2_2'
)
)
),
...
A single Seller + associated Items:
// In SellersController.php
debug($this->Seller->find('first',
array('conditions' => array('id' => $id), 'recursive' => 2)));
produces:
array(
'Seller' => array(
'id' => '1',
'name' => 'tom'
),
'Item' => array(
(int) 0 => array(
'id' => '1',
'title' => 'tom item 1',
'seller_id' => '1',
'Seller' => array(
'id' => '1',
'name' => 'tom'
),
// Look! 'Images' is WITHIN 'Item !!!
'Images' => array(
(int) 0 => array(
'id' => '1',
'item_id' => '1',
'path' => 'tom_1_1'
),
(int) 1 => array(
'id' => '2',
'item_id' => '1',
'path' => 'tom_1_2'
)
)
),
(int) 1 => array(
'id' => '2',
'title' => 'tom item 2',
'seller_id' => '1',
'Seller' => array(
'id' => '1',
'name' => 'tom'
),
'Images' => array(
(int) 0 => array(
'id' => '3',
'item_id' => '2',
'path' => 'tom_2_1'
),
(int) 1 => array(
'id' => '4',
'item_id' => '2',
'path' => 'tom_2_2'
)
)
),
....
This is the expected and correct behavior as Image is not directly associated with Seller, but with Item.
Imagine, how would you determine the proper association when Image would be defined on the same level as Item? That would indicate an association with Seller which doesn't exist.
If you want a different structure, format it in your afterFind callback or wherever it's appropriate. However, I wouldn't recommend trying to work against the CakePHP standards. In case applicable, adapt your view code.
Your question is not clear to me. If just wants to prevent Image to coming in 2nd case, try this
$this->Seller->Item->unbindModel(array(
'hasMany' => array('Image')
));
$result = $this->Seller->find('first',
array('conditions' => array('id' => $id), 'recursive' => 2)));
debug($result);

cakePHP 2.0 Associated models data not being retrieved

I have 2 tables, Militia and Injunctions. Militia can have many injunctions and injunctions only belong to one militia. I set those up in the models but when i call a find all for militia the injunctions aren't pulled out.
Militia model
class Militia extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
public $hasMany = array(
'Injunction' => array(
'className' => 'Injunction',
'foreignKey' => 'militia_id'
//'conditions' => array("not" => array('Injunction.removed' => null))
)
);
}
Injunctions model
class Injunction extends AppModel {
public $belongsTo = array(
'Militia' => array(
'className' => 'Militia',
'foreignKey' => 'militia_id'
)
);
}
and the query getting the militia members
$user = $this->Session->read("User");
$militias = $this->Militia->find('all',
array(
'conditions'=>array(
"Militia.user_id" => $user['User']['id'],
"Militia.deleted" => 0
)
)
);
output
/app/Controller/UsersController.php (line 41)
array(
(int) 0 => array(
'Militia' => array(
'id' => '26',
'first_name' => 'Chris',
'last_name' => 'Morris',
'created' => '2013-02-11 13:45:24',
'user_id' => '2',
'status' => '1',
'deleted' => '0'
)
),
(int) 1 => array(
'Militia' => array(
'id' => '31',
'first_name' => 'John',
'last_name' => 'Smith',
'created' => '2013-02-11 14:03:50',
'user_id' => '2',
'status' => '0',
'deleted' => '0'
)
),
(int) 2 => array(
'Militia' => array(
'id' => '32',
'first_name' => 'test',
'last_name' => 'user',
'created' => '2013-02-11 14:21:38',
'user_id' => '2',
'status' => '0',
'deleted' => '0'
)
),
(int) 3 => array(
'Militia' => array(
'id' => '33',
'first_name' => 'test',
'last_name' => 'user',
'created' => '2013-02-11 14:24:02',
'user_id' => '2',
'status' => '1',
'deleted' => '0'
)
)
)
I've done the same thing before on other projects but for some reason this one time it's not pulling out the associated data. It's probably something stupid like a typo but I've been looking and testing and can't find anything wrong.
Check ORM (hasOne, hasMany, belongsTo, hasAndBelongsToMany) definition,
Check if foreign key ids are set right
Check if there are data's in the table and its related table.
Check the results of find without condition
Check to see if recursive level is greater than 0
To rule out your old cached query issue, set debug level higher to 0
Look out in the bottom section of sql dump to see what query is being fired, if it feels ok try same query with SQL away from Cake/Php.

Cakephp find all query on multiple models

I try the following to get al the Articles That belong to MenuItem 6 and have an Article content_type of 'blog'. It does find all the articles with content_type='blog'. But I only want it to return the Article if it belongs to Menuitem 7. And now it return an empty value for MenuItem when not 7.
How can I accomplish that it'll only load the articles from MenuItem 7?
MenuItem has a HABTM relationship with Article
code:
$d=$this->Article->find('all' , array('contain' => array(
'MenuItem' => array(
'conditions' => array(
'MenuItem.id ' => 7,
),
'fields'=>'id'
),
'Tag'=>array(
'conditions'=>array(
'Tag.name'=>'tag1'
),
'fields'=>'name'
)
),
'conditions'=>array('Article.content_type' => 'blog'),
'fields'=>array('id','content_type'),
'recursive'=>1
));
debug($d);
array:
array(
(int) 10 => array(
'Article' => array(
'id' => '15',
'content_type' => 'blog'
),
'Tag' => array(),
'MenuItem' => array()
),
(int) 11 => array(
'Article' => array(
'id' => '16',
'content_type' => 'blog'
),
'Tag' => array(),
'MenuItem' => array(
(int) 0 => array(
'id' => '7',
'MenuItemsArticle' => array(
'id' => '18',
'title' => '',
'article_id' => '16',
'menu_item_id' => '7'
)
)
)
)
)
I think your best bet for this type of find condition is to modify your models to use hasMany through (The Join Model). Here's an example I tested that does what you want:
Article.php
class Article extends AppModel {
public $hasMany = array('ArticleMenuItem');
}
MenuItem.php
class MenuItem extends AppModel {
public $hasMany = array('ArticleMenuItem');
}
ArticleMenuItem.php
class ArticleMenuItem extends AppModel {
public $useTable = 'articles_menu_items';
public $belongsTo = array(
'Article',
'MenuItem'
);
}
The Find Call
$articles = $this->ArticleMenuItem->find('all', array(
'conditions' => array(
'menu_item_id' => 7,
'Article.content_type' => 'blog'
),
'contain' => array(
'Article' => array(
'fields' => array(
'id',
'title'
)
),
'Tag'
)
));
Here's what it produces:
array(
(int) 0 => array(
'ArticleMenuItem' => array(
'article_id' => '1',
'menu_item_id' => '7'
),
'Article' => array(
'id' => '1',
'content_type' => 'blog',
'title' => 'test blog'
)
),
(int) 1 => array(
'ArticleMenuItem' => array(
'article_id' => '4',
'menu_item_id' => '7'
),
'Article' => array(
'id' => '4',
'content_type' => 'blog',
'title' => 'another'
)
)
)
And here's the query it generates:
SELECT `ArticleMenuItem`.`article_id`, `ArticleMenuItem`.`menu_item_id`, `Article`.`id`, `Article`.`content_type`, `Article`.`title` FROM `caketest`.`articles_menu_items` AS `ArticleMenuItem` LEFT JOIN `caketest`.`articles` AS `Article` ON (`ArticleMenuItem`.`article_id` = `Article`.`id`) WHERE `menu_item_id` = 7 AND `Article`.`content_type` = 'blog'
I've set $recursive = -1 in my AppModel as well. I would suggest doing the same since you are using the containable behavior, it's much more efficient because it only pulls back the data that you need. :)
Hope this helps, any questions just let me know.

Containable Nested models

I have a few nested models which I'm trying to load using Containable behaviour in CakePHP.
Most of it works fine.
However, 1 model that has a hasMany relation only returns 1 object:
$article = $this->News->find('first',array(
'conditions'=>array('News.id'=>$id),
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'Product' => array(
'Brand',
'Category'
)
)))
));
The object only being loaded once is the relation Newspicture hasMany NewspicturesProduct
When I log the queries, I get the following:
SELECT `NewspicturesProduct`.`id`, `NewspicturesProduct`.`x`, `NewspicturesProduct`.`y`, `NewspicturesProduct`.`product_id`, `NewspicturesProduct`.`newspicture_id`, `NewspicturesProduct`.`w`, `NewspicturesProduct`.`h` FROM `edclondon`.`newspictures_products` AS `NewspicturesProduct` WHERE `NewspicturesProduct`.`newspicture_id` = 3
Which gives me 3 results in phpMyAdmin. But only 1 in CakePHP's debug:
'Newspicture' => array(
(int) 0 => array(
'id' => '3',
'news_id' => '2',
'newspicture_file_path' => '5022443f-ddf8-4115-ae57-618e9d60b047.jpg',
'newspicture_file_size' => '1232546',
'order' => null,
'NewspicturesProduct' => array(
'id' => '1',
'x' => '0.180664',
'y' => '0.295312',
'product_id' => '3',
'newspicture_id' => '3',
'w' => '0.286133',
'h' => '0.478125',
'Product' => array(
'id' => '3',
//....
'Brand' => array(
'id' => '6',
//...
),
'Category' => array(
'id' => '6',
//....
)
)
)
)
When retrieving the Newspictures object rather then retrieving the News object, I do get all 3 NewspicturesProduct objects.
It seems to me that the code corresponding the query you showed should be:
$article = $this->News->find('first',array(
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'conditions'=>array('NewspicturesProduct.newspicture_id'=>'3')
'Product' => array(
'Brand',
'Category'
)
)))
));
and not the one you gave...
It seems you need 3 records from NewspicturesProduct. If that then you can try:
$article = $this->News->find('first',array(
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'conditions'=>array(
'limit'=> 3
),
'Product' => array(
'Brand',
'Category'
)
)))
));

Resources