cakePHP model associations (retrieving data deeper) - cakephp

I tried to find this everywhere but I cant. Here is my problem:
Lets say I have models associated like this:
Student (has many Memberships)
Membership (belongs to Student and TeacherCourse)
TeacherCourse (belongs to Teacher and Course)
Teacher (has many TeacherCourses)
Course (has many TeacherCourses)
When I use membershipsController to send all membership to the view, I get only for example $membership['Membership']['id'] or $membership['TeacherCourse']['id'] BUT NOT $membership['TeacherCourse']['Teacher']['id']...
Which means I get info on Memberships and TeacherCourse because they are directly associated. My question is how can I also get info about Teachers or Courses directly?

You could increase the model's recursion level to obtain deeper associations (set $this->Membership->recursive to 2 or 3), but in general it's better to use the Containable behavior so you can choose which associations you want to retrieve. For example this would return memberships with associated TeacherCourse and Teacher models:
$membership = $this->Membership->find('all', array(
'contain'=>array(
'TeacherCourse'=>array(
'Teacher'=>array()
),
));
See also the full documentation of the Containable behavior at http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

Scratching my head for a few minutes on this one when I realised you need to add the following to the Model first:
public $actsAs = array('Containable');
Just adding the contain array in the controller may not return the variables you're looking for in the view without the above.

Related

CakePHP Tree-Node-like behaviour on model

I'm currently working on a project in which I'm going to need to create a binary-tree structure on a model. It's something similar to "A person can have 2 friends. Each of those 2 friends are persons as well, so they can also have 2 friends each one." Finally I will need to do a depth-first node search.
I want to do a depth search up to 4 levels, and I also want to know if any of the levels are uncompleted, for example:
person(root)
person(branch A) person(branch B)
person person person person
person person person person (no children) (only 1 child)person
as you can see, branch A is complete up to 4 levels. but branch B is not.
I'm guessing the way to know which branch is complete and which one isn't is to verify using empty() function on the array, something like:
$person = $this->Person->find('first', $conditions);
if(empty($person['Person']['friend_1']))
$uncomplete = true;
I'm missing the algorithm to iterate through nodes here, but what I really need is how do I retrieve 4 depths of hasOne / belongsTo (not sure which one is it), is it setting Model::recursive = 4?
Any suggestions are appreciated, thanks
in Cake we don't have recursive 4. recursive used for associated tables which are associated to current table (other meaning Model behavior) which can be between -1 to 2.
and also regarding to your table structure. please read the cake documents here to see which one one this is suits your table structure. In cake we have 3 kind of model structure. e.g. $actsAs = Tree or Containable or Translate
look at these question and article also:
Reference 1
Reference 2
Reference 3

CakePHP's naming conventions on hasMany through (The Join Model)

After struggling with this inconvenience for a couple of weeks now, I've decided to open en now topic here. A topic that can help me, but I'm sure it will help some others with this same problem too!
I wonder how I should name the tables, controllers and models of a hasMany through table (thus with additional fields) and it's coupled tables. I tried to stick on the CakePHP naming conventions as discribed in it's cookbook, but this constantly gave me some "Object not found" errors. For practical reason, I'll show you my problem with a multiple-words named table. Perhaps that could be the reason of the problem? ;)
Situation
I have a fansite of a themepark and as you now, a themepark has many attractions. To ride an attraction, you must have a minimal height. Sometimes, small people can only ride it with an adult companion. But most of the time: you are allowed to ride the attraction because you just are tall enough ;)
Now I want to show the information of a specific attraction on my website. Name, content, photos, and so on. In addition to that information, I want to display my guests if they (or their kids) are tall enough to ride that attraction. It should appear like this way:
0m00 -> 1m00: not allowed
1m00 -> 1m30: allowed with an adult companion
1m30 -> 1m90: allowed
Database
I have two tables that are representing two objects: "attractions" & "attraction_accessibilities". In this case, I'm 100% sure the database names are correct.
Table "attraction_accessibilities" (id - name):
1 - Not allowed
2 - Allowed with an adult companion
3 - Allowed
Table "attractions" (id - name):
1 - Boomerang
2 - Huracan
3 - Los Piratas
4 - El Volador
...
Secondly, I should have another table between "attractions" and "attraction_accessibilities". This table should contain:
an id specific for each record
a link to the id of the "attractions" table (attraction_id)
a link to the id of the "attraction_accessibilities" table
(attraction_accessibility_id)
the additional information like "min-height" and "max-height"
I think I should name that table "attraction_accessibilities_attractions". It's a constriction of the two other tables, and I did it that way because CakePHP proposed it when you're making a HABTM association (http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm).
But unfortunately, when I do call it that way, I've never succeeded to link those models in my application together.
Question
Is there anybody who've had the same problem but found a solution for it? If "yes": how should I name my database tables then and also important: how should I name my controller and model .php files?
Many thanks for the one who could help me and some other hopeless programmers ;)
If you use the HABTM relationship with unique set to keepExisting then you can name the table as you like and set the joinTable parameter according to he name you choosed
i.e. in your Attraction model
public $hasAndBelongsToMany = array(
'AttractionAccessibility' =>
array(
'joinTable' => 'attraction_accessibilities_attractions',
)
);
Instead if you're going to use the hasMany through relation then you can name the table as you like. In fact the so called "hasMany through" is just the concatenation of two hasMany relationships
so. If you name your table restrictions then
class Attraction extends AppModel {
public $hasMany = array(
'Restriction'
);
}
class AttractionAccessibility extends AppModel {
public $hasMany = array(
'Restriction'
);
}
class Restrictionextends AppModel {
public $belongsTo = array(
'Attraction ', 'AttractionAccessibility '
);
}

