CakePHP recursive not working with conditions - php

I have a problem with querying associated data from a Model in CakePHP. I wrote an example to show the behavior:
TestController.php:
class TestController extends AppController
{
public $uses = array(
'User',
'Upload',
'Detail'
);
public function test(){
$result = $this->Upload->find('all', array(
'recursive' => 2,
'conditions' => array('Detail.id' => 1)
));
print_r($result);
}
}
Upload.php:
class Upload extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
Detail.php:
class Detail extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
User.php:
class User extends AppModel {
public $hasOne = 'Detail';
public $hasMany = array(
'Upload' => array(
'className' => 'Upload',
'foreignKey' => 'user_id',
)
);
}
When I remove the condition I get back an array with Details included. But with the condition I get the following error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Detail.id' in 'where clause'
Looking at the SQL Queries it seems like he is not joining the tables correctly when I add the condition. Without the condition he is joining all three tables.
Is this a bug in CakePHP or am I doing anything wrong?

No, it is not a bug with CakePHP. It's simply the way it's designed, using conditions during a find on associated models will often create an invalid query. You should be using containable behavior or manually joining to use conditions on associated models.
Also, I suspect that you will not get the results you are looking for doing this way anyways. CakePHP by default uses left joins. Therefore, your results will not be limited by those associated with the desired Detail ID, but rather, it will get all uploads, all users associated with those uploads, and then only those details associated with those users that have the correct ID. The simplest way then to get what you're probably looking for is to do the query from the opposite direction:
$result = $this->Detail->find('all', array(
'recursive' => 2,
'conditions' => array('Detail.id' => 1)
));
EDIT: If you do want to do left joins, then make your query this way:
$result = $this->Upload->find('all', array(
'contain' => array('User' => array('Detail' => array('conditions' => array('Detail.id' => 1))),
));

Related

How To Save Data in CakePHP 2 for 3 tree like connected tables

I have 3 tree like connected tables. Their schemas as follows:
Member{
//Some column
}
Transactions{
member_id :: foreign key of member table
//Some other column
}
TransactionItems{
transaction_id :: foreign key of Transaction table
//Some other column
}
I define models like this:
class Members extends AppModel {
public $primaryKey = 'id';
public $hasOne = array(
'Transactions' => array(
'className' => 'Transactions',
'foreignKey' => 'member_id',
'dependent' => true
)
);
}
class Transactions extends AppModel {
public $primaryKey = 'id';
public $belongTo = array('Members');
public $hasOne = array(
'TransactionItems' => array(
'className' => 'TransactionItems',
'foreignKey' => 'transaction_id',
'dependent' => true
)
);
}
class TransactionItems extends AppModel {
public $primaryKey = 'id';
public $belongTo = array('Transactions');
public $belongsTo = array(
'Transactions' => array(
'className' => 'Transactions',
'foreignKey' => 'transaction_id'
)
);
}
I have a Data Array which I want to save into database. My scheme is:
Array(
[Members] = [],//Array
[Transactions] = [],//Array
[TransactionItems] = []//Array
)
The problem is that whenever I run $this->Members->saveAll($data). It save data in Member and Transactions table. But do not create data in TransactionItems table. I want to save in all 3 tables at a time.
Any help would be grateful.
Second level (and above) associations must be nested, ie the data structure needs to be:
array(
'Members' => array(),
'Transactions' => array(
'TransactionItems' => array()
)
)
A bit awkward, but that's how it works in 2.x. You can always refer to the structure that is being returned when reading data, it needs to be the same when saving it.
Furthermore you must set the deep option to true in order to be able to save second level and above associations (by default only first level associations are being saved):
$this->Members->saveAll($data, array('deep' => true));
See also
Cookbook > Models > Saving Your Data > Model::saveAssociated()

CakePHP return hasOne relationships within main result array

I have a model class in CakePHP defined like this:
class Programme extends AppModel {
public $hasOne = array(
'ProgrammeLikes' => array(
'className' => 'ProgrammeLikes',
'fields' => array('likes'));
}
When retrieving my models from the database they are returned as an array with an array keyed to 'Programme' and a separate array keyed to 'ProgrammeLikes' (which contains the 'likes' value correctly). In order to reduce the changes necessary to existing code I want the 'likes' value to be within the 'Programme' array.
Is this possible?
Thanks in advance
Use virtualFields here to get this thing to be done.
class Programme extends AppModel {
public $hasOne = array(
'ProgrammeLikes' => array(
'className' => 'ProgrammeLikes',
'fields' => array('likes')
);
public $virtualFields = array(
'likes' => 'SELECT likes FROM programme_likes AS ProgrammeLikes WHERE ProgrammeLikes.id = Programme.programme_likes_id'
);
// Where programme_likes_id is the foriegnkey for Programme model
}
Note: I assumed programme_likes is your table name for ProgrammeLikes Model and programme_likes_id is the foriegnkey for
Programme Model, so you can arrange the query in your own way that suits your requirement.

CakePHP - Correct way to do hasMany through

I have 3 models
Categories:
class Category extends AppModel {
public $belongsTo = array(
'Parent' => array(
'className' => 'Category',
'foreignKey' => 'parent_id'
),
);
public $hasMany = array(
'Children' => array(
'className' => 'Category',
'foreignKey' => 'parent_id'
),
'UserCategoryMeta'
);
}
Users:
class User extends AppModel {
public $hasMany = array(
'UserCategoryMeta' => array(
'className' => 'UserCategoryMeta',
'foreignKey' => 'user_id',
),
);
}
UserCategoryMeta:
class UserCategoryMeta extends AppModel
{
public $belongsTo = array(
'User', 'Category'
);
}
What I need to do is have each user be able to choose many categories and for each of those associations I need the user to set search terms which is just 1 field in the DB.
So the UserCategoryMeta table looks like this:
id | user_id | category_id | search_terms
I've found a way which might work but it seems very hacky.
In the usercontroller I have:
$Categories = $this->User->Category->find('list');
Then in the add view I have the checkboxes:
echo $this->Form->input('Category.Category',array(
'type' => 'select',
'multiple' =>'checkbox',
'options' => $Categories,
));
Then the only way I could get each of those checkboxes to have a search terms input next to them is to do this in the add view:
foreach ($Categories as $key => $category){
echo '<input type="text" id="Category'.$key.'SearchTerms" name="data[Category][search_terms]['.$key.']"><br/>';
}
This produces what I want but obviously since I'm just creating random inputs when the form get's submitted it get's black holed. I have managed to get passed this but I know I'm doing it the wrong way and hopefully someone can help me do it the right way.
Also then once I have this data array in the controller im not sure how to save it to the database correctly.
Thanks in advance for any help!
Just do what you're doing, but in the repeat, use $this->Form->input instead of just manually writing the HTML.

Retrieving data through two model relationships

I'm trying to retrieve some data through two model relationships with CakePHP. The models and their associations are as follows:
User hasOne Profile HABTM Skill
I would like the user's skills to be returned when I do a find() operation on the User model, and right now it isn't returned. Here's my find call which is being executed against the User model:
$this->paginate = array(
'conditions' => array(
'OR' => array(
'Profile.firstname LIKE' => "%$q%",
'Profile.lastname LIKE' => "%$q%"
)
)
);
It's returning user data and profile data, but not any skill data. I tried setting recursive to 2 or 3, but that doesn't help either. The only way I can get skill data is if I run find() against the Profile model, but I can't do that. For clarification here's the relevant models and their relationships:
// app/Model/User.php
class User extends AppModel {
public $hasOne = 'Profile';
}
// app/Model/Profile.php
class Profile extends AppModel {
public $belongsTo = 'User';
public $hasAndBelongsToMany = 'Skill';
// app/Model/Skill.php
class Skill extends AppModel {
public $hasAndBelongsToMany = 'Profile';
Can anyone help me get the users skills when retrieving user data? Thanks.
Use CakePHP's Containable Behavior. Your find will then look something like this:
$this->User->find('all',array(
'conditions' => array(
'OR' => array(
'Profile.firstname LIKE' => "%$q%",
'Profile.lastname LIKE' => "%$q%"
)
),
'contain' => array(
'Profile' => array(
'Skill'
)
)
));
MUCH simpler, easier to read, and voila - you get the data you want without needing to use the dreaded 'recursive'.

Cakephp : left-right joins with non-conventioned tables

I'm using an existing database (I can't change it and its table names are not like cake conventions want it), and I'd like to do some left joins but can't do it properly :/
I've already defined my tables, giving them primary keys and the relations in the models.
Here is my problem :
Table Wysipage can have 0 to n wysipage_content, and 0 to n wysipage_menu.
an element from wysipage_content corresponds to 1 and only 1 Wysipage.
an element from wysipage_menu corresponds to 0 or 1 Wysipage.
I'd like to make a request who would give me a list of all the elements from Wysipages, with their eventuals contents and menus, all that in a single table, and by only one request.
Here are my tables definitions (I'm avoiding you the entire schema, just be aware there is a wp_id and a wp_name column) :
class Wysipage extends AppModel {
var $actsAs = array('Containable');
public $useTable = 'wysipage';
public $primaryKey = 'wp_id';
public $displayField = 'wp_name';
var $hasMany = array(
'un' => array(
'Wysipage_contenu' => array(
'className' => 'Wysipage_contenu',
'foreignKey' => 'wpc_wp_id',
)),
'deux' => array(
'Wysipage_menu' => array(
'className' => 'Wysipage_menu',
'foreignKey' => 'wpm_wp_id',
))
);
class Wysipage_contenu extends AppModel {
var $actsAs = array('Containable');
public $useTable = 'wysipage_contenu';
public $primaryKey = 'wpc_id';
public $displayField = 'wpc_h1';
public $belongsTo = array(
'Wysipage' => array(
'className' => 'Wysipage',
'foreignKey' => 'wp_id'
)
);
class Wysipage_menu extends AppModel {
var $actsAs = array('Containable');
public $useTable = 'wysipage_menu';
public $primaryKey = 'wm_id';
public $displayField = 'wm_lien';
public $belongsTo = array(
'Wysipage' => array(
'className' => 'Wysipage',
'foreignKey' => 'wm_wp_id'
)
);
And here is my code to try request (but failed) :
$this->loadModel('Wysipage_contenu');
$this->loadModel('Wysipage_menu');
$this->Wysipage->contain();
$mes_wysipages = $this->Wysipage->find('all', array('joins' => array(
array(
'table' => 'wysipage_contenu',
'alias' => 'wpc',
'type' => 'LEFT',
'conditions'=> array('wpc.wpc_wp_id = Wysipage.wp_id')
),
array(
'table' => 'wysipage_menu',
'alias' => 'wpm',
'type' => 'LEFT',
'conditions'=> array('wpm.wm_wp_id = Wysipage.wp_id')
)
)));
$this->set('wysipages', $mes_wysipages);
$this->render();
What have I done wrong? Is the problem in my model declarations? Or do I use a wrong request type? :(
The request I'd like to make is simply :
SELECT wp_id, wp_name, wpc_id, wpc_name
FROM wysipage
LEFT JOIN wysipage_contenu ON wysipage.wp_id = wysipage_contenu.wpc_wp_id
Just this :(
I'm not even sure I want a LEFT join or a RIGHT join, but anyway the problem remains the same, this code gives me bad answers with multiple occurrences of the same lines :/
Thanks :/
PS : Sorry for my bad English, it's not my native language.
You can not join content and menu tables on wp_id in same query because they both have many-to-one relationship to wp_id, and there by for each row from content there are all rows from menu with same wp_id in result of such join. You need to do 2 queries: one for content and one for menu. Or if list columns you needed identical for both tables you can union both results from this two queries.

Categories