I have the tables USERS(id, email), MAIL(id, message, receiver, user_id), SERVICES(id, service, user_id)...
A user posts a service, the another user replies to that service through the mail. I want to write a query that will retrieve the email of the user that posted that particular service from USERS and capture it in the receiver field in MAIL when someone replies to the service.
I'm fairly new to cake, and have no idea where to go from here
$receiver = $this->User->find('first',array(
'conditions' => array(
'Users.user_id' => 'Services.user_id',
)
));
SQL Query
SELECT u.email FROM users AS u, services AS s WHERE s.user_id = u.id LIMIT 0 , 30
Assuming you have User.php model, create one action as shown below
function getData(){
$options = array(
//'conditions' => array(‘User.user_id’=>9),
'joins' => array(
array(
'alias' => 'Service',
'table' => ‘services’,
'type' => 'LEFT',
'conditions' => array(
'Service.user_id = User.id',
),
),
array(
'alias' => 'Mail',
'table' => ‘mail',
'type' => 'LEFT',
'conditions' => array(
'Mail.user_id = User.id',
),
)
),
'fields' => array('User.email'),
'limit'=>10
);
$returnData = $this->find('all',$options);
return $returnData;
}
From controller call this method as
$data = $this->User->getData();
print_r($data);
Hope it will solve your problem!
Related
I am working on a cakephp 2.x .. I have two tables in my database both have the userid I am using bindModel.. my query is working fine ... I just have one problem .. I want to add the condition in where clause that
**where userid = $userid**
function getMessages($userid){
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'AND' =>
array(
array('OR' => array(
array('Message.mobileNo = Contact.mobileNo'),
array('Message.mobileNo = Contact.workNo'),
)),
)
),
'type' => 'LEFT',
)
)
), false);
return $this->find('all', array('conditions' => array(),
'fields' => array('Message.mobileNo'
),
'group' => 'Message.mobileNo',
'limit' => 6));
}
I am getting user id in parameter ... so I want to add the condition that get the following result where
message.userid and contact.userid = $userid ...
Just split the conditions in two lines like:
'conditions' => array(
'Contact.user_id' => $userid,
'Message.user_id = Contact.user_id',
[...]
)
But the approach that makes more sense is to leave the bind as is - after all the bind is generally between message users and contact users with the same id - and add the condition for the specific case in your find('all') with 'Message.user_id' => $userid.
i have a two tables namely; histories and users. i need to display data like:
id | Username | Lastest created Post | First created Post
the data of id and username is from users table and the last created and first created post data is from histories. i need to view all the users, their lastest created post and their first created post. please help me to make controller and view thanks
Try below.
<?php
$users = $this->User->find('all',array
(
'conditions' => array
(
//conditions goes here
),
'fields' => array
(
'User.id',
'User.username',
'History.Lastest created Post',
'History.First created Post'
)
));
?>
Assume that relation between 'User' and 'History' table is One-to-One and there's a 'user_id' column in History table, you may need to specify relation between them in History model, for example:
var $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Then, you need to perform joins to do this. For example, somewhere in your User model, try something like this:
class User extends AppModel {
....
function getAllUsersHistory{
$allHistories = $this->find('all', array(
'joins' => array(
'table' => 'history',
'alias' => 'HistoryJoin'
'type' => 'INNER',
'conditions' => array(
// your conditions, for example: 'History.user_id' => 'User.id'
)
),
'fields' => array(
'User.id',
'User.username',
'History.lastest_created_post',
'History.first_created_post'
)
));
return $allHistories;
}
.....
}
I have 3 tables: users, specialities, specialities_users.
User HABTM Speciality
Speciality HABTM User
SpecialitiesUser belongsTo Speciality,User
I have also model SpecialitiesUser.
When I want to get specialities for given user I can do it through
SpecialitiesUser->find('all' array('conditions' => array('user_id' => $given_user_id));
Now I want to get all specialities that user NOT belongs to. How I can do this in Cake?
The $other_specialities variable in the following code should be what you want:
$the_users_specialities = $this->SpecialitiesUser->find('list', array(
'conditions' => array(
'SpecialitiesUser.user_id' => $given_user_id
),
'fields' => 'SpecialitiesUser.speciality_id'
));
$other_specialities = $this->Speciality->find('all', array(
'conditions' => array(
'NOT' => array(
'Speciality.id' => $the_users_specialities
)
)
));
UPDATE: This is how to do it using a single query:
$db = $this->Speciality->getDataSource();
$sub_query = $db->buildStatement(
array(
'fields' => array('`SpecialitiesUser`.`speciality_id`'),
'table' => $db->fullTableName($this->SpecialitiesUser),
'alias' => 'SpecialitiesUser',
'conditions' => array('`SpecialitiesUser`.`user_id`' => $given_user_id),
),
$this->Speciality
);
$other_specialities = $this->Speciality->find('all', array(
'conditions' => array(
$db->expression('`Speciality`.`id` NOT IN (' . $sub_query . ')')
)
));
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)
i have multiple habtm like these :
// User model
var $hasMany = array('Post');
// Post model
var $hasAndBelongsToMany = array('Category', 'Tag');
// Category model
var $hasAndBelongsToMany = array('Post');
// Tag model
var $hasAndBelongsToMany = array('Post');
I tried to fetch all post along with its user and tags (within a certain category), somehow if i fetch tags, the result was wrong.
$this->paginate = array
(
'Post' => array
(
'limit' => 2,
'fields' => array(
'Post.title', 'Post.content', 'Post.slug', 'Post.created',
'Tag.name',
'User.username', 'User.created', 'User.post_count', 'User.avatar_file_name'),
'joins' => array
(
array(
'table' => 'categories_posts',
'alias' => 'CategoriesPost',
'type' => 'inner',
'conditions'=> array('CategoriesPost.post_id = Post.id')
),
// FETCH USER
array(
'table' => 'users',
'alias' => 'User',
'type' => 'inner',
'conditions'=> array('Post.user_id = User.id')
),
// FETCH TAGS
array(
'table' => 'posts_tags',
'alias' => 'PostsTag',
'type' => 'inner',
'conditions'=> array('PostsTag.post_id = Post.id')
),
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'inner',
'conditions'=> array('Tag.id = PostsTag.tag_id')
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'inner',
'conditions'=> array('Category.id = CategoriesPost.category_id', 'Category.slug' => $slug)
)
)
)
);
$posts = $this->paginate();
could anyone gimme a solution since i'm a newbie?
many thanks...
I've run into a similar problem, when trying to paginate a resultset using an associated model. I ended up having to manually bind my models together, run the query, and then unbind them in order to get Cake to contain the right data together. ( http://book.cakephp.org/view/86/Creating-and-Destroying-Associations-on-the-Fly )
You could also try the containable behaviour which will allow you to specify which models you want to include in your result set. Containable is core in 1.2+ ( http://book.cakephp.org/view/474/Containable ), otherwise you'll need to grab it yourself.
I'm not too sure on why you have such a gargantuan query there though. I would be more inclined to do something similar to the following.
$this->Model->recursive = 2;
$this->Model->paginate();
And let Cake get all the related data for me through my associations. Then I would adjust the return using a conditions array ( http://api.cakephp.org/class/controller#method-Controllerpaginate ) to specify the category.
Sorry it's not a defacto solution, but I'm a CakePHP amateur myself! You might find it easier to view the queries, results etc, using DebugKit, http://github.com/cakephp/debug_kit