greatings!
my models are not being configured as containable. this following query dont display the correct result (i want to retrieve all the available cars between two dates in all category)
$car=$this->Genre->find('all', array(
'contain' => array(
'cars'=>array(
'conditions'=>array(
'cars.startdate <=' => $qdu ,
'cars.enddate >=' => $qau
)
)
)
)
);
this is my Genre model:
class Genre extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'houses' => array(
'className' => 'houses',
'foreignKey' => 'houses_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'cars' => array(
'className' => 'cars',
'foreignKey' => 'genres_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
and this is car model:
class Car extends AppModel {
public $belongsTo = array(
'Genres' => array(
'className' => 'Genres',
'foreignKey' => 'genres_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'ways' => array(
'className' => 'ways',
'foreignKey' => 'cars_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
Model names should normally be singular, so "Car" instead of "cars".
$car=$this->Genre->find('all', array(
'contain' => array(
'Car'=>array(
'conditions'=>array(
'Car.startdate <=' => $qdu ,
'Car.enddate >=' => $qau
)
)
)
)
);
Also, make sure the Genre model loads the Containable behaviour.
class Genre extends AppModel {
$actsAs = array('Containable');
}
Associations should also use singular, capitalised model names:
class Genre extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'House' => array(
'className' => 'House',
'foreignKey' => 'houses_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'Car' => array(
'className' => 'Car',
'foreignKey' => 'genres_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
It's a Cake convention to have pluralised controllers (CarsController, GenresController) and singular models (Car, Genre). When defining associations you're in essence linking models together and not controllers, hence the need for singular, capitalised classnames.
Related
I have 5 tables: tournaments, rounds, videos, rounds_tournaments, rounds_videos
Relations:
Tournament hasMany Video
Video belongsTo a Tournament
Tournament HABTM Round
Round HABTM Tournament
Video HABTM Round
Round HABTM Video
What I want to achieve:
Now I only want the videos that belong to a specific Tournament and Round.
So when clicking on a Tournament I only want the rounds associated with the clicked Tournament. And when clicking on a Round I only want the get the videos associated with the clicked Round and the Tournament.
I've tried everything but can't get it to work. I've played with several views as well (the index, view actions of Round/Tournament/Video). I'm not sure where to put the the JOIN statement, but I think it's inside the controller.
The Problem I've got atm is that when clicking on a Round it gives me the videos of that Round but for all the Tournaments and not for one Tournament.
Also looked here:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
CakePHP find HABTM
Round Model:
public $hasAndBelongsToMany = array(
'Tournament' => array(
'className' => 'Tournament',
'joinTable' => 'rounds_tournaments',
'foreignKey' => 'round_id',
'associationForeignKey' => 'tournament_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
'Video' => array(
'className' => 'Video',
'joinTable' => 'rounds_videos',
'foreignKey' => 'round_id',
'associationForeignKey' => 'video_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
Video Model:
public $belongsTo = array(
'Tournament' => array(
'className' => 'Tournament',
'foreignKey' => 'tournament_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
public $hasAndBelongsToMany = array(
'Round' => array(
'className' => 'Round',
'joinTable' => 'rounds_videos',
'foreignKey' => 'video_id',
'associationForeignKey' => 'round_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
Tournament Model:
public $hasMany = array(
'Video' => array(
'className' => 'Video',
'foreignKey' => 'tournament_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasAndBelongsToMany = array(
'Round' => array(
'className' => 'Round',
'joinTable' => 'rounds_tournaments',
'foreignKey' => 'tournament_id',
'associationForeignKey' => 'round_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
);
RoundsVideo model:
public $belongsTo = array(
'Round' => array(
'className' => 'Round',
'foreignKey' => 'round_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Video' => array(
'className' => 'Video',
'foreignKey' => 'video_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
RoundsTournament model:
public $belongsTo = array(
'Round' => array(
'className' => 'Round',
'foreignKey' => 'round_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Tournament' => array(
'className' => 'Tournament',
'foreignKey' => 'tournament_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
What I have tried:
index action of VideosController
$this->Video->recursive = -1;
$options['joins'] = array(
array('table' => 'rounds_videos',
'alias' => 'RoundsVideo',
'type' => 'inner',
'conditions' => array(
'Video.video_id = RoundsVideo.video_id'
)
),
array('table' => 'rounds',
'alias' => 'Round',
'type' => 'inner',
'conditions' => array(
'RoundsVideo.round_id = Round.round_id'
)
)
);
$this->set('videos', $this->Paginator->paginate());
index action of TournamentsController
$this->Tournament->recursive = -1;
$options['joins'] = array(
array('table' => 'rounds_tournaments',
'alias' => 'RoundsTournament',
'type' => 'inner',
'conditions' => array(
'Tournament.tournament_id = RoundsTournament.tournament_id'
)
),
array('table' => 'rounds',
'alias' => 'Round',
'type' => 'inner',
'conditions' => array(
'RoundsTournament.round_id = Round.round_id'
)
)
);
$this->set('tournaments', $this->Paginator->paginate());
can someone provide an explanation for me?
I've got a database where I'm trying to update 4 tables from one page, Organizations, OrganizationsDetails, Account, AccountRoles. The add view itself would contain fields for the Account and the Organization. When I call the OrganizationsController add function, I want to then call add on AccountController, AccountRolesController (set Account as an administrator role), and OrganizationDetailsController (set that Account as the administrator for that specific Organization).
Should I be using components for this? When I call add() on the Organizations, I always intend to perform these steps - never solely create the Organizations entry below. Should I be doing this directly in that function or some other method?
Let me know if I need to explain more, thanks!
EDIT: Added the four models below:
Organization
class Organization extends AppModel {
public $hasMany = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'organization_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasOne = array(
'OrganizationDetail' => array(
'className' => 'OrganizationDetail',
'foreignKey' => 'organization_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
OrganizationDetail
class OrganizationDetail extends AppModel {
public $belongsTo = array(
'Organization' => array(
'className' => 'Organization',
'foreignKey' => 'organization_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
AccountRole
class AccountRole extends AppModel {
public $belongsTo = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Account
class Account extends AppModel {
public $belongsTo = array(
'Organization' => array(
'className' => 'Organization',
'foreignKey' => 'organization_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasOne = array(
'OrganizationDetail' => array(
'className' => 'OrganizationDetail',
'foreignKey' => 'account_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasMany = array(
'AccountRole' => array(
'className' => 'AccountRole',
'foreignKey' => 'account_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
You've got wrong the whole MVC idea. Controllers should not call methods in other controllers (or at least not in this scenario).
The right approach would be to set up OrganizationsController->add() so that it saves all the data by independent calls to each Model->save($data), or by using
Organization->saveAssociated($data,array('deep' => true))
Your form fields need to be named properly for this to work. For example:
echo $this->Form->create('Organization', array('action' => 'add'));
echo $this->Form->input('Organization.name'));
echo $this->Form->input('Account.0.name', array('label' => 'Account name'));
echo $this->Form->input('OrganizationDetail.details', array('label' => 'Org. Details'));
echo $this->Form->input('Account.0.AccountRole.id', array('label' => 'Account Role'));
echo $this->Form->end('Add');
However, you may get into trouble if you try to go too many levels deep. If you find yourself in this situation, it's probably because you data model is flawed.
This is what your data model currently looks like:
This is what I believe it should look like:
It is critical to have your data model right before you start coding. It can save you many headaches!
I'm in trouble with a more complex situation of HABTM in Cakephp. Ina nutshel, the situation is: a have users and movies, the relationship between then is HABTM, but i also need a relationship types.
USERS - HABTM - MOVIES
|
RELATION_TYPES
The relationship between users and movies could be many different types: author type, public type, etc.
When i try to use the contain behavior, i get an error message that says:
Model "MoviesUser" is not associated with model "RelationType".
But all the models are associated.
These are the models:
User Model
public $hasAndBelongsToMany = array(
'Movie' => array(
'className' => 'Movie',
'joinTable' => 'movies_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'movie_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => 'Movie.title',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
Movie Model
public $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'movies_users',
'foreignKey' => 'movie_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
MoviesUser Model
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Movie' => array(
'className' => 'Movie',
'foreignKey' => 'movie_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'RelationType' => array(
'className' => 'RelationType',
'foreignKey' => 'relation_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
In the MoviesController, i'm trying to get some movies and the relation with users and the type of the relation. Here is where i call the Contain on Movies Controller:
$this->Movie->contain(array(
'Person' => array(
'order' => array(
'Person.name'=>'ASC'
),
'Activity'
),
'Country' => array(
'order'=>array(
'Country.name'=>'ASC'
)
),
'User'=>array(
'conditions'=>array(
'User.id'=>$this->Session->read('Auth.User.id')
),
'MoviesUser'=>array(
'RelationType'
)
)
));
When i try to run this i get the message Model "MoviesUser" is not associated with the model "RelationType".
The fields of movies_user are:
id, user_id, relation_type_id, rate, comment
The fields of relation_types are:
id, title
And here my $has_many property on RelationType Model:
public $hasMany = array(
'MoviesUser' => array(
'className' => 'MoviesUser',
'foreignKey' => 'relation_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
I have two Model. One is user and onother is UserProfile and in created their tables users and user_profiles respectivly. I want when i delete user data thenrelated UserProfile should also automatically deleted. I created hasone and belongs to relation. One more thing i am deleting data using checkbox with delete button so i could able to delete multiple record at once.
User.php
public $hasOne = array(
'UserProfile' => array(
'className' => 'UserProfile',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
UserProfile.php
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
In UsersController i have a deleteSelected function
UsersController.php
public function deleteSelected()
{
foreach($this->data['User'] as $key => $value)
{
if($value != 0)
{
$this->User->delete($value);
}
}
$this->redirect($this->referer());
}
finaaly i am getting an Error: Table groups_users for model GroupsUser was not found in datasource default. Please tell whats wrong in my code. Thanks.
set dependent to true
public $hasOne = array(
'UserProfile' => array(
'className' => 'UserProfile',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
i recently upgraded my 1.3-app to 2.4 with https://github.com/dereuromark/upgrade
But now some usages of $this->Model->find() doesn't work. Especially the joins to the associated models/tables.Resulting in "Unknown column 'Bill.customer_id' in where clause".
My setup:
Tables and Associations: http://i.stack.imgur.com/bjOIz.png (Image)
Models:
App::uses('AppModel', 'Model');
class Customer extends AppModel {
public $actsAs = array('Logable', 'Containable');
public $hasMany = array(
'Bill' => array(
'className' => 'Bill',
'foreignKey' => 'customer_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)...
-----------------------------------------------------------------
App::uses('AppModel', 'Model');
class Bill extends AppModel {
public $actsAs = array('Containable', 'Logable', 'Lockable');
public $belongsTo = array(
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'conditions' => '',
'fields' => '',
'order' => ''
)...
public $hasAndBelongsToMany = array(
'Stage' => array(
'className' => 'Stage',
'joinTable' => 'bills_stages',
'foreignKey' => 'bill_id',
'associationForeignKey' => 'stage_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)...
--------------------------------------------------------------
App::uses('AppModel', 'Model');
class BillsStage extends AppModel {
public $actsAs = array('Containable', 'Logable', 'Lockable');
public $belongsTo = array(
'Bill' => array(
'className' => 'Bill',
'foreignKey' => 'bill_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Stage' => array(
'className' => 'Stage',
'foreignKey' => 'stage_id',
'conditions' => '',
'fields' => '',
'order' => ''
)...
--------------------------------------------------------
App::uses('AppModel', 'Model');
class Stage extends AppModel {
public $displayField = 'name';
public $actsAs = array('Tree', 'Containable', 'Logable');
public $hasMany = array(
'Bill' => array(
'className' => 'Bill',
'foreignKey' => 'stage_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)...
public $hasAndBelongsToMany = array(
'Bill' => array(
'className' => 'Bill',
'joinTable' => 'bills_stages',
'foreignKey' => 'stage_id',
'associationForeignKey' => 'bill_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)...
In 1.3 the joins are working. In 2.4 only 1-dimensional sql query.
Any idea?
--edit--
$billsStages = $this->Customer->Bill->BillsStage->find('all', array(
'conditions' => array(
'Bill.customer_id' => $this->Customer->id,
'Stage.id' => array_keys($vk_stages)
),
'order' => 'BillsStage.created DESC'
));
Try a join (example taken out from the docs)
$options['joins'] = array(
array('table' => 'bills',
'alias' => 'Bill',
'type' => 'LEFT',
'conditions' => array(
'Bill.customer_id' => $this->Customer->id,
)
),
array('table' => 'stages',
'alias' => 'Stage',
'type' => 'LEFT',
'conditions' => array(
'Stage.id' => array_keys($vk_stages)
)
)
);
$options['conditions'] = array();
$items= $this->Customer->Bill->BillsStage->find('all', $options);
(adjust to your case).
According to this answer, a contain for conditions on the secondary model is not possible, since contain makes a different query. So use joins if you want to apply conditions on secondary models, or create a simple function in your model that does two separate queries and creates a macro array that returns the mixed results if you want to use contain.