CakePHP 2.4 saveAssociated not saving all children elements - cakephp

Problem Synopsis:
(CakePHP v2.4.2) When I use saveAssociated (or saveAll, same result) for input for a new record with a hasMany/belongsTo relationship with multiple child elements, only the last child element gets saved because it INSERTs the first element, but then executes UPDATES for subsequent elements.
I've used saveAssociated for very similar purposes in this same application and had no problem with it, so I'm baffled.
Queries on all these work just fine, i.e., I get the multiple children associated with each parent.
Models synopsis:
class Site extends AppModel {
// sites columns: id (primary key), bunch of others
public $hasMany = array(
'SiteUser' => array(
'className' => 'SiteUser',
'foreignKey' => 'id', // Yes, I would have preferred 'site_id', lost battle
'dependent' => true
)
);
}
class SiteUser extends AppModel {
// site_users columns: rowid(PK), id (FK to sites), name
public $belongsTo = array(
'className' => 'Site',
'foreignKey' => 'id'
);
}
Equivalent request data (processed from form):
$site_data = array(
'Site' => array('field1' => 'value1', 'field2' => 'value2' ),
'SiteUser' => array(
array('name' => 'Jane Doe'),
array('name' => 'John Doe'),
array('name' => 'Moe Money')
)
);
In the controller:
unset($this->Site->SiteUser->validate['id']);
$saved_site = $this->Site->saveAssociated($site_data);
Results:
All of the Site data gets saved as expected. Only the last SiteUser element (Moe Money in the example) is saved. This is the same regardless of the number of elements in SiteUser, i.e., only the last element gets saved.
SQL Log:
It performs an
INSERT INTO site_users (`name`, `id`) VALUES ('Jane Doe', 1)
but then executes
UPDATE site_users SET 'name' = 'John Doe', 'id' = 1 WHERE site_users = 1
UPDATE site_users SET 'name' = 'Moe Money', 'id' = 1 WHERE site_users = 1
This obviously leaves the very last element as the one to get saved, the others are over-written by updates.
Thanks for any pointers in advance.

You better stick to the conventions, id as the foreign key? No, really, don't do that!
In any case you must tell your SiteUser model about the primary key column name in case it doesn't follow the conventions. See Cookbook > Models > Model Attributes > primaryKey
class SiteUser extends AppModel {
public $primarykey = 'rowid';
// ...
}
And while setting this to rowid will most likely fix the problem, I'd again advise to stick to the naming conventions instead!

Related

Retrieving the associated model in a hasMany through association without recursive

I have set up a hasMany through association between two tables - Books and Authors. The associations work properly, except when attempting to retrieve all the authors that belongs to a book. If I do this
$book = $this->Book->findById($id)
The array returned will not have an array Authors; it will have the AuthorToBook join model information, but it won't automatically fetch the Author associated with it.
I have to set recursive to 2 in order to retrieve the join model, and the associated Author. I am also re-fetching the Book data for each AuthorToBook association there is.
'AuthorToBook' => array(
(int) 0 => array(
'id' => '1',
'author_id' => '1',
'book_id' => '2',
'Author' => array(
'id' => '1',
'name' => 'J.R.R Tolkien',
'date_of_birth' => '1892-01-03 00:00:00',
'bio' => 'Professor and inventor of the Elvish Language'
),
'Book' => array(
'id' => '2',
'title' => 'Return of the King',
'pagecount' => '1200',
'publisher_id' => '1'
)
)
)
Now the question is - how can I fetch the associated model formed with a hasMany Through relationship without setting the recursive parameter?
Here's the source
<?php
class Author extends AppModel
{
public $hasMany = array('AuthorToBook');
}
?>
<?php
class AuthorToBook extends AppModel
{
public $useTable = 'authors_to_books';
public $belongsTo = array('Author', 'Book');
}
?>
<?php
class Book extends AppModel {
public $hasMany = array('AuthorToBook');
}
?>
Containable Behavior is your friend and is THE replacement for recursive. It allows you to specify exactly which associated model(s) you want to retrieve with each query.
Recursive is a cool thing, but it's pretty much agreed that you shouldn't actually use it beyond doing the intro tutorial for CakePHP. Set public $recursive = -1; in your AppModel (which makes it off by default), and never look back.

CakePHP Access a different table my view