How do I add a HABTM record in CakePHP?

I am new to CakePHP and have a fairly basic question.
I have two tables : books and users. books and users have a habtm relationship. I have created the MVC for the above.
Now when a user logs into the system, I want the user to be able to reserve a book (ie an entry in books_users), by looking at the results of the 'index' action. What is the API to be used?
$this->Book->save() does not seem appropriate as we aren't creating a book. We only want an association between an existing book and the logged-in user.
I am trying to avoid, retrieving $this->Book, iterating manually through the sub-array User, creating a new sub-array and saving the whole thing back. I am sure there must be a simpler way.
Adapted from Chuck's answer, unsure why edit was pushed back.
In app/Model/Book.php
class Book extends AppModel {
/************************************************************************
* If you want your multiple assoc. to work you must set unique to *
* false, otherwise when you save an entry it will enforce unique *
* on book ID and subsequently your associations will delete previously *
* saved associations, acting more like "User HasMany Books". *
************************************************************************/
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'unique' => false
));
public function addUser($bid, $uid) {
$this->data['User']['id'] = $uid;
$this->data['Book']['id'] = $bid;
$this->save($this->data);
}
}
In app/Controller/BooksController.php (or UsersController)
$this->Book->addUser($bid, $uid);
Fat Models / Skinny Controllers. Allows duplicate entries (you need to constrain limits and check for duplicates, otherwise default behaviour makes HMBTM difficult). Does exactly what you want it to, you just need to supply book and user id.
CakePHP doesn't tend to encourage complex associations, and the reason this is because HMBTM is just a convenience and care should be taken when mixing it with other associations, as per the link provided below, self defined associations are more predictable than HMBTM in CakePHP
http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated
You simply need to save a record to Book or User that contains the ids of both and it will insert it into the HABTM table.
$this->data['User']['id'] = {USER_ID};
$this->data['Book']['id'] = {BOOK_ID};
$this->Book->save($this->data);
Look at the HABTM table and you will find the record.
did you bake your application? This (basic) functionality will be provided to you for you to adapt.
In short - take the id of the book and the id of the user. Save it to your books_users table.
id | book_id | user_id
1 1 1
2 2 2
3 2 3
If you have set your associations up correctly, when you request a user, all their books will be returned from the join table.
Your logic will need to deal with - number of books available,if a person can reserve more than one book at once...
http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm
Has an example.

CakePHP containable behaviour not firing

First time using Cake and its containable behaviour, but it's not working as expected ... or at all.
I'm trying to obtain a list of accessories for a product. Product model HABTM products (alias 'ProductRelation'). Join table is products_products which has two product ids - product_id and related_id. It's against this I want to pull the list of accessories (products driven from the related_id column) for a given product_id
In my Product model, I've added $actsAs = array('Containable');
And in my controller, a quick test of containable using reference from the cookbook fails to contain products at all, even without conditions.
debug($this->Product->find('all', array('contain' => 'ProductRelation')));
.. returns an array of every product in the db, with ALL related models - images, content tabs, ratings, reviews, pricing, etc I haven't tried applying any 'conditions' against this because the call as written should limit data to the product and it's ProductRelation data, according to the cookbook ...
Any tips?
It seems like you have recursive on. Try using the following:
debug($this->Product->find('all', array(
'contain' => 'ProductRelation',
'recursive' => -1
)));
If that works for you, you should start adding containable to the AppModel class and setting the recursive property to -1. This will ensure you only ever get the results you request.
NB: Cake does not join for HABTM, so you can not use ProductRelation in any conditions.

