Thursday, November 25, 2010

RE: Table / Role / Profile Relations

Sorry 1 last thing.

Model in User, is it needed since in my setup User has role_id which would
be the same as model (in my case of role is Buyer User.model is also Buyer)
they would never be different. Each role has completely different
controllers / actions thru routing so knowing the Model to swap it out in
the code for ($this->User->{$model}-> would never be used since Buyer would
always be Buyer and Seller would always be Seller so in my example model
field would not really be needed if im following your example correct?

-----Original Message-----
From: cricket [mailto:zijn.digital@gmail.com]
Sent: November-25-10 3:35 PM
To: cake-php@googlegroups.com
Subject: Re: Table / Role / Profile Relations

On Thu, Nov 25, 2010 at 1:34 PM, Dave Maharaj <me@davemaharaj.com> wrote:
> Sounds good. I think I found your post to an earlier similar question
>
>
http://cakephp.1045679.n5.nabble.com/model-for-different-role-users-td132610
> 6.html
>
>
> How do you set up the "foreign_key" for example
> Seller has id: 5 user_id: 15  so User model would be seller but
foreign_key?
> Id => 15?

The foreign_key is the id of the other model. So:

id => 15
model => Seller
foreign_key => 5

Here's some code from one of my apps.

User:

public $hasOne = array(
'Member' => array(
'className' => 'Member',
'foreignKey' => 'user_id',
'dependent' => true
),
'Affiliate' => array(
'className' => 'Affiliate',
'foreignKey' => 'user_id',
'dependent' => true
),
'Administrator' => array(
'className' => 'Administrator',
'foreignKey' => 'user_id',
'dependent' => true
)
);

Member:

public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Country',
'Region'
);

UsersController:

function admin_add($model = null)
{
if (!empty($this->data))
{
$model = $this->data['User']['model'];

if ($this->User->{$model}->save($this->data))
{
$foreign_key =
$this->User->{$model}->getLastInsertID();
$this->data['User']['foreign_key'] = $foreign_key;

$password = $this->_createPassword();
$this->data['User']['password'] = $password['hash'];

$this->User->create();

if ($this->User->save($this->data))
{
$user_id = $this->User->getLastInsertID();

$this->User->{$model}->saveField('user_id',
$user_id);

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: