Monday, December 28, 2009

Re: convention clarifications, and joins with cakephp

you can setup your model to look for any field as foreign key...
just do that:

in your case, User hasMany Mail
and Mail belongsTo User

in your user model:

var $hasMany = array(
'MailTo' => array(
'className' => 'Mail',
'foreignKey' => 'to'
),
'MailFrom' => array(
'className' => 'Mail',
'foreignKey' => 'from'
)
);

in your mail model:

var $belongsTo = array(
'UserTo' => array(
'className' => 'User',
'foreignKey' => 'id'
),
'UserFrom' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);


by convention, primary key should be named just ID, not table_id... the foreign key field should be named table_id...
but, of course, you can change it...

--
Renato de Freitas Freire
renato@morfer.org


On Mon, Dec 28, 2009 at 11:20 AM, Jaimon <kal.lizkor@gmail.com> wrote:
hi,
just few days ago i found out about this miracle called cakePHP so i
am pretty green to it.
i need to build a mail application, so i have followed the convention
and created:

data base description:
table of users <user_id (primary key), fname, lname>.
table of mails <mail_id(primary key), from (foreign key to user_id),
to (foreign key to user_id), content, opened>

my questions:
1) according to the convention, a foreign key should be called related
table+'_id'. how should i call the columns if there are two foreign
keys that relate to the same table. like from and to in the mails
table.

2) i would like to do an inner JOIN the between the two table.
something like
SELECT user_id, mail_id
FROM users
INNER JOIN mails
ON users.user_id =mails.to AND mails.opened=false.

but i have no clue how to do it.

please advise, thanks






Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: