Yii Framework Forum: Check Foreign Key before save - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Check Foreign Key before save Rate Topic: -----

#1 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 17 May 2010 - 08:13 AM

Hey, I want to check ForeignKey before save, if data is not in the foreign table then get an error message. Can anyone help me how to do that?

here is the relation

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'role' => array(self::BELONGS_TO, 'Role', 'Role_id'),

);
}

public function create()
{
if(!isset($this->Role_id)) {
$this->addError('', 'Role is not set');
} else {
$this->Role_id = $this->role->id;
}

$this->save();
}
----

$this->Role_id = $this->role->id;
this is not working. Am I wrong?
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 17 May 2010 - 09:07 AM

You can use a CRequiredValidator instead of checking it, and for the value in the foreign table you can use a CExistValidator
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users