I have two tables Contact and Quote, this is a one to many relationship (i.e. one contact can have many quotes). Foreign keys are all setup correctly.
When I go to create a new quote I want to be able to select from a drop down list of contacts.
My code looks like this:
Contact Model:
class Contact extends AppModel {
public $hasMany = array('Quote' => array('className' => 'Quote', 'foreignKey' => 'contact_id'));
}
Quote Model
class Quote extends AppModel {
public $belongsTo = array('Contact' => array('className' => 'Contact', 'foreignKey' => 'contact_id'));
public $validate = array(
'name' => array(
'rule' => 'notEmpty'
),
'amount' => array(
'rule' => 'notEmpty'
)
);
}
Add method in QuotesController:
public function add() {
// TODO: Update this so the user can select the id from a drop down list.
$this->request->data['Quote']['contact_id'] = '1';
if ($this->request->is('post')) {
$this->Quote->create(); // This line writes the details to the database.
if ($this->Quote->save($this->request->data)) {
$this->Session->setFlash('Your quote has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your quote.');
}
}
}
As you can see I'm currently just hard coding the user id as part of the add process.
I'm assuming you have read and are using this method in your view.
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
Less easy to find (or easier to overlook) is this:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list
So in your controller you want to do
$contacts = $this->Article->find('list', array('fields' => array('Contact.id', 'Contact.name'));
$this->set(compact('contacts'));
Then in the view:
echo $this->Form->select('contact_id', $contacts);
Modify the fields for the find to reflect what is actually in your model. And if you need fields combined, you can possibly do it with virtual fields: http://book.cakephp.org/2.0/en/models/virtual-fields.html. Otherwise you can select the fields that need to be composited and use a foreach loop to combine them into a id=>[displayed value] array to pass on to the view. Only the id is the important thing and has to correspond to an id in the Contacts table.

cakephp saveAll associated model field is not being saved

I'm currently using cakephp 2.2.3.
I have the following Model Associations:
VehicleModel -> Vehicle -> Order
Plan -> Order
Vehicle HABTM Tag
Inside the Vehicle controller, add action, I have:
if(!empty($this->request->data)) {
if($this->Vehicle->saveAll($this->request->data)) {
$this->Session->setFlash('Vehicle was successfully added.');
}
}
The $this->request->data array is formatted like this:
array(
'VehicleModel' => array(
'category_id' => '2',
'make_id' => '1'
),
'Order' => array(
'plan_id' => '2'
),
'Vehicle' => array(
'vehicle_model_id' => '13',
'price' => ' 8700',
'year' => '1994',
'km' => '100',
'color' => '61',
'fuel' => '1',
'gear' => '20',
'type' => '51',
'city' => 'Rio de Janeiro',
'state' => 'RJ'
),
'Tag' => array(
'Tag' => array(
(int) 0 => '69',
(int) 1 => '11'
)
)
)
The orders table has the following fields:
id , plan_id , vehicle_id , created , modified.
Vehicle Model:
class Vehicle extends AppModel {
public $belongsTo = array('User' , 'VehicleModel');
public $hasMany = array('Order' , 'Image');
public $hasAndBelongsToMany = array('Accessory' , 'Tag');
}
Order Model:
class Order extends AppModel {
public $belongsTo = array('Vehicle' , 'Part' , 'Plan');
public $validate = array(
'plan_id' => array(
'rule' => 'notEmpty'
)
);
}
The problem I'm having is that the Order.plan_id field is not being saved, although all other fields are being saved normally. What can I be doing wrong?
Just to be clear, When I do the multiple saving manually, everything
works just fine. I mean, when I write:
$this->Vehicle->save()
and then set
$this->request->data['Order']['vehicle_id'] = $this->Vehicle->id
and finally
$this->Vehicle->Order->save()
everything works just fine. It's the saveAll that is causing me
trouble.
If that is the case, see where $this->request->data['Order']['vehicle_id'] = $this->Vehicle->id. Comparing this to your var dump above, order never contains the relation to the main model, which leads me to ask if this is a new record you are trying to save or an update? I think you might have to not go with a saveAll here if you are setting a new record because the main id is not yet set. Please see:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto
Particularly: "If neither of the associated model records exists in the system yet (for example, you want to save a new User and their related Profile records at the same time), you’ll need to first save the primary, or parent model."
They basically do the long version you are doing.

HABTM selection seemingly ignores joinTable

UPDATE #2 -- SOLUTION FOUND:
Turns out my use of this lookup:
$this->User->Group->find(....)
was not what I needed. To pull out a user's groups I needed to use:
$this->User->find('all',array('conditions' => array('User.id' => $user_id)));
< /UPDATE #2>< PROBLEM>
I'm attempting to do a HABTM relationship between a Users table and Groups table. The problem is, that I when I issue this call:
$this->User->Group->find('list');
The query that is issued is:
SELECT [Group].[id] AS [Group__id], [Group].[name] AS [Group__name] FROM [groups] AS [Group] WHERE 1 = 1
I can only assume at this point that I have defined my relationship wrong as I would expect behavior to use the groups_users table that is defined on the database as per convention. My relationships:
class User extends AppModel {
var $name = 'User';
//...snip...
var $hasAndBelongsToMany = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'user_id',
'associationForeignKey' => 'group_id',
'joinTable' => 'groups_users',
'unique' => true,
)
);
//...snip...
}
class Group extends AppModel {
var $name = 'Group';
var $hasAndBelongsToMany = array ( 'User' => array(
'className' => 'User',
'foreignKey' => 'group_id',
'associationForeignKey' => 'user_id',
'joinTable' => 'groups_users',
'unique' => true,
));
}
Is my understanding of HABTM wrong? How would I implement this Many to Many relationship where I can use CakePHP to query the groups_users table such that a list of groups the currently authenticated user is associated with is returned?
UPDATE
After applying the change suggested by ndm I still receive a large array return (Too big to post) which returns all groups and then a 'User' element if the user has membership to that group. I looked at the query CakePHP uses again:
SELECT
[User].[id] AS [User__id],
[User].[username] AS [User__username],
[User].[password] AS [User__password],
[User].[email] AS [User__email], CONVERT(VARCHAR(20),
[User].[created], 20) AS [User__created], CONVERT(VARCHAR(20),
[User].[modified], 20) AS [User__modified],
[User].[full_name] AS [User__full_name],
[User].[site] AS [User__site],
[GroupsUser].[user_id] AS [GroupsUser__user_id],
[GroupsUser].[group_id] AS [GroupsUser__group_id],
[GroupsUser].[id] AS [GroupsUser__id]
FROM
[users] AS [User] JOIN
[groups_users] AS [GroupsUser] ON (
[GroupsUser].[group_id] IN (1, 2, 3, 4, 5) AND
[GroupsUser].[user_id] = [User].[id]
)
Is there an easy way to refine that such that I only receive the group ids & names for the entries I have membership to? I was thinking of using:
array('conditions'=>array('GroupsUser.user_id'=>$user_id))
...but I receive an sql error on the groups table:
SELECT TOP 1 [Group].[name] AS [Group__name], CONVERT(VARCHAR(20), [Group].[created], 20) AS [Group__created], CONVERT(VARCHAR(20), [Group].[modified], 20) AS [Group__modified], [Group].[id] AS [Group__id] FROM [groups] AS [Group] WHERE [GroupsUser].[user_id] = 36 ORDER BY (SELECT NULL)
I think you just misunderstood what the list find type is ment to do.
The query is totally fine, the list find type is used for retreiving a list of records of a single model only, where the models primary key is used as index, and the display field as value.
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list

