I have multiple tables associated with each other:
Messages
Message details
Message details has the fields id, Message_id, created_date and message.
I want to sort messages on the message details created_date.
How can I do that?
$messages = $this->Message->find('all', array(
'conditions' => array('Message.status'=> 'progress')
));
// What do I do now to sort it on message_details created time?
class MessageDetail extends AppModel {
//public $hasMany=array('MessageDetail');
//$belongsTo = array('Message');
public $belongsTo = array(
'Message' => array(
'className' => 'Message',
'foreignKey' => 'message_id'
)
);
}
class Message extends AppModel {
public $hasMany=array('MessageDetail');
}
Can You pls try this code,You need Descending order put DESC instead of ASC
$messages = $this->Message->find('all', array(
'conditions' => array('Message.status'=> 'progress'),
'order' => array('MessageDetails.created_date ASC')
));
You can simple pass order, and mention which field to sort, see below:
$messages = $this->Message->find('all', array(
'conditions' => array('Message.status'=> 'progress'),
'ORDER' => 'MessageDetail.created_date ASC'
));
UPDATE
You Message.php Model will look like:
class Message extends AppModel {
public $hasMany = array(
'MessageDetail' => array(
'className' => 'MessageDetail',
'foreignKey' => 'message_id',
'order' => 'MessageDetail.created_date DESC'
),
);
}
and MessageDetail.php Model will be:
class MessageDetail extends AppModel {
public $belongsTo = array(
'Message' => array(
'className' => 'Message',
'foreignKey' => 'message_id'
)
);
}
Related
I'm using CakePHP 2.6.7.
Here is my database (simplified) :
***Users***
id
username
***Cars***
id
CarMake
CarModel
NumberPlate
user_id
***Addresses***
id
address
postalcode
city
country
***Addresses_Users***
user_id
address_id
This is what I want :
One user can have multiple cars and one car is owned by only one user.
One user can have multiple addresses and one address can by owned by multiple users.
One car can have multiple addresses and one address can be owned by multiple cars.
So right now I have a model User.php :
<?php
class User extends AppModel {
public $hasMany = array(
'Car' => array(
'classname' => 'Car')
);
public $hasAndBelongsToMany = array(
'Address' => array(
'classname' => 'Address')
);
?>
And a model Car.php :
<?php
class Car extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User'
)
);
public $hasMany = array(
'Address' => array(
'classname' => 'Address')
);
}
?>
And a model Address.php :
<?php
class Address extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User'
),
'Car' => array(
'classname' => 'Car')
);
}
?>
I tried to link cars with addresses but I can't get it to work...
If I do a debug($this->User->find()); I have this result :
array(
'User' => array(
'id' => '1',
'username' => 'wblondel',
),
'Car' => array(
(int) 0 => array(
'id' => '2',
'CarMake' => 'Allard',
'CarModel' => '2005',
'NumberPlate' => '56QDS1',
'user_id' => '1',
),
(int) 1 => array(
'id' => '5',
'CarMake' => 'Alvis',
'CarModel' => '25',
'NumberPlate' => '5lDS1',
'user_id' => '1',
)
),
'Address' => array(
(int) 0 => array(
'id' => '2',
'address' => 'fghfghgf',
'postalcode' => '78200',
'city' => 'buchelay',
'country' => '40'
),
(int) 1 => array(
'id' => '3',
'address' => 'jkjkjk',
'postalcode' => '69100',
'city' => 'villeurbanne',
'country' => '59'
)
)
)
What I would firstly like is that in each car there is an array "address"...
Thanks!
From what you've described your associations look wrong. They should be something like:-
User
<?php
class User extends AppModel {
public $hasMany = array(
'Car'
);
public $hasAndBelongsToMany = array(
'Address'
);
}
Car
<?php
class Car extends AppModel {
public $belongsTo = array(
'User'
);
public $hasAndBelongsToMany = array(
'Address'
);
}
Address
<?php
class Address extends AppModel {
public $hasAndBelongsToMany = array(
'Car',
'User'
);
}
You don't need to use className unless the alias you want to use is different from the model name or the model belongs to a plugin.
Then if you use the Containable behavior you should be able to get everything back using something like:-
$this->User->find(
'all',
array(
'contain' => array(
'Address',
'Car' => array(
'Address'
)
)
)
);
To control what exactly is retrieved from a find() query, you can use the ContainableBehavior.
By the way I think you have an error in your Address model as an address doesn't "belong to" a user, but can have many ones. It should be an HABTM relation as you have done in the User model.
i have some troubles with cakephp 2.x and the relationship of the models, i have 3 models, Post, User and Comment.
I would like bind the Post with his User and Comment with his User.
In Post Model:
$belongsTo = array('Comment','User'),
$hasMany = array('CommentOfPost'=>array('className'=>'Comment'));
I have Post bind with User, and all comment from the post but i don't have users of the comments.
Edit :
Sql output
1 SELECT `Post`.`id`, `Post`.`title`, `Post`.`content`, `Post`.`tags`, `Post`.`created`, `Post`.`modified`, `Post`.`user_id`, `User`.`id`, `User`.`username`, `User`.`password`, `User`.`role`, `User`.`name`, `User`.`lastname`, `User`.`birthdate`, `User`.`email`, `User`.`created`, `User`.`modified` FROM `blog`.`posts` AS `Post` LEFT JOIN `blog`.`users` AS `User` ON (`Post`.`user_id` = `User`.`id`) WHERE `Post`.`id` = 16 LIMIT 1 1 1 0
2 SELECT `CommentOfPost`.`id`, `CommentOfPost`.`post_id`, `CommentOfPost`.`user_id`, `CommentOfPost`.`content`, `CommentOfPost`.`created` FROM `blog`.`comments` AS `CommentOfPost` WHERE `CommentOfPost`.`post_id` = (16)
Edit :
Comment Model
public $belongsTo = array('User' => array('className' => 'User'));
Post Model
public $belongsTo = array('Comment' => array('className' => 'Comment'));
Edit :
Thank for reply, i have the same result like before i have Post and his User and Post and his comments but not users of comments
Now my model Post
$hasMany = array(
'Comment' => array(
'className' => 'Comment',
)
),
$belongsTo = array(
'User' => array(
'className' => 'User',
)
);
User model
$hasMany = array('Comment' => array('className' => 'Comment'));
Comment Model
$belongsTo = array('Users' => array('className' => 'User'), 'Posts' => array('clasName' => 'Post'));
I use paginate querying in my PostsController
$this->Paginator->settings = $this->paginate;
$data = $this->Paginator->paginate('Post');
$this->set('posts', $data);
Edit :
My paginate params
$paginate = array(
'limit' => 10,
'recursive' => 2,
'order' => array(
'Post.id' => 'desc'
)
);
I try with and without recursive option
Ok it's done !
For resume i have :
class User extends AppModel {
public $hasMany = array('Comment' => array('className' => 'Comment'));
}
class Post extends AppModel
{
$hasMany = array(
'Comment' => array(
'className' => 'Comment',
)
),
$belongsTo = array(
'User' => array(
'className' => 'User',
)
);
}
class Comment extends AppModel {
public $belongsTo = array('User' => array('className' => 'User'), 'Post' => array('clasName' => 'Post'));
}
PostsController extends AppController
{
....
$this->Post->recursive = 2;
$post = $this->Post->findById($id);
...
}
Thank for your help guys, you're awesome !
Post does not belongs to Comment. It has many comments.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
Example from the cookbook:
class User extends AppModel {
public $hasMany = array(
'MyRecipe' => array(
'className' => 'Recipe',
)
);
}
For your application:
class Post extends AppModel {
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
)
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
)
);
}
I'm trying to traverse the relations on CakePHP models.
This is the database:
I can access product attributes (product->attributes) on a product model but I cannot access the product attributes on Ad model (ad->product->attributes).
Here is my code:
//Product Model
class Product extends AppModel {
public $useTable = 'products';
public $displayField = 'name';
public $hasAndBelongsToMany = array(
'Attributes' =>
array(
'className' => 'Attribute',
'joinTable' => 'product_has_attributes',
'foreignKey' => 'products_id',
'associationForeignKey' => 'attributes_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'with' => 'product_has_attributes'
)
);
public $hasMany = array(
'Ads' => array(
'className' => 'Ad',
'foreignKey' => 'Products_id',
'conditions' => '',
'order' => '',
'limit' => '',
'dependent' => true
)
);
//Ad Model
class Ad extends AppModel {
public $displayField = 'Name';
public $belongsTo = array(
'Product' => array(
'className' => 'Products',
'foreignKey' => 'products_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
//Attribute Model
class Attribute extends AppModel {
public $displayField = 'name';
public $hasAndBelongsToMany = array(
'Products' =>
array(
'className' => 'Product',
'joinTable' => 'product_has_attributes',
'foreignKey' => 'attributes_id',
'associationForeignKey' => 'products_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'with' => 'product_has_attributes'
)
);
// Products controller -> Action View
class ProductsController extends AppController {
public function view($id = null) {
if (!$this->Product->exists($id)) {
throw new NotFoundException(__('Invalid product'));
}
$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
$this->set('product', $this->Product->find('first', $options));
}
}
// Ads controller -> Action View
class AdsController extends AppController {
public function view($id = null) {
if (!$this->Ad->exists($id)) {
throw new NotFoundException(__('Invalid ad'));
}
$options = array('conditions' => array('Ad.' . $this->Ad->primaryKey => $id));
$this->set('ad', $this->Ad->find('first', $options));
}
And here is what I do in the views:
//Products Views: snipet of view.ctp
print_r ($product);
// this prints rhe product and all associated attributes
//Ads Views: snipet of view.ctp
print_r ($ad['Product']);
//this will print only the product fields, but not the attributes associated to the product
What is wrong? How do I access the relation Ad->product->attribute from my Ad model?
I can think of a couple of simple ways to accomplish this.
First:
class AdsController extends AppController {
if (!$this->Ad->exists($id)) {
throw new NotFoundException(__('Invalid ad'));
}
$options = array(
'conditions' => array('Ad.' . $this->Ad->primaryKey => $id),
'recursive' => 1, // or more
);
$this->set('ad', $this->Ad->find('first', $options));
}
That would be the simplest code change to make sure you get attributes that are related to the product that is returned.
Otherwise, you alluded to this in your question. You can access related Models through the model, so:
$this->Ad->Product->find(...);
I have had issues with Cake not liking my find conditions when using related models in conditions, and I'm not sure of the proper syntax, but if you wanted to pursue that, I'm sure you can track it down through the docs or by experimentation.
Finally, I would advise you to check into the ContainableBehavior, which will allow you to fine tune which fields are actually returned in the results. I hope this answers your question!
Perhaps the problem could be that you're not using CakePHP's conventions.
From the docs:
"new join table’s name needs to include the names of both models involved, in alphabetical order, and separated with an underscore ( _ )."
So, your join table should be named attributes_products.
Also, check your foreign keys. They should be in singular form.
attributes_products.id
attributes_products.product_id
attributes_products.attribute_id
Hopefully that solves the problem.
References:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm
http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions
I'm trying to make it so when I do a $this->Topic->find('all'), it will pull all the associated User data, and all the associated Post data. The problem is that I can't manage to do that right now.
Right now I do $this->User->find('all'), which returns the Topic data, and all Posts inside that Topic. I guess this is what my code tells it to do, but it's not what I want. Please forgive my explanation.. I really suck at CakePHP associations.
Here are my models for your reference:
Topic.php
class Topic extends AppModel {
var $name = 'Topic';
var $actsAs = array('Containable');
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'topicid'
)
);
var $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
var $belongsTo = array(
'Forum' => array(
'className' => 'Forum',
'foreignKey' => 'id'
)
);
}
Post.php
class Post extends AppModel {
var $name = 'Post';
var $actsAs = array('Containable');
var $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'userid'
),
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'topicid'
)
);
}
Forum.php
class Forum extends AppModel {
var $name = 'Forum';
var $hasMany = array(
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'forumid'
)
);
}
User.php
class User extends AppModel {
var $name = 'User';
var $actsAs = array('Containable');
var $hasMany = array(
'ForumPost' => array(
'className' => 'ForumPost',
'foreignKey' => 'userid'
),
'ForumTopic' => array(
'className' => 'ForumTopic',
'foreignKey' => 'userid'
)
);
}
I want it so that I can do $this->Topic->find('all'), and it gives me the Topic data, then the Post data for each Post in the Topic, and the User data for each Post in the Topic (respectively)..
Thanks for any help.
I found out my problem, thanks to savant in the #cakephp IRC channel. My models were named wrong.. :)
I have in a video sharing website project these models :
class Video extends AppModel {
var $name = 'Video';
var $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'Tag',
'joinTable' => 'videos_tags',
'foreignKey' => 'video_id',
'associationForeignKey' => 'tag_id',
'unique' => true,
)
);
}
class Tag extends AppModel {
var $name = 'Tag';
var $hasAndBelongsToMany = array(
'Video' => array(
'className' => 'Video',
'joinTable' => 'videos_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'video_id',
'unique' => true,
)
);
}
class VideosTag extends AppModel {
var $name = 'VideosTag';
var $belongsTo = array(
'Video' => array(
'className' => 'Video',
'foreignKey' => 'video_id',
),
'Tag' => array(
'className' => 'Tag',
'foreignKey' => 'tag_id',
'conditions' => '',
'counterCache' => 'videos_tag_counter'
)
);
}
The counterCache for tags is not working. I don't know why and when I tried to add a beforeSave() callback to videosTag model I found that it doesn't execute when a video get saved (and this video has tags and i find them in the database so how the relation videosTags is saved ? ) !!! can any body explain why this is happening.
Saving a Video with data like this:
array(
'Video' => array(
...
),
'Tag' => array(
'Tag' => array(
...
),
),
);
on the Video model will not trigger a beforeSave callback on the VideosTag model because Cake handles HABTM data without requiring (or even using) the join/with/through model.
There is no built in functionality for what you are trying to achieve, as far as I am aware.
Check out Counter Cache behavior for HABTM relations, might do what you need