How can I set limit to association model ("Comment" model in below code) when using paginator component.
I am using below code but not working :
$this->Paginator->settings = array(
'Post' => array(
'recursive' => 1,
'conditions' => $conditions,
'limit' => 10,
),
'Comment' => array(
'limit' => 1
)
);
You should use Containable behavior (in Post model):
public $actsAs = array('Containable');
Then, your Paginator settings should look like:
$this->Paginator->settings = array(
'contain' => array(
'Comment' => array(
'limit' => 1
)
),
'conditions' => $conditions,
'limit' => 10,
);
You have Post hasMany Comment?
If yes, You can set limit by variable $hasMany in Post model:
class Post extends AppModel {
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
//...
'limit' => '1'));
}
Related
// in Person
public $hasMany = array(
'PersonRecommendation' => array(
'className' => 'PersonRecommendation',
'foreignKey' => false,
'dependent' => true,
),
)
// in PersonRecommendation
public $belongsTo = array(
'Person' => array(
'className' => 'Person',
'foreignKey' => false,
'conditions' => array('Person.email = PersonRecommendation.email'),
),
);
When I query these out with find() the Person returns ALL recommendations. Not just those associated by email. I've tried a variety of different things, and I'm stumped. Can someone help?
Using cake 2.4.5.
You need to change the Person model
public $hasMany = array(
'PersonRecommendation' => array(
'className' => 'PersonRecommendation',
'foreignKey' => false,
'dependent' => true,
'conditions' => array('Person.email = PersonRecommendation.email')
)
)
Try the following:
'conditions' => array($Person->email => $PersonRecommendation->email)
I am using the CakeDc Users plugin 2.0 and on the plugins admin_index view I would like to see the associated Books for the users.
I have added $hasMany to User.php Model
public $hasMany = array(
'Book' => array(
'className' => 'Book',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => 'order',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
And in the UserController.php I added 'contain'
protected function _setupAdminPagination() {
$this->Paginator->settings = array(
'limit' => 20,
'order' => array(
$this->modelClass . '.created' => 'desc'),
'contain' => array('Book')
);
}
public function admin_index() {
$this->Prg->commonProcess();
unset($this->{$this->modelClass}->validate['username']);
unset($this->{$this->modelClass}->validate['email']);
$this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs;
if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) {
$parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs);
} else {
$parsedConditions = array();
}
$this->_setupAdminPagination();
$this->Paginator->settings[$this->modelClass]['conditions'] = $parsedConditions;
$this->set('usersList', $this->Paginator->paginate());
$this->layout = 'btst';
}
However I do not get the associated books in the view, I still just the array of users. This should work?
UPDATE:
debug ($this->Paginator->settings);
Output
array(
'limit' => (int) 20,
'order' => array(
'User.created' => 'desc'
),
'contain' => array(
(int) 0 => 'Book'
),
'User' => array(
'conditions' => array()
)
)
Debug $this->Paginator->settings before calling $this->Paginator and try
'contain' => array('Book')
However it is bad practice to modify plugins directly, you won't be able to update them anymore without trouble (merge conflicts, API changes...) in the worst case. The plugins documentation comes with a lot documentation that explains how to extend the plugin instead of modinfy it directly.
Edit: This was actually a bug in the plugin, fixed it in https://github.com/cakedc/users/commit/73aa350
I have the following code that gives an error from the moment I do a find condition on the associated Model (HABTM):
class Users extends Model{
public $useTable = 'users';
public $hasAndBelongsToMany = array(
'UserCategories' => array(
'className' => 'UserCategories',
'joinTable' => 'user_categories_link',
'foreignKey' => 'user_id',
'associationForeignKey' => 'category_id',
'unique' => false,
)
);
public function getData($page = 0, $category = '', $subcategory = ''){
return $this->find('all', array(
'limit' => 6,
'page' => $page,
'conditions'=> array(
'active'=> 1,
'UserCategories.category_name' => $category, // THIS GIVES ERROR
'UserCategories.category_subcategory' => $subcategory, // THIS GIVES ERROR
)
));
}
In my Controller:
$this->Users->getData(0, 'somemaincategory', 'somesubcategory');
I can't seem to do conditions on the related HABTM-Model (UserCategories in this case). I also tried to use 'contain' (with $actsAs), but then he stills gives me all the User data even if there is no Category linked with it. The Category array is in that case just blank.
Hope someone can help me.
Thanks in advance,
AƤron
Do a manual join. You can use this to do an actual inner join (contain will act as a left join). http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
$this->find('all',
array(
'conditions' => array(
'active' => 1,
),
'joins' => array(
array(
'table' => 'user_categories_link',
'alias' => 'UserCategoriesLink',
'type' => 'inner',
'conditions' => array(
'UserCategoriesLink.user_id = User.id'
),
),
array(
'table' => 'user_categories',
'alias' => 'UserCategories',
'type' => 'inner',
'conditions' => array(
'UserCategories.id = UserCategoriesLink.category_id',
'UserCategories.category_name' => $category,
'UserCategories.category_subcategory' => $subcategory,
),
)
),
)
);
Is there a way in cakephp using ORM to get the item that belongs to a specific child item. For example I was to get the related Post record for a specific Comment records.
This is my Comment model:
var $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
I was trying this but it's pull back every post, even those that don't have the comment I'm querying against:
$this->Post->contain('Comment');
$results = $this->Post->find('all', array(
'contain' => array(
'Comment' => array(
'conditions' => array(
'id' => 15
)
)
)));
Any other way to do this?
Are you sure you don't have to specify the model in your conditions?
For example:
$this->Post->contain('Comment');
$results = $this->Post->find('all', array(
'contain' => array(
'Comment' => array(
'conditions' => array(
'Comment.id' => 15
)
)
)));
my research led me to this post regarding the issue with contains:
http://nuts-and-bolts-of-cakephp.com/2008/07/17/forcing-an-sql-join-in-cakephp/
so my final solution was as follows:
$this->Post->unbindModel(array('hasMany' => array('Comment')));
$results = $this->Post->bindModel(array('hasOne' => array(
'Comment' => array(
'foreignKey' => false,
'conditions' => array('Comment.post_id = Post.id'))
)));
$results = $this->Post->find('all', array(
'conditions' => array(
'Comment.id' => 10
)));
not pretty but gets the job done :)
I use Cakephp framework, and I have problem with my association.
How create belong to association in models in cake php.
When I use belongto and hasMany in my model.
Can I find sample model to view this example?
Simple belongsTo association:
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
?>
Simple hasMany association:
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'user_id',
'conditions' => array('Comment.status' => '1'),
'order' => 'Comment.created DESC',
'limit' => '5',
'dependent'=> true
)
);
}
?>
More specific information about associations is in the CakePHP Book.
User has Many Photos
Photos belongs to User
In User Model :
var $hasMany = array(
'Photo' => array(
'className' => 'Photo',
'foreignKey' => 'user_id'
);
In Photo Model :
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'PhotoAlbum' => array(
'className' => 'PhotoAlbum',
'foreignKey' => 'photo_album_id',
'conditions' => '',
'fields' => '',
'order' => '',
))