I want to achieve a structure where e.g. an Organisation has many Departments, and where Departments has many Persons.
I've set up my model structure like this:
Organisations
<?php
class Organisation extends AppModel {
public $hasMany = array(
'Department' => array(
'className' => 'Department',
'foreignKey' => 'organisations_id'
)
);
}
Departments
<?php
class Department extends AppModel {
public $hasMany = array(
'Person' => array(
'className' => 'Person',
'foreignKey' => 'departments_id'
)
);
}
Persons
<?php
class Person extends AppModel {
}
Then I have a controller like this:
<?php
class OrganisationsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->set('organisations', $this->Organisation->find('all'));
}
}
When I print out $organisations I get an array like this:
Array
(
[0] => Array
(
[Organisation] => Array
(
[id] => 1
[created] => 2013-01-03 16:02:47
)
[Department] => Array
(
[0] => Array
(
[id] => 1
[created] => 2013-01-03 16:02:47
[organisations_id] => 1
)
)
)
)
I'm new to both PHP and CakePHP, but wouldn't you expect the Person array to be included in the Organisation array? And if not, is there another way to achieve a structure like the one described above (Organisation->Department->Person)?
Any hints on how to go about this is highly appreciated! :)
You are probably looking for recursive
Or you could make use of the containable behaviour
But please have a look at the result. When using recursive you can get a lot of data you don't want! So please be careful and select the fields you actually need!
Recursive
You would get something like:
<?php
class OrganisationsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->Organisation->recursive = 2; # or -1, 0, 1, 2, 3
$this->set('organisations', $this->Organisation->find('all'));
}
}
You could also declare this in the find itself like so:
$this->set('organisations', $this->Organisation->find('all' array(
'recursive' => 2 # or -1, 0, 1, 2, 3
)
));
Containable
class Organisation extends AppModel {
public $hasMany = array(
'Department' => array(
'className' => 'Department',
'foreignKey' => 'organisations_id'
)
);
$actsAs = array('Containable');
}
Now in your controller you can do something like:
$this->set('organisations', $this->Organisation->find('all', array(
'contain' => array('User')
)
));
But as always, there are many roads leading to Rome. So please read the books very carefully!
http://book.cakephp.org/2.0/en/index.html
http://book.cakephp.org/2.0/en/models/model-attributes.htm
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
Recursive function will do it for you.
Just in OrganisationsController index function , try doing like below.
$this->Organisation->recursive = 2;
$this->set('organisations', $this->Organisation->find('all'));
Note: Recursive may affect your performance , you can use unbind method to get rid of it by just fetching the data you want.
Related
I'm having some trouble with CakePHP's models.
I have three tables - CC_PLAYLIST, CC_PLAYLISTCONTENTS, and CC_FILES.
I'd like to get a list of all playlists, with all of their contents, and then the relevant file information for each piece of playlist content.
I.E
Playlist 1
-- Playlist Content 1
---- File Info
-- Playlist Content 2
---- File Info
-- Playlist Content 3
---- File Info
Playlist 2
-- Playlist Content 123
---- File Info
In my controller I have:
$playlistcontent = $this->AirtimePlaylist->find('all') however that seems to turn up a flat array with all playlistcontents, all files, and empty playlist arrays.
AirtimeFile.php
class AirtimeFile extends AppModel {
public $useTable = 'cc_files';
public $actsAs = array('Containable');
public $hasMany = array('AirtimeFileAttribute' => array(
'className' => 'AirtimeFileAttribute',
'foreignKey' => 'track_id'
));
}
AirtimePlaylist.php
class AirtimePlaylist extends AppModel {
public $useTable = 'cc_playlist';
public $actsAs = array('Containable');
public $hasMany = array('AirtimePlaylistContent' => array(
'className' => 'AirtimePlaylistContent',
'foreignKey' => 'playlist_id'
));
}
AirtimePlaylistContent.php
class AirtimePlaylistContent extends AppModel {
public $useTable = 'cc_playlistcontents';
public $actsAs = array('Containable');
public $hasOne = array('AirtimePlaylist' => array(
'className' => 'AirtimePlaylist',
'foreignKey' => 'id'
),'AirtimeFile' => array(
'className' => 'AirtimeFile',
'foreignKey' => 'id'
));
}
In your controller you need to use contain
$model = $this->AirtimePlaylist->find('all',
array(
'contain' => array('AirtimePlaylistContent' => array('AirtimeFile') )
)
);
I am having trouble with linking data like first name from the table Users in the view of chats using cakePHP. Below is my code and whatever I do, the script does not select the user_id from the chats database table to display the first name. I must be overseeing something, but the loop I'm thinking in is giving me some headaches. Can someone please get me out of this loop?
User.php
<?php
class User extends AppModel {
public $hasMany = array(
'Ondutylog',
'Chat'
);
}
?>
Chat.php
<?php
class Chat extends AppModel {
public $belongsTo = array(
'User'
);
}
?>
ChatsController.php
<?php
class ChatsController extends AppController {
var $uses = array('User', 'Chat');
public function view() {
$chats = $this->Chat->find('all', array(
'order' => array('id' => 'ASC'),
'recursive' => -1
));
$this->set('chats', $chats);
$id = $chats['Chat']['user_id'];
$userdetails = $this->Chat->User->find('first', array(
'conditions' => array(
'id' => $id
),
recursive' => -1
));
return $userdetails;
}
}
?>
view.ctp
<?php
foreach($chats as $chat) :
echo "<tr>";
echo "<td>".$userdetails['User']['firstname']."</td>";
echo "<td>".$chat['Chat']['user_id']."</td>";
echo "<td>".$chat['Chat']['text']."</td>";
echo "<td>".$chat['Chat']['created']."</td>";
echo "</tr>";
endforeach
?>
The array I get returned in $chats
[Chat] => Array
(
[id] => 1
[user_id] => 11
[text] => hello
[created] => 2014-05-21 19:56:16
[modified] => 2014-05-21 19:56:16
)
Change
return $userdetails;
to
$this->set(compact('userDetails'));
You are supposed to set the view var not return the info.
Though why are you making a separate query for it instead of just using 'recursive' => 0 which would get the associated user record through table join and you can just use $chat['User']['firstname'] in view.
Also get rid of var $uses = array('User', 'Chat');. It's not needed. $this->Chat is already available and the User model is accessed through association as $this->Chat->User as you have already done.
You need charge the model in your controller
class ChatsController extends AppController {
public function view() {
$this->loadModel('User');
$this->loadModel('Chat');
$chats = $this->Chat->find('all', array(
'order' => array('id' => 'ASC'),
'recursive' => -1
));
$this->set('chats', $chats);
$id = $chats['Chat']['user_id'];
$userdetails = $this->Chat->User->find('first', array(
'conditions' => array(
'id' => $id
),
recursive => -1
));
$this->set(compact('userDetails'));
}
}
I found the solution and it's closer than I thought of myself. Because the Models User and Chat were already joined I just had to use a couple of lines in the Controller. So I modified it like this:
public function view() {
$chats = $this->Chat->find('all');
$this->set('chats', $chats);
}
And nothing more...
I have a model class in CakePHP defined like this:
class Programme extends AppModel {
public $hasOne = array(
'ProgrammeLikes' => array(
'className' => 'ProgrammeLikes',
'fields' => array('likes'));
}
When retrieving my models from the database they are returned as an array with an array keyed to 'Programme' and a separate array keyed to 'ProgrammeLikes' (which contains the 'likes' value correctly). In order to reduce the changes necessary to existing code I want the 'likes' value to be within the 'Programme' array.
Is this possible?
Thanks in advance
Use virtualFields here to get this thing to be done.
class Programme extends AppModel {
public $hasOne = array(
'ProgrammeLikes' => array(
'className' => 'ProgrammeLikes',
'fields' => array('likes')
);
public $virtualFields = array(
'likes' => 'SELECT likes FROM programme_likes AS ProgrammeLikes WHERE ProgrammeLikes.id = Programme.programme_likes_id'
);
// Where programme_likes_id is the foriegnkey for Programme model
}
Note: I assumed programme_likes is your table name for ProgrammeLikes Model and programme_likes_id is the foriegnkey for
Programme Model, so you can arrange the query in your own way that suits your requirement.
Sorry if the Title misses what i'm aiming at, but had no clue how to name this. Here's my Quest:
I have a single Cart, holding multiple Items (1:n) which are belonging to prices (1:1).
The Relations between Items and Prices are working as well:
class Item extends AppModel {
public $hasOne = 'Price';
}
class Price extends AppModel {
public $belongsTo = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'id'
)
);
}
But now i make a select (find) of a cart and want those items included, which is also working as well:
class CartItem extends AppModel {
public $useTable = 'cart_items';
public $belongsTo = array('Item', 'Address');
}
But what i really need is to get those prices of each item, which is not working ($result in afterFind()-Callback in Item-Model does not include the assigned prices & afterFind()-Callback in Price-Model is not called when finding a cart)..
what am i missing here?
/EDIT: Recent Changes:
class AppModel extends Model {
var $actsAs = array('Containable');
}
class CartsController extends AppController {
public function getCart() {
$cart = ClassRegistry::init('CartItem')->find('all', array(
'contain' => array(
'Item' => array(
'Price'
),
'Address'
),
'conditions' => array(
'id_cart' => $cart['Cart']['id']
)
));
}
The above changes cause that i'll get a Price into a found Item but only into the last one thats found.
You haven't shown your actual find(), but I suspect you're not setting an appropriate 'recursive' param or are not using 'contain'.
I prefer using the containable behaviour enabled from AppModel:
var $actsAs = array('Containable');
Then you can do something like:
$cartItem = $this->CartItem->find(
'first',
array(
'contain' => array(
'Item' => array(
'Price'
),
'Address'
),
'conditions' => array('CartItem.id' => 123)
)
);
My Category Model:
class Category extends AppModel {
public $displayField = 'name';
// public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Post' => array(
'className' => 'Post',
'joinTable' => 'categories_postss',
'foreignKey' => 'category_id',
'associationForeignKey' => 'post_id',
'unique' => 'keepExisting'
)
);
}
$params['contain'] = array('Post' => array(
'limit'=> 3));
pr($this->Category->find('first',$params)); exit;
It is fetching all Posts, irrespective of limit.
What I want to do:
I have this page where I ma listing all the categories and latest 5 posts related to it.
I want to limit the associated model to only 5 rows.
Any ideas?
Containable behavior is not in use
The most likely reason for this problem is that the containable behavior is not being used at all.
Compare, for the below code example:
$results = $this->Category->find('first', array(
'contain' => array(
'Post' => array(
'limit' => 3
)
)
));
Without containable behavior, it'll generate the following queries:
SELECT ... FROM `crud`.`categories` AS `Category` WHERE 1 = 1 LIMIT
SELECT ... FROM `crud`.`posts` AS `Post`
JOIN `crud`.`categories_posts` AS `CategoriesPost` ON (...)
With containable behavior, it'll generate the following queries:
SELECT ... FROM `crud`.`categories` AS `Category` WHERE 1 = 1 LIMIT
SELECT ... FROM `crud`.`posts` AS `Post`
JOIN `crud`.`categories_posts` AS `CategoriesPost` ON (...) LIMIT 3
Given this (and the code in the question) check that the AppModel has the containable behavior in $actsAs:
<?php
// app/Model/AppModel.php
class AppModel extends Model {
public $actsAs = array('Containable');
}
Limit always required?
Alternatively, or possibly in addition, you may prefer to put a limit in the association definition - To do so just define the 'limit' key:
class Category extends AppModel {
public $hasAndBelongsToMany = array(
'Post' => array(
'limit' => 100, // default to a high but usable number of results
)
);
}
the hasAndBelongsToMany relationship seems unnecessary to me. I think you only need Category hasMany Post and Post belongsTo Category relationships. Add category_id to the posts table. Make both models actAs containable.
Post Model
class Post extends AppModel {
public $actsAs = array('Containable');
var $belongsTo = array(
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id'
),
// ... more relationships
);
Category Model
class Category extends AppModel {
public $actsAs = array('Containable');
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'category_id'
),
// ... more relationships
);
Categories Controller
class CategoriesController extends AppController {
public $paginate = array(
'Category' => array(
'contain' => array(
'Post' => array(
'limit' => 3
), // end Post
) // end Category contain
) // end Category pagination
); // end pagination
public function index() {
// for paginated results
$this->set('categories', $this->paginate());
// for find results
$this->Category->contain(array(
'Post' => array(
'limit' => 3
)
));
$this->set('categories', $this->Category->find('all'));
}