How do I join two tables in a third n..n (hasAndBelongsToMany) relationship in CakePHP?

I have a n...n structure for two tables, makes and models. So far no problem.
In a third table (products) like:
id
make_id
model_id
...
My problem is creating a view for products of one specifi make inside my ProductsController containing just that's make models:
I thought this could work:
var $uses = array('Make', 'Model');
$this->Make->id = 5; // My Make
$this->Make->find(); // Returns only the make I want with it's Models (HABTM)
$this->Model->find('list'); // Returns ALL models
$this->Make->Model->find('list'); // Returns ALL models
So, If I want to use the list to pass to my view to create radio buttons I will have to do a foreach() in my make array to find all models titles and create a new array and send to the view via $this->set().
$makeArray = $this->Make->find();
foreach ($makeArray['Model'] as $model) {
$modelList[] = $model['title'];
}
$this->set('models', $models)
Is there any easier way to get that list without stressing the make Array. It will be a commom task to develops such scenarios in my application(s).
Thanks in advance for any hint!
Here's my hint: Try getting your query written in regular SQL before trying to reconstruct using the Cake library. In essence you're doing a lot of extra work that the DB can do for you.
Your approach (just for show - not good SQL):
SELECT * FROM makes, models, products WHERE make_id = 5
You're not taking into consideration the relationships (unless Cake auto-magically understands the relationships of the tables)
You're probably looking for something that joins these things together:
SELECT models.title FROM models
INNER JOIN products
ON products.model_id = models.model_id
AND products.make_id = 5
Hopefully this is a nudge in the right direction?
Judging from your comment, what you're asking for is how to get results from a certain model, where the condition is in a HABTM related model. I.e. something you'd usually do with a JOIN statement in raw SQL.
Currently that's one of the few weak points of Cake. There are different strategies to deal with that.
Have the related model B return all ids of possible candidates for Model A, then do a second query on Model A. I.e.:
$this->ModelB->find('first', array('conditions' => array('field' => $condition)));
array(
['ModelB'] => array( ... ),
['ModelA'] => array(
[0] => array(
'id' => 1
)
)
Now you have an array of all ids of ModelA that belong to ModelB that matches your conditions, which you can easily extract using Set::extract(). Basically the equivalent of SELECT model_a.id FROM model_b JOIN model_a WHERE model_b.field = xxx. Next you look for ModelA:
$this->ModelA->find('all', array('conditions' => array('id' => $model_a_ids)));
That will produce SELECT model_a.* FROM model_a WHERE id IN (1, 2, 3), which is a roundabout way of doing the JOIN statement. If you need conditions on more than one related model, repeat until you have all the ids for ModelA, SQL will use the intersection of all ids (WHERE id IN (1, 2, 3) AND id IN (3, 4, 5)).
If you only need one condition on ModelB but want to retrieve ModelA, just search for ModelB. Cake will automatically retrieve related ModelAs for you (see above). You might need to Set::extract() them again, but that might already be sufficient.
You can use the above method and combine it with the Containable behaviour to get more control over the results.
If all else fails or the above methods simply produce too much overhead, you can still write your own raw SQL with $this->Model->query(). If you stick to the Cake SQL standards (naming tables correctly with FROM model_as AS ModelA) Cake will still post-process your results correctly.
Hope this sends you in the right direction.
All your different Make->find() and Model->find() calls are completely independent of each other. Even Make->Model->find() is the same as Model->find(), Cake does not in any way remember or take into account what you have already found in other models. What you're looking for is something like:
$this->Product->find('all', array('conditions' => array('make_id' => 5)));
Check out the Set::extract() method for getting a list of model titles from the results of $this->Make->find()
The solution can be achieved with the use of the with operation in habtm array on the model.
Using with you can define the "middle" table like:
$habtm = " ...
'with' => 'MakeModel',
... ";
And internally, in the Model or Controller, you can issue conditions to the find method.
See: http://www.cricava.com/blogs/index.php?blog=6&title=modelizing_habtm_join_tables_in_cakephp_&more=1&c=1&tb=1&pb=1

Resources