I have two tables, say:
TAB1
----
id
tab1_md5
TAB2
----
id
tab2_md5
I would like to create a hasMany relation without foreignKey to be able to use cakephp recursive stuff but don't know how to create the relationship.
I've tried with:
var $hasMany = array(
'Tab2' => array(
'className' => 'Tab2',
'foreignKey' => false))
but i don't know what i should specify in condition
EDIT: the relation is tab1.tab1_md5=tab2.tab2_md5
I don't believe you can do a hasMany relationship without using a foreign key. CakePHP, at that point, has no idea how these tables should be related. Without knowing the relationship it can't do any joins to include the associated table data.
It's not possible. Cake must do a separate query to fetch hasMany data. In that separate query it only uses the primary key of the related model. AFAIK there's currently no way to make it use anything but the primary key. So you'll have to do these queries manually:
$tab1s = $this->Tab1->find('all', array(...));
$tab2s = $this->Tab2->find('all', array(
'conditions' => array('Tab2.tab2_md5' => Set::extract('/Tab1/tab1_md5', $tab1s))
));
$grouped = array();
foreach ($tab2s as $tab2) {
$grouped[$tab2['Tab2']['tab2_md5']][] = $tab2;
}
foreach ($tab1s as &$tab1) {
$tab1['Tab2'] = isset($grouped[$tab1['Tab1']['tab1_md5']]) ? $grouped[$tab1['Tab1']['tab1_md5']] : array();
}
Something along these lines. You could do this automatically in an afterFind callback in the model itself to get the Cake automagic effect.
Related
I am using CakePHP 2.4 and I need to have a model called Files to store file information for Users, Cases, and Trucks. Now a file can be associated with many other models (Users, Cases, Trucks, etc). In other words I want Users to have Files, Cases to have Files, and Trucks to have Files.
Do I need to create UserFiles model, CaseFiles model, and TruckFiles or can I have a table entry that flags in Files that it is a User record, Case record, or Truck record? I also want to do the same for Notes and Tasks, so there would either be UserNotes, CaseNotes, and TruckNotes or can I have Notes (id, foreign_id, associatedModel(would be Truck, User, or Case), etc..). So that when I create my Users, I have Files that tie to my User.id and have a condition that the associatedModel is User.
I think I will need to create the UserFiles, UserNotes, and UserTasks. But if there is a good design and better way to do this I am open to suggestions.
CakePHP already has a Class named File. See: http://api.cakephp.org/2.4/class-File.html
For this its better to use other name, for example Attach.
User hasMany Attach
Case hasMany Attach
Truck hasMany Attach
Instead of create three foreign keys on 'attaches' table (like user_id, case_id, and truck_id), in this case i prefer create a model_name and model_id fields as foreign key.
schema table attaches:
- id
- model_name
- model_id
- ...
model Attach:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'model_id',
'conditions' => array('Attach.model_name' => 'User'),
),
'Case' => array(
'className' => 'Case',
'foreignKey' => 'model_id',
'conditions' => array('Attach.model_name' => 'Case'),
),
'Truck' => array(
'className' => 'Truck',
'foreignKey' => 'model_id',
'conditions' => array('Attach.model_name' => 'Truck'),
),
);
model User, Case, Truck:
public $hasMany = array(
'Attach' => array(
'className' => 'Attach',
'foreignKey' => 'model_id',
'dependent' => true,
'conditions' => array('Attach.model_name' => 'User'), // Change the condition on Case and Truck model
),
);
You would need to create these models
User
File
Case
Truck
Then, you define model associations as you need.
For example: User hasMany File, Case hasMany File, Truck hasMany File, File belongsTo User, Case and Truck
One way to think about it is if the relationship is one to many (hasMany), many to one (belongsTo), one to one (hasOne), or many to many (hasAndBelongsToMany). With the first three mentioned, you can store the foreign key in one of the models because at least one of the models being associated is associated with only one entry in the other table. In the many to many relationship, you need a join table to store the pairs of foreign keys representing the associations.
In this case, if you're talking about a file that can belong to many users, and users can have many files, then that is a many to many or hasAndBelongsToMany relationship, and then yes, you need a UsersFile join table. If however, a file can only belong to one user, or a user can only have one file, then you're talking about one of the other relationships, and you do not need the join table. The same can be said for files/cases and files/trucks.
For now I am using following code to get books from certain category:
$options['conditions']['Category.id'] = $category_id;
$options['joins'] = array(
array(
'table' => 'books_categories',
'alias' => 'BookCategory',
'type' => 'inner',
'conditions' => array('Book.id = BookCategory.id_book'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'inner',
'conditions' => array('BookCategory.kat = Category.id'),
),
);
$this->Book->find('all',$options);
This just finds all books from given category_id.
So there are 3 tables: categories,books and books_categories. books_categories has two fileds: book_id and category_id so basicly its just HABTM relation. The problem is that one book may belong to many categories and I want for example find all books from category 5 but excluding books from categories 5,6 and 7. How I can do this?
edit -------------------------------------------------------------------------------
Ok so I figured out how it should look in pure SQL - the conditions should be like this:
where
category_id = <given category>
and books.book_id not in
(
select book_id from book_categories
where category_id in (<given set of cat>)
)
order by books.inserted
this will get all books from one category but excluding books from a set of other categories.
Now I want to force Cake to generate similar SQL query.
I tried so far:
$options['conditions']['Category.id'] = $category_id;
$options['conditions']['AND'][] = 'Book.id NOT IN (SELECT id_book FROM book_categories WHERE kat IN (133,134))';
$options['order'] = array('Book.inserted' => 'desc');
$options['joins'] = array(
array(
'table' => 'book_categories',
'alias' => 'BookCategory',
'type' => 'inner',
'conditions' => array('Book.id = BookCategory.id_book'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'inner',
'conditions' => array('BookCategory.kat = Category.id'),
),
);
This generates this query (sory - table names are little bit different):
SELECT `Book`.`id`, `Book`.`OK`, `Book`.`price`, `Book`.`link`, `Book`.`title`,
`Book`.`author`, `Book`.`img`, `Book`.`inserted`, `Book`.`checked`, `Book`.`big_img`, `Book`.`lang`, `Book`.`asin`, `Book`.`description`, `Book`.`last_update`, `Book`.`review`, `Book`.`changed`
FROM `amazon`.`linki` AS `Book`
inner JOIN `amazon`.`cross_kategorie_full` AS `BookCategory` ON (`Book`.`id` = `BookCategory`.`id_book`)
inner JOIN `amazon`.`kategorie` AS `Category` ON (`BookCategory`.`kat` = `Category`.`id`)
WHERE `Category`.`id` = 4
AND `Book`.`OK` = 'OK'
AND ((`Book`.`big_img` NOT LIKE '%no-image%')
AND (`Book`.`id` NOT IN (SELECT id_book FROM cross_kategorie_full WHERE kat IN (133,134))))
ORDER BY `Book`.`inserted` desc LIMIT 20
But there is error: Maximum execution time of 30 seconds exceeded - so There is something that doesnt end (loop?) in this sql statement...
Update relative to updated question
For the sql to yield correct results (in an acceptable time) you'll need to join with Categories again giving it another alias. Since this leads to another question, I suggest you post it tagged with mysql and query-optimization.
End update
As it is, a HABTM relationship is a bit devious (since it really isn't a HABTM). If you have only one row per book-category match in books_categories you can't know to what other categories a certain book belongs to, so you can't really tell which ones you really want (i.e. don't belong in those other categories). It's CakePHP's data layer and models that solve this problem for you behind the scenes :)
The only solution I see is to use Set::extract to further query the results that you get and filter out Books that belong to Categories that you didn't want to include. (something like:
// returns all books not belonging to categories 3,4
$new_result = Set::extract('/Books/Category[id!=3][!=4]', $results);
On a side note, I find it very useful in cases like this, to query the DB and visualize the complexity of the SQL query that gets you the required results. Also, you should activate the CakePHP debug toolbar to see the SQL queries that are sent to the DB so you have a better idea of what's going on behind the scenes.
The CakePHP book, advises the following at the end of "Associations: Linking models together" Section (emphasis mine).
Using joins allows you to have a maximum flexibility in how CakePHP handles associations and fetch the data, however in most cases you can use other tools to achieve the same results such as correctly defining associations, binding models on the fly and using the Containable behavior. This feature should be used with care because it could lead, in a few cases, into bad formed SQL queries if combined with any of the former techniques described for associating models.
I think there is a typo in there. You want to get all books from category 5 but not from category 5, 6 and 7?
Nevermind. Cake convensions state out that there should be always a primary key within the HABTM table, so you may add a "id" column. With this the next steps are much easier or should I rather say: "They are getting possible".
What you do next is, you create an association model called "BooksCategory".
Use the 'with' index explained here to link your Book and Category models with each other over that new model. Don't forget to use plugin syntax (PluginName.ModelName) in case the linking model belongs to a plugin.
You are now putting two belongsTo associations in the linking model for each of the models you are linking. This makes sure to fetch those while finding.
Next thing you do is:
$this->Book->BooksCategory->find(
'all',
array(
'conditions' => array(
'BooksCategory.category_id' => 5
)
)
);
Finished ;) No you get only the books from category 5.
Greetings
func0der
I was hoping to create and save the associated data like what I did with belongsTo data - the associated data are created on the fly and foreign ID is also generated and saved on the fly by a single call to saveAll or saveAssocated which has transaction built-in.
But this seems not the case for data in hasMany relationship. Take User and Comment as example. Comment has user_id as the foreign key.
It seems that I cannot save User $data by using single saveAll($data) on User
Array(
'name' => 'Jack',
'email' => 'jack#abc.com',
'Comment' => array(
array(
'title' => 'I like this article.'
)
)
)
I read some docs. It seems that I need to mention the user_id as the foreign key for the Comment data to create correctly.
If that's the case, since I don't have user ID until it's created, it seems that I need to code to let SAVE happen twice.
I really think that I am missing something. There should be a CAKE way for doing this.
This is done automatically by Cake as long as you follow the conventions and format the data accordingly. For hasMany associations, the main model data, and the associated model data, need to be set on string keys on the same level, like
array
(
'User' => array(),
'Comment' => array()
)
Also note that
The saveAll function is just a wrapper around the saveMany and
saveAssociated methods. it will inspect the data and determine what
type of save it should perform. If data is formatted in a numerical
indexed array, saveMany will be called, otherwise saveAssociated is
used.
This function receives the same options as the former two, and is
generally a backwards compatible function. It is recommended using
either saveMany or saveAssociated depending on the case.
So either will do.
Long story short, the model data needs to be separated and indexed by string keys, it's essentially the same format as a find() call would return it. That way Cake will know that it needs to save associated data, and will automatically insert the foreign key for the Comment records.
array
(
'User' => array
(
'name' => 'Jack',
'email' => 'jack#abc.com'
),
'Comment' => array
(
array
(
'title' => 'I like this article.'
)
)
)
See also
Cookook > Models > Saving Your Data > Associations: Linking Models Together > hasMany
Cookook > Models > Saving Your Data > Model::saveMany()
Cookook > Models > Saving Your Data > Model::saveAssociated()
Cookook > Models > Saving Your Data > Model::saveAll()
I have a nodes table (Node model). I'd like it to be associated to different data types, but only if one if it's field is set to 1.
Example:
My nodes table has a data_article field (tinyint 1). I only want the Node to $hasMany Article IF that field is a 1.
I tried this:
public $hasMany = array(
'Article' => array(
'conditions' => array('Node.data_articles' => '1')
),
);
But I get an error:
Column not found: 1054 Unknown column 'Node.data_articles' in 'where
clause'
Because the association is doing the Article find in it's own query:
SELECT `Article`.`id`, `Article`.`title`, `Article`.`node_id`, ...more fields...
FROM `mydatabase`.`articles` AS `Article`
WHERE `Node`.`data_artiles` = '1'
AND `Article`.`node_id` = ('501991c2-ae30-404a-ae03-2ca44314735d')
Obviously that doesn't work, since the Node table isn't being Joined at all in this query.
TLDR:
Is it possible to have associations or not based on a field in the main model? If not, how else can I keep different data types in multiple tables, and not have to query them all every time?
I don't think that there's a standard way to do this with CakePHP (at least I can't imagine a way). What definitely is possible would be binding associated models dynamically.
So you might query your model without associations by passing the recursive parameter as -1 to the find() method and based on the result unbind the associated models dynamically. Afterwards you would have to query again, for sure. You might build a behavior out of this to make it reusable.
A quite elegant solution would be possible, if Cake could make a two-step query with a callback after the main model was queried, but before associated models are queried, but this isn't possible at the moment.
Edit: There might be a non-Cake way to archieve this more performantly with a custom Query including IF-statements, but I'm not the SQL expert here.
You should do it from the other side:
public $belongsTo = array(
'Node' => array(
'conditions' => array('Node.data_articles' => '1')
),
);
I've got my models set up so that Users belong to Groups. There is a working HABTM relationship between these two.
The code below is used to populate the database with some demo data. Prior to the following snippet, the Groups table has been filled with some sample group data.
The code snippet below works. It adds records to the Users table and the join table (groups_users) gets the correct entries, too. So basically, the HABTM relationship works fine.
Here is my question: How do I add associations with multiple groups instead of just the single relationship? Doing a 'Group'=>array($group1, $group2) instead of the 'Group'=>$group1' as outlined below does NOT work. When I use an array, nothing at all is added to the join table.
Your help is greatly appreciated!
$groups = $this->Group->find('all');
for ($i=0; $i<=200; $i++) {
$groupIndex1 = rand(0, count($groups)-1);
$groupIndex2 = rand(0, count($groups)-1);
$this->User->set(
array(
'id'=>null,
'name'=>'Dummy User '.$i0,
'Group'=>$groups[$groupIndex1]
)
);
$this->User->save();
To save multiple groups for a single user, you will have to define hasMany relationship in your User model. And in your saving code it will looks like:
$groups = $this->Group->find('all');
for ($i=0; $i<=200; $i++) {
$groupIndex1 = rand(0, count($groups)-1);
$groupIndex2 = rand(0, count($groups)-1);
$data =
array('User' => array(
'id'=>null,
'name'=>'Dummy User '.$i0,
),
'Group'=> array($groups[$groupIndex1], $groups[$groupIndex2])
)
);
$this->User->saveAssociated($data, array('deep' => true);
This link might help you to understand 'deep' => true option.