Saving related models with a newly created parent ID in CakePHP - cakephp

I am sure there must be a simple way to do this, but I have been combing the manual and can't find it.
A user creates a new "course" model and in the same form adds a dynamic number of "student" models. Each student belongs to one course. So I receive the data to the controller roughly like this:
Array
(
[Course] => Array(name, etc. etc.),
[Student] => Array(
[0] => Array(student details, etc.),
[1] => Array(student 2 details, etc.))
)
Once the controller receives this, I save the Course data to create the new course. I now have the course unique ID from the database...how do I easily save the student data, adding the course_id automatically to each Student, without having to loop through all of them and adding the course_id manually?
Thanks!

If your model relationships are setup correctly, you can use saveAll().
Check the book on Saving your Data.

Related

Cakephp contains association when using matching()

In my project I have the following tables: Messages, Recipients, Groups and Users. A Message has many Recipients, and a Recipient has one Group and one User.
In my RecipientsTable::beforeFind I have some code to automatically contain Groups and Users for Recipient finds, since I always need to access those associations.
public function beforeFind($event, $query, $options, $primary) {
return $query->contain([
'Groups',
'Users',
]);
}
I don't know if this is a bad design decision but it has worked for me so far.
The problem has come now that I'm trying to filter messages by group, and I tried doing so by using the matching function:
$possible_groups = [1,2,3]; //just an example
$query->matching('Recipients', function($q) use ($possible_groups){
return $q->where(['Recipients.group_id IN' => $possible_groups]);
});
When I execute the query, I get the following error:
Messages is not associated with Groups
Is there any way to keep my beforeFind like that and be able to use matching? Or, is there a better way to automatically load associations without using beforeFind?
TL;DR: A hasMany B, B hasOne C. If a query on table A uses matching on table B and table B's beforeFind uses contain to load C, C ends up contained onto the original query (of A) and the execution fails since A is not associated with C.
Use table classes to load Table associations automatically. Like using hasMany(), hasOne(), belongsTo(), belongsToMany() in necessary table classes.
Official documentation:
https://book.cakephp.org/3.0/en/orm/associations.html
Table files should be in the Table folder inside the Model folder like src\Model\Table.

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 1.3 with muti-type tree

I am using CakePHP 1.3's TreeBehavior. In one DB table I get all the models records. Every record has a type field where the model name is stored. Till now everything is working as a charm. But I get some errors in specific actions. First of all if i do $this->Article->verify() I get errors for almost every record:
Array
(
[0] => Array
(
[0] => index
[1] => 1
[2] => missing
)
[1] => Array
(
[0] => index
[1] => 2
[2] => missing
)
[2] => Array
(
[0] => index
[1] => 3
[2] => missing
)
....
I don't find any difference between the records that has error and those that does not have
I tried Recover but still the same. Why am I getting these errors and what do they mean? I have checked the tree data and every field (parent_id, lft, rght) is correct.
One other issue:
I wold like to move the records up and down in the tree. If I set scope to the record type I move them but all the children records from other types does not move.
My tree is something like this:
Category
Category
Category
Attachment
Category
Attachment
Article
Attachment
Attachment
Article
Attachment
Attachment
Category
Category
If i dont set the scope moving a record sometimes does not see the end of the tree and start moving between other record types. How do I set the scope to avoid this?
EDIT: I have track down the possible problem and probably everything comes from using both Tree and Translate (which i did not mention) behaviours both in multiple Models using one table. So what happen:
When saving Data for each model it also saves a record in the i18n table with the translation. And when checking with verify or recovering the table The Tree behavior only "sees" the posts in the current Model, also giving the "missing index" because not seeing the record from the other models.
Is there any way to fix this, to get it working or to look for other option?
Is there any option to use multiple trees inside one database. So the rght,lft values will be dublicated for every model but there will be no conflict because they will not "know" for each other. Just to use one table and not 10+ with same structure
If your tree metadata is correct then the following should fix any problems which aren't in your own code:
$this->Article->recover($mode = 'tree');
See http://book.cakephp.org/1.3/view/1356/Data-Integrity for more information.

CakePHP HABTM relationship, setting related records always adds new entry in jointable even if already exists

This is my code:
$charities = explode(',',$this->data['Charity']['charities']);
foreach ($charities as $key=>$charity){
$data['Charity'][$key] = $charity;
}
$this->Grouping->id = $this->data['Charity']['grouping_id'];
if ($this->Grouping->save($data)){
//Great!
}else{
//Oh dear
}
The code is from an action that gets called by AJAX. $this->data['Charity']['charities'] is a comma separated list of ids for the Charity model, which HABTM Grouping.
The code works fine, except that Cake doesn't seem to check whether that charity is already associated with that grouping, so I end up with lots of duplicate entries in the join table. I can see that this will be a pain later on so I'd like to get it right now.
What am I doing wrong?
Thanks
A few things I spotted:
A. Is $this->data['Charity']['charities'] an array with named keys corresponding to the table columns? Because inside the foreach() loop, you are taking the key and putting it in the $data['Charity'] array. The result could be a wrong format. The $data array for Model saves is commonly formatted as $data['Charity']['NAME_OF_COLUMN1], $data['Charity']['NAME_OF_COLUM2], and so on for each column that wants to be saved. So, if the keys are numbers, then you could be having something like: $data['Charity'][0], $data['Charity'][1], which is wrong.
B. HABTM Associations are saved differently. What I do recommend, is that you take a look at the book's section on saving HABTM. I save a HABTM relation like this (supposing a relation between users and coupons):
$this->data['Coupon'] = array('id' => 1, 'code' = 'BlaBla');
$this->data['User'] = array('id' => 33);
$this->Coupon->save($this->data, false);
So, as you can see, the $data array has a subarray named after the foreign model ('User') with the columns+values that I want to save. Then I save this array to the Coupon model.
C. Be aware that CakePHP will always delete old records and insert new ones within the join table when it is 'updating' HABTM relations. To avoid this, you set the 'unique' field to false in the Model's $HABTM configuration.

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