Hello everyone,
I have a problem and I hope anyone of you can help me with that.
Certainly I have read a lot of topics about hasMany and the likes, but
so far I couldn't find the answer I need.
Let me start by explaining what I want:
For now I have 2 models: message.php and user.php
[code]
class Message extends AppModel {
var $name = 'Message';
var $belongsTo = array('User' =>
array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
/*
AND
*/
class User extends AppModel
{
var $name = 'User';
var $hasMany = array(
'Message' =>array(
'className' => 'Message',
'conditions' => 'User.id =
Message.from_id',
'foreignKey' => 'user_id'
)
);
}
[/code]
Ok sofar for the models.
The thing I want is to do a select on the messages table getting all
the messages send to a certain id. For now with this setup it's
possible. However.. I want to get the username who have sent the
message.
I tried to do it with a ordinary query on my db and that worked:
[sql code]
SELECT m.user_id, m.from_id, m.subject, m.content, u.username
FROM users u, messages m
WHERE u.id = m.from_id
AND m.user_id = 1
[/sql code]
outputs:
user_id from_id subject content username
2 1 subject contents XYZ
Which is the result that I want.
Here's the controller code:
[code]
var $uses=array('Message','User');
function xyz()
{
$message = $this->Message->findAll('Message.user_id=1');
pr($message);
}
[/code]
cake gives me back this:
[array output snippet]
[0] => Array
(
[Message] => Array
(
[id] => 3
[user_id] => 1
[from_id] => 2
[created] => 2007-01-14 13:53:53
[subject] => 2nd message from test to username
[content] => bdfiugfigsdifgisudgfiudsf
fljhfughofdhpgifhoghdfg
sdfghfsoguhodhfgohfdohg
sdlghfdouhgodfhgohdfohodf
)
[User] => Array
(
[id] => 1
[useremail] =>
[username] => username
[userpassword] => #########
[userfirstname] => first
[userlastname] => last
[created] => now()
[delete_date] => 0000-00-00 00:00:00
[ip] =>
)
)
[/array output snippet]
Which is not what I want. I want to get the username and the messages
this 'from_id' sent.
So my question.. how do I get cake to give me back the same output as
the ordinary query?
Hope anyone can help me with this.
Thanks in advance!
Greets Ed
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---