CakePHP Save data in HABTM Table - cakephp

I am trying to save an array like this
$myData = array(
'User' => array('id' => 17),
'Group' => array(
array('group_id' => 2),
array('group_id' => 3),
array('group_id' => 4),
array('group_id' => 5),
array('group_id' => 6)
)
);
In my HABTM join table (groups_users). I tried the following save calls, but none of them worked.
$this->User->save($myData);
$this->User->saveAssociated($myData);
$this->User->saveAll($myData);
$this->User->GroupsUsers->save($myData);
$this->User->GroupsUsers->saveAll($myData);
Before you ask: Yes, my associations are set-up correctly and I was able to save data by calling:
$this->User->GroupsUsers->saveAll(array(
0 => array(
'GroupsUsers' => array('user_id' => $id, 'group_id' => 1)
),
1 => array(
'GroupsUsers' => array('user_id' => $id, 'group_id' => 2)
)
));
BUT only one of both records are saved, although I set unique to false in the model's HABTM relationship definition.
Where is the error? Is the structure of my array invalid?

The problem is that your data array is incorrectly formatted.
Your Group model will probably have an id field, and your join model GroupsUser will have a group_id field.
So you need to change your group_id to id

try
$myData = array(
'User' => array('id' => 17),
'Group' => array(
'Group' => array(
0 => 2,
1 => 3,
2 => 4,
3 => 5,
4 => 6
)
)
);

Just to answer your question even if it's old.
You doesn't have to call your 'GroupsUsers' model in your saveAll call, this is precisely the goal of associating Models, cakePHP does that for you.
You should pass this array to a saveAll function on your User/Group model:
array{
array{
'User' => array {
'id' => 25
},
'Group' => array {
'id' => 2
}
}
array{
'User' => array {
'id' => 47
},
'Group' => array {
'id' => 2
}
}
}
Both your User and Group Models should have and HABTM relation with each other, so that you just have to do that:
From the User/Group controller:
$this->User/Group->SaveAll($myData);
From the User/Group Model:
$this->SaveAll($myData);
And that's it, the GroupsUsers table will save 2 records, by saving by yourself in the HABTM relation table, you are not using the power of cakePHP.
And the records in both User and Group tables will be created if the ID doesn't exist, updated if not.
The only reason to save in an HABTM table is if you have some extra information you want to save in this table such as a 'validated' fields if you want to validate a member after he asked to join a group for example.

Related

CakePHP How to get Photo from different table?

Hi i have two table friends and galleries I want to use CakePHP model Association between two table which Friend.friend_id = 1 and Gallery.user_id = 1
friends
id friend_id
-------------
1 5
2 1
3 6
galleries
id user_id photo
------------------------
1 1 photo1.jpg
2 6 photo2.jpg
3 5 photo3.jpg
Above two table i want to join. But i am unable to join. Please help me.
After lots of research finally i got my answer. This code helps to other-
//$user_id=$this->Session->read('Auth.User.id');
$user_id=1;
$this->Friend->bindModel(
array('hasOne' => array(
'Gallery' => array(
'className' => 'Gallery',
'foreignKey' => FALSE,
'conditions' => array('Friend.friend_id=Gallery.user_id'),
'type' => 'LEFT'
)
)
)
);
$this->Friend->bindModel(array('belongsTo' => array('User' => array('className' => 'User', 'foreignKey' => 'friend_id', 'conditions'=>array('Friend.user_id'=>$user_id)))));
$data = $this->Friend->find('all');
pr($data);

CakePHP nested associations, empty records errors

I have few tables which are connected (hasMany) like a tree: 1 -> 2 -> 3.
There are few records in table 1, table 2 and 3.
I'm using CakePHP to fetch all data from table 1 with connected table 2, which is connected with table 3.
However few records in table 1 don't have any connected records in table 2. The same is in table 2, some records don't have connected records in table 3.
For the second situation scripts work fine. I get something like this:
1 -> 2 -> empty. But in the first situation, when data looks similar to: 1-> empty -> empty I get errors that table 3 doesn't exist.
Is there any solution to skip this errors and get pretty formatted association table as return to my query?
$options = array(
'conditions' => array(
'Table1.id' => $table1_ids
),
'contain' => array(
'Table2' => array(
'conditions' => array(
'id' => $table2_ids
),
'Table3' => array(
'conditions' => array(
'date_end >' => date('Y-m-d H:i:s')
),
'fields' => array('id'),
),
'fields' => array()
),
),
'fields' => array('id', 'name')
);
$this->Table1->recursive = -1;
$table1 = $this->Table1->find('all', $options);
It's not really cakephp to blame here but your lack of understanding what contain does.
What you're looking for are left joins, which is actually hinted in the documentation of contain.

cakephp correct array to use updateAll

I am using cakephp 2.5 and I have an array that I wish to update many records at once in the Page model. I can't seem to get the format of the array correct. I get the error:
Notice (8): Undefined index: newOrder [APP/Controller/PagesController.php, line 133]
$newOrder = array(
'Page' => array(
0 => array(
'id' => 3,
'order' => 0),
1 => array(
'id' => 4,
'order' => 0),
2 => array(
'id' => 7,
'order' => 0
)
));
$this->Page->updateAll($newOrder);
One of the parts I think I am missing is using 'data' as part of the array. But I am unsure where to place it.
I have also tried:
$this->Page->updateAll($newOrder['Page']);
You should use saveMany for your requirement. Find the explanation below -
updateAll
updateAll(array $fields, array $conditions) - is used to update one or more records with the same value based on a condition or multiple conditions. eg: If you want to update all your pages & set all of them to order = 0, you can use updateAll without passing the primary keys -
$this->Page->updateAll(
array('Page.order' => 0)
);
If you want to update some pages based on a condition you will do something like -
$this->Page->updateAll(
array('Page.order' => 0),
array('Page.type' => 'PROMOTED')
);
Assuming you have a type field in your page model, the above query will set order 0 for all pages with type PROMOTED
Ref - http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-array-conditions
saveMany
Now if you want to update some specific records with some specific values which you want to create in an array, you should use a saveMany(array $data = null, array $options = array())
To save/update multiple records using a data array, you should first create the data array in this format -
$data = array(
array('Page' => array('id' => 1, 'order' => 0)),
array('Page' => array('id' => 2, 'order' => 0)),
array('Page' => array('id' => 3, 'order' => 0))
);
$this->Page->saveMany($data);
Now you can use saveMany to update the three records with the given id(primary key) with order 0. Note if you don't pass primary keys i.e id in the arrays, saveMany will just create new records for the given array.
Ref - http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array

Saving multiple hasMany through at once

I have a many-to-many relation between 2 models Invoiceand Position defined with the hasMany through method like described in the Cookbook.
Now when i create a Invoice, i want to save multiple Positions to that Invoice in one save()-operation. Something like this:
$data = array(
'Invoice' => array(
// invoice stuff
),
'InvoicesPosition' => array(
[0] => array(
// additional meta information
'Position' => array(
// position stuff
// ...
),
),
[1] => array(
// ...
'Position' => array(
// position stuff
// ...
),
),
// and so on
),
);
$this->Invoice->saveAll($data, array('deep' => true));
The result should be one new Invoice, with 2 Positions linked to it
(That means 1 new record in the invoices table, 2 new records in the positions table and 2 new records in the join table).
Is it possible with Cake's built-in methods? Or do i have to overwrite the saveAll()-method for that model?
Use 'deep' => true option in saveAll() method.
$this->Invoice->saveAll($data, array('deep' => true));

Cakephp complex find condition with count on child table

I have two tables topics and posts. Relation: topics hasmany posts. In both table, there is a status field (Y, N) to moderate the content. In my page, I want to list all not moderated topics for which at least one post status is N or topic status itself is N. Is it possible to do with find function in cakephp2.0. Im using Containable behavior.
I need to apply pagination too.
This is one solution:
Search on the Post model for (Posts with N status) OR (Posts which belong to Topics with N status) and store the topic_id
Now search on the Topic model for topics with ID on the list
Something like this:
# TopicsController.php
$ids = $this->Topic->Post->find('list', array(
'fields' => array('Post.topic_id')
'conditions' => array(
'OR' => array(
'Post.status' => 'N',
'Topic.status' => 'N',
)
)
));
$this->paginate = array(
'conditions' => array('Topic.id' => (array)$ids),
'order' => array('Topic.created' => 'DESC')
);
$topics = $this->paginate('Topic');
Since you're searching on the Posts model, CakePHP will join the parent Topic data and you can filter by both statuses.
If i understand correctly you can use:
$conditions => array ('OR' => array ('Topic.status' => 'N', 'Post.status' => 'N'));
Well I haven't tested it but following should work
$this->recursive = -1; //necessary to use joins
$options['joins'] = array(
'table' => 'posts',
'alias' => 'Post',
'type' => 'left',
'conditions' => array('Topic.id = Post.topic_id', 'Post.status = N') //updated code
);
$options['group'] = array('Topic.id HAVING count('Topic.id') >= 1 OR Topic.status = N');
$this->Topic->find('all', $options);

Resources