cant get required fields from containable model cakephp - cakephp

I have a hasmany relationship between Guardian ans Student. A Guardian hasmany Students. I cant get the required fields from containable object students, instead I get everything from Students but I do get the required fields from Guardian.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
$this->Guardian->Behaviors->load('Containable');
$guardians =$this->Guardian->find('all',array(
'contain'=>array('Student',
array( 'fields'=> array('Student.guardian_id,Student.id,Student.first_name' ))),
'order' => array('guardian_first_name ASC'),
'fields'=> array('Guardian.guardian_first_name,Guardian.guardian_last_name,Guardian.id' ),
'recursive'=> -1
));
array(
(int) 0 => array(
'Guardian' => array(
'guardian_first_name' => '',
'guardian_last_name' => '',
'id' => '166'
),
'Student' => array(
(int) 0 => array(
'id' => '166',
'student_inactive' => true,
'student_enq' => false,
'student_unallocated' => false,
'first_name' => 'Kala',
'last_name' => 'narayanan',

The fields option is not nested correctly, which you maybe would have noticed if you'd format your code properly, something along the lines of this:
$this->Guardian->Behaviors->load('Containable');
$guardians = $this->Guardian->find('all', array(
'contain' => array(
'Student',
array(
'fields'=> array(
'Student.guardian_id,Student.id,Student.first_name'
)
)
),
'order' => array(
'guardian_first_name ASC'
),
'fields' => array(
'Guardian.guardian_first_name,Guardian.guardian_last_name,Guardian.id'
),
'recursive'=> -1
));
The array holding the fields option must be passed as the value for Student key.
// ...
'contain' => array(
'Student' => array(
'fields'=> /* ...*/
)
),
// ...
On a side note, when passing the fields as a comma separated string (which might not be the best idea), it's not necessary to pass them in an array.

array(
'fields'=> array(
guardian_id, id, first_name'
)
)
use fields without model name.

Related

cake 2.5.1 bind 2 models together

I have a model Ranking which holds a contact_id and belongsTo Model Contact.
Model Contact has a costumer_id and belongsTo Model Costumer.
And hasMany Rankings.
There is also a Model Product which hasMany Ranking.
On a statistics page I select
$this->Product->recursive = 1;
$this->set('products', $this->Paginator->paginate())
;
and I get the array of
array(
'Product' => array(
'id' => '69',
),
'Ranking' => array(
(int) 0 => array(
'id' => '29',
'contact_id' => '9',
'product_id' => '69',
'ranking' => '9',
),
I would like to bind now the Contact and Costumer to the ranking based on the contact_id.
Is this manually possible via bindModel?
If yes, how can I do that?
I tried to set $this->Product->recursive = 1; to 2 and 3, but that select so many other things which I would need to clear with unbindModel... So I hope there is a smarter way of bind those model to get to the data...?
What you basically want to use is containable behavior. With this behavior you are able to filter and limit model find operations. You have the possibility to add this behavior on model level or at the controller to avoid side effects if the application has already grown to a complicated level.
Example from Cake-Book:
// Activate Containable Behavior on the fly in a controller
$this->User->Behaviors->load('Containable');
$this->User->contain();
$this->User->find('all', array(
'contain' => array(
'Profile',
'Account' => array(
'AccountSummary'
),
'Post' => array(
'PostAttachment' => array(
'fields' => array('id', 'name'),
'PostAttachmentHistory' => array(
'HistoryNotes' => array(
'fields' => array('id', 'note')
)
)
),
'Tag' => array(
'conditions' => array('Tag.name LIKE' => '%happy%')
)
)
)
));
Hope this gives you a push into the right direction.
using find it will get me the right data with this:
$this->set('products', $this->Product->find('all', array(
'contain' => array(
'Ranking' => array(
'Contact' => array(
'foreignKey' => 'contact_id',
'Customer' => array(
'foreignKey' => 'customer_id',
)
)
)
)
)));
When using the Paginator it looks like
$this->Paginator->settings['contain'] = array(
'Ranking' => array(
'Contact' => array(
'foreignKey' => 'contact_id',
'Customer' => array(
'foreignKey' => 'customer_id',
)
)
)
);
$this->Product->Behaviors->load('Containable');
$this->set('products', $this->Paginator->paginate());
Thanks so much!!

cakephp model associations - find other child of parent model

I have associations:
Product -> hasMany -> ProductOption
Product -> hasMany -> ProductImage
I want to find the product data including ProductImage with the ProductOption.id
When I do this:
$this->Basket->Product->ProductOption->find('first',array(
'contain' => array(
'Product' => array(
'ProductOption' => array(
'conditions' => array('ProductOption.id = '.$id)
),
'ProductImage'
)
)
));
I get this:
array(
'ProductOption' => array(
'id' => '46',
'product_id' => '9',
),
'Product' => array(
'id' => '9',
'name' => 'Some product',
)
)
Which is most of what I want but without 'ProductImage' included. How do I get this in the array?
I think you're using too much recursion. If you're using Containable on Product, make the query on it, not ProductOption, like this:
$this->Basket->Product->find('first',array(
'contain' => array(
'ProductOption' => array(
'conditions' => array('ProductOption.id = '.$id)
),
'ProductImage'
)
));
And no need to use Product on 'contain' array, it's already guessed.
(I didn't try this, but almost sure)

Trying to get "City" data from Event->Venue->City using 'contain' in CakePHP

I'm trying to return a list of events, and include the city where it's taking place. The city is only associated through the Event's Venue though.
Below is the code I'm using. It returns all the correct data, but it doesn't return ANY city data (other than the city_id field in Venue - which I'm not sure why it's returning).
Associations:
Event belongsTo Venue
Venue hasMany Event
Venue belongsTo City
City hasMany Venue
Code:
$this->Event->Behaviors->attach('Containable');
$events = $this->Event->find('all', array(
'limit' => 5,
'order' => 'Event.created DESC',
'fields' => array(
'name',
'description',
'phone',
'price_general',
'price_child',
'price_adult',
'price_child',
'tickets_url'
),
'contain' => array(
'Venue' => array(
'fields' => array(
'name',
'address',
'city_id',
),
'City' => array(
'fields' => array(
'City.name',
'state'
),
'conditions' => array(
'City.id' => 'Venue.city_id'
)
)
),
'Schedule' => array(
'fields'=>array(),
'Date' => array(
'conditions'=>array(
'Date.start >=' => $start_date,
'Date.start <=' => $end_date,
)
)
)
),
));
Bonus answer: (that I have currently asked in another StackOverflow question) - The Date conditions are supposed to filter which events show up, but instead, they're only filtering which Date data to show.
WORKING ANSWER: (thanks bancer)
$this->Event->recursive = -1;
$options['joins'] = array(
array('table' => 'schedules',
'alias' => 'Schedule',
'type' => 'LEFT',
'conditions' => array(
'Event.id = Schedule.event_id',
)
),
array('table' => 'dates',
'alias' => 'Date',
'type' => 'LEFT',
'conditions' => array(
'Date.schedule_id = Schedule.id',
)
)
);
$options['fields'] = array(
'Event.name',
'Schedule.start_date',
'Date.start',
);
$options['limit'] = 5;
$events = $this->Event->find('all', $options);
I would recommend to avoid using Containable. It generates too many queries in some cases. A better way for complex queries is to join tables "manually".
Another option I would consider at the first place is to search through 'Venue' model without using Containable like this: $this->Event->Venues->find('all', ...). As Venues directly associated with Cities and Events there should be possible to get what you want without extra complexities.
Update: take a look at the question How to change the sequence of 'joins' in CakePHP?
instead of containable, did you try including the city data in fields itself by fields=> array('','','City.name)

CakePhp: On the fly associations using a re-named Model field?

I'm trying to use on the fly associations to trim down the data I retrieve, but the model I'm using is associated to other models with a re-named field because I have 2 of the same models associated with it.
So, here's the model, say 'test', that has two 'user' fields, both related to the User model.
In the model:
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'User_Watched' => array(
'className' => 'User',
'foreignKey' => 'user_id_watched'
)
);
When I retrieve data related to 'test', I want to only retrieve particular data linked to the 'User' and 'User_Watched' fields without any other nested information.
But when I do:
$this->User->unbindModel(array('hasMany' => array('something1', 'something2')), false);
then something1 and something2 data does not show up for the 'User' field of model 'test', but is still retrieved for the 'User_watched' field.
Can I not retrieve unwanted data for the 'User_watched' field?
Hope this makes sense... :)
KcYxA,
Containable behavior might help a lot in this case, as benjamin mentioned, your "find" queries would look like:
$this->User->find('first', array(
'conditions' => array('User.id' => $id),
'contain' => array('UserWatched')
));
In this case, you won't have to use unbindModel method. In this example, you'll get User and UserWatched data.
If you need only User data from "find", then tell Cake to "$this->User->contain();" so it won't go further then User model.
to use on the fly associations to trim
down the data I retrieve
Good idea.
'foreignKey' => 'user_id_watched'
should possibly be:
'foreignKey' => 'user_watched_id'.
Edit 1: At least this would make sense according to my current understanding. If user_id is a correct foreign key(FK), which cakephp uses to unbind the relations, but user_id_watched isn't, than your described behavior is explained.
Edit 2: The Containable behavior gives you another tool for controlling associated models.
Change $primaryKey in fly, run controller
Sample:
// Models
//....
class PreProductoDescripcion extends AppModel {
/**
* Primary key field
*
* #var string
*/
public $primaryKey = 'id_producto_descripcion';
//....
//....
}
class SenasaPedidosDetalles extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'cod_tango';
public $belongsTo = array(
'SenasaPedidos' => array(
'className' => 'SenasaPedidos',
'foreignKey' => 'senasa_pedidos_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'PreProductoDescripcion' => array(
'className' => 'PreProductoDescripcion',
'foreignKey' => 'cod_tango',
//'conditions' => array('SenasaPedidosDetalles.cod_tango' => 'PreProductoDescripcion.codigo'),
'fields' => '',
'order' => ''
)
);
//....
#
// Controller Fly
//...
$this->SenasaPedidos->Behaviors->load('Containable');
$this->SenasaPedidos->SenasaPedidosDetalles->PreProductoDescripcion->primaryKey = 'codigo';
$datos = $this->SenasaPedidos->find(
'first', array(
'fields' => array( 'SenasaPedidos.*' ),
'conditions' => array( 'SenasaPedidos.id' => $id ),
'contain' => array(
'Usuarios' => array(
'fields' => array( 'Usuarios.apellido_nombre' )
),
'Clientes' => array(
'fields' => array( 'Clientes.razon_social' )
),
'Provincias' => array(
'fields' => array( 'Provincias.nombre' )
),
'Transportes' => array(
'fields' => array( 'Transportes.razon_social' )
),
'SenasaPedidosDetalles' => array(
'fields' => array( 'SenasaPedidosDetalles.*' ),
'PreProductoDescripcion' => array(
'fields' => array(
'PreProductoDescripcion.id_producto_descripcion',
'PreProductoDescripcion.descripcion'
)
)
),
)
));
//...

Problem with excluding conditions in complex cakephp model

I'm fairly new to cakephp and I'm having a problem with a complicated find function on my models.
I have a number of Groups each containing a number of Users, and each group can make a number of Orders. Each order consists of a number of OrderAmounts which contain an amount and a user_id (relating to a User).
I have a find which finds an Order, and returns all users in the Group relating to that Order and any OrderAmounts corresponding to that user:
$currentOrder = $this->Order->find('first', array(
'conditions' => array(
'Order.group_id' => $this->Session->read("Auth.User.group_id")
),
'contain' => array(
'Group' => array(
'User' => array(
'OrderAmount' => array(
'OrderAmountType'
)
)
)
)
));
What I now want to do is to return a list of all the Users in the Group relating to the Order above who do not have a corresponding OrderAmount.
So far I have this, but I'm not sure where to put the condition to exclude users with OrderAmounts - if I put the conditions in the contains part it simply removes the OrderAmounts from the model, and if I put them in the top level conditions in the find I get an error.
$currentOrderOutstanding = $this->Order->Group->User->find('all', array(
'conditions' => array(
'Group.id' => $this->Session->read("Auth.User.group_id")
),
'fields' => array('User.id'),
'contain' => array(
'OrderAmount',
'Group'
)
));
Take a look at this example from the tutorial:
$this->User->find('all', array(
'contain'=>array(
'Profile',
'Account' => array(
'AccountSummary'
),
'Post' => array(
'PostAttachment' => array(
'fields' => array('id', 'name'),
'PostAttachmentHistory' => array(
'HistoryNotes' => array(
'fields' => array('id', 'note')
)
)
),
'Tag' => array(
'conditions' => array('Tag.name LIKE' => '%happy%')
)
)
)
));
Notice that all the conditions are inside the 'contain' array. Does it help?

Resources