Hi,
I have three tables in Mysql DB.
- user: _id as PK
- role: _id as PK
- user_role: _id as AI user_id FK to user role_id FK to role PRIMARY KEY(user_id, role_id)
Models:
User
$this->hasMany('_id', 'UserRole', 'user_id', array('alias' => 'UserRole'));
Role
$this->hasMany('_id', 'UserRole', 'role_id', array('alias' => 'UserRole'));
UserRole
- $this->belongsTo('role_id', 'Role', '_id', array('alias' => 'Role'));
- $this->belongsTo('user_id', 'User', '_id', array('alias' => 'User'));
$model = new UserRole();
$model->user_id =1;
$model->role_id = 1;
$model->create();
/*
Which throws error:
Phalcon\Mvc\Model\Message Object
(
[_type:protected] => InvalidCreateAttempt
[_message:protected] => Record cannot be created because it already exists
[_field:protected] =>
[_model:protected] =>
)
*/
NOTE: user_role table is empty
Can any one plz suggest to overcome this error and how to define the relations in my above scenario.
thanks in advance.