> 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
No comments:
Post a Comment