Self "HABTM" or "HasMany Through" concept confusion

Bounty:
+500 rep bounty to a GOOD solution. I've seriously banged my head against this wall for 2 weeks now, and am ready for help.
Tables/Models (simplified to show associations)
nodes
id
name
node_type_id
node_associations
id
node_id
other_node_id
node_types
id
name
General Idea:
A user can create node types (example "TV Stations", "TV Shows", and "Actors"...anything). If I knew ahead of time what the node types were and the associations between each, I'd just make models for them - but I want this to be very open-ended so the user can create any node-types they want. Then, each node (of a specific node-type) can relate to any other node of any other node-type.
Description and what I've tried:
Every node should be able to be related to any/every other node.
My assumption is that to do that, I must have an association table - so I made one called "node_associations" which has node_id and other_node_id.
Then I set up my association (using hasMany through I believe):
(below is my best recollection of my set-up... it might be slightly off)
//Node model
public $hasMany = array(
'Node' => array(
'className' => 'NodeAssociation',
'foreignKey' => 'node_id'
),
'OtherNode' => array(
'className' => 'NodeAssociation',
'foreignKey' => 'other_node_id'
)
);
//NodeAssociation model
public $belongsTo = array(
'Node' => array(
'className' => 'Node',
'foreignKey' => 'node_id'
),
'OtherNode' => array(
'className' => 'Node',
'foreignKey' => 'other_node_id'
)
);
At first, I thought I had it - that this made sense. But then I started trying to retrieve the data, and have been banging my head against the wall for the past two weeks.
Example Problem(s):
Lets say I have a the following nodes:
NBC
ER
George Clooney
Anthony Edwards
Tonight Show: Leno
Jay Leno
Fox
Family Guy
How can I set up my data structure to be able to pull the all TV Stations, and contain their TV Shows, which contain their Actors (as example)? This would be SIMPLE with normal model setup:
$this->TvStation->find('all', array(
'contain' => array(
'TvShow' => array(
'Actor'
)
)
));
And then, maybe I want to retrieve all male Actors and contain the TV Show which contain the TV Station. Or TV Shows that start at 9pm, and contain it's actor(s) and it's station...etc etc.
But - with HABTM or HasMany Through self (and more importantly, and unknown data set), I wouldn't know which field (node_id or other_node_id) the model is, and overall just can't wrap my head around how I'd get the content.
The Idea
Let's try to solve this with convention, node_id will be the model who's alias comes alphabetically first and other_node_id will be the one that comes second.
For each contained model, we create a HABTM association on-the-fly to Node class, creating an alias for each association (see bindNodes and bindNode method).
Each table we query we add an extra condition on node_type_id to only return results for that type of node. The id of NodeType is selected via getNodeTypeId() and should be cached.
For filtering results using condition in deeply related associations, you would need to manually add extra join, creating a join for each jointable with a unique alias and then joining each node type itself with an alias to be able to apply the conditions (ex. selecting all TvChannels that have Actor x). Create a helper method for this in Node class.
Notes
I used foreignKey for node_id and associationForeignKey for other_node_id for my demo.
Node (incomplete)
<?php
/**
* #property Model NodeType
*/
class Node extends AppModel {
public $useTable = 'nodes';
public $belongsTo = [
'NodeType',
];
public function findNodes($type = 'first', $query = []) {
$node = ClassRegistry::init(['class' => 'Node', 'alias' => $query['node']]);
return $node->find($type, $query);
}
// TODO: cache this
public function nodeTypeId($name = null) {
if ($name === null) {
$name = $this->alias;
}
return $this->NodeType->field('id', ['name' => $name]);
}
public function find($type = 'first', $query = []) {
$query = array_merge_recursive($query, ['conditions' => ["{$this->alias}.node_type_id" => $this->nodeTypeId()]]);
if (!empty($query['contain'])) {
$query['contain'] = $this->bindNodes($query['contain']);
}
return parent::find($type, $query);
}
// could be done better
public function bindNodes($contain) {
$parsed = [];
foreach($contain as $assoc => $deeperAssoc) {
if (is_numeric($assoc)) {
$assoc = $deeperAssoc;
$deeperAssoc = [];
}
if (in_array($assoc, ['conditions', 'order', 'offset', 'limit', 'fields'])) {
continue;
}
$parsed[$assoc] = array_merge_recursive($deeperAssoc, [
'conditions' => [
"{$assoc}.node_type_id" => $this->nodeTypeId($assoc),
],
]);
$this->bindNode($assoc);
if (!empty($deeperAssoc)) {
$parsed[$assoc] = array_merge($parsed[$assoc], $this->{$assoc}->bindNodes($deeperAssoc));
foreach($parsed[$assoc] as $k => $v) {
if (is_numeric($k)) {
unset($parsed[$assoc][$k]);
}
}
}
}
return $parsed;
}
public function bindNode($alias) {
$models = [$this->alias, $alias];
sort($models);
$this->bindModel(array(
'hasAndBelongsToMany' => array(
$alias => array(
'className' => 'Node',
'foreignKey' => ($models[0] === $this->alias) ? 'foreignKey' : 'associationForeignKey',
'associationForeignKey' => ($models[0] === $alias) ? 'foreignKey' : 'associationForeignKey',
'joinTable' => 'node_associations',
)
)
), false);
}
}
Example
$results = $this->Node->findNodes('all', [
'node' => 'TvStation', // the top-level node to fetch
'contain' => [ // all child associated nodes to fetch
'TvShow' => [
'Actor',
]
],
]);
I think you have incorrect relations between your models. I guess it will be enough with:
// Node Model
public $hasAdBelongsToMany = array(
'AssociatedNode' => array(
'className' => 'Node',
'foreignKey' => 'node_id'
'associationForeignKey' => 'associated_node_id',
'joinTable' => 'nodes_nodes'
)
);
// Tables
nodes
id
name
node_type_id
nodes_nodes
id
node_id
associated_node_id
node_types
id
name
Then you can try using ContainableBehavior to fetch your data. For Example, to find all TVShows belonging to a TVStation:
$options = array(
'contain' => array(
'AssociatedNode' => array(
'conditions' => array(
'AssociatedNode.node_type_id' => $id_of_tvshows_type
)
)
),
conditions => array(
'node_type_id' => $id_of_tvstations_type
)
);
$nodes = $this->Node->find('all', $options);
EDIT :
You can even have second level conditions (see last example on this section, look at the 'Tag' model conditions). Try this:
$options = array(
'contain' => array(
'AssociatedNode' => array(
'conditions' => array(
'AssociatedNode.node_type_id' => $id_of_tvshows_type
),
'AssociatedNode' => array(
'conditions' => array( 'AssociatedNode.type_id' => $id_of_actors_type)
)
)
),
conditions => array(
'node_type_id' => $id_of_tvstations_type
)
);
$nodes = $this->Node->find('all', $options);
I think unfortunately part of the problem is that you want your solution to contain user data in the code. Since all your nodes types are user data, you want to avoid trying to use those as the classes methods in your application, as there could be infinite of them. Instead I would try and create methods that model the data operations you want to have.
One omission I see in the provided data model is a way to record the relationships between types. In your example you mention a relationship between TvStation -> TvShows -> Actor etc. But where are these data relationships defined/stored? With all of your node types being user defined data, I think you'll want to/need to record store those relationships somewhere. It seems like node_types needs some additional meta data about what the valid or desired child types for a given type are. Having this recorded somewhere might make your situation a bit simpler when creating queries. It might help to think of all the questions or queries you're going to ask the database. If you cannot answer all those questions with data that is in the database, then you are probably missing some tables. Model associations are just a proxy for data relations that already exist in your tables. If there are gaps there are probably gaps in your data model.
I don't think this is the answer you're looking for but hopefully it helps you find the right one.
Why don't you create a method in the node model?
Something like:
<?php
// first argument is a nested array filled with integers
(corresponding to node_type_id)
//second one id of a node
//third one corresponds to the data you want(empty at beginning in most case)
public function custom_find($conditions,$id,&$array){
//there may several type of nodes wanted: for instances actors and director of a serie, so we loop
foreach($conditions as $key_condition=>$condition){
//test to know if we have reached the 'bottom' of the nested array: if yes it will be an integer '2', if no it will be an array like '2'=>array(...)
if(is_array($condition))){
//this is the case where there is deeper levels remaining
//a find request: we ask for the node defined by its id,
//and the child nodes constrained by their type: ex: all actors of "Breaking Bad"
$this->id=$id;
$result=$this->find('all',array(
'contain' => array(
'OtherNode' => array(
'conditions'=>array('node_type_id'=>$key_condition)
)
)
)
);
//we add to $array the nodes found. Ex: we add all the actors of the serie, with type_id as key
$array[$key_condition]=$result['OtherNode'];
//Then on each node we just defined we call the function recursively. Note it's $condition not $conditions
foreach($array[$key_condition] as &$value){
$this->custom_find($condition,$value['Node']['id'],$value);
}
}else{
//if we simply add data
$this->id=$id;
$result=$this->find('all',array(
'contain' => array(
'OtherNode' => array(
'conditions'=>array('node_type_id'=>$value)
)
)
)
);
$array[$condition]=$result['OtherNode'];
}
}
}
That code is almost certainly wrong, it's just to give you an idea of what I mean.
Edit:
What it does:
it's a recursive function that takes a nested array of conditions and the id of a node and gives back nested array of nodes.
For instance: $conditions=array('2','4'=>array('5','6'=>array('4')))
How it works:
For a single node it gives back all the child nodes corresponding to the condition in the array: then it does the same for the children with the conditions one level deeper, until there is no more levels left.

Resources