Laravel 4 BelongsToMany sync()方法无法正常工作

时间:2013-07-31 01:13:36

标签: laravel laravel-4 eloquent

使用sync()方法时遇到问题。请帮帮我。

我有三张表格如下。

表:tbl_roles

+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id_role        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name           | varchar(60)      | NO   | UNI | NULL    |                |
| description    | text             | YES  |     | NULL    |                |
+----------------+------------------+------+-----+---------+----------------+

表:tbl_permissions

+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id_permission  | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name           | varchar(255)     | NO   |     | NULL    |                |
| description    | text             | YES  |     | NULL    |                |
+----------------+------------------+------+-----+---------+----------------+

表:tbl_link_roles_permissions

+---------------+------------------+------+-----+---------+-------+
| Field         | Type             | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| id_role       | int(10) unsigned | NO   | MUL | NULL    |       |
| id_permission | int(10) unsigned | NO   | MUL | NULL    |       |
+---------------+------------------+------+-----+---------+-------+

我在角色和权限模型之间创建了多对多关系。

角色模型:

class Role extends Eloquent
{
    /**
     * Permissions
     */
    public function permissions()
    {
        return $this->belongsToMany('Permission', 'tbl_link_roles_permissions', 'id_role', 'id_role');
    }
}

许可模式:

class Permission extends Eloquent
{
    /**
     * Roles
     */
    public function roles()
    {
        return $this->belongsToMany('Role', 'tbl_link_roles_permissions', 'id_permission', 'id_permission');
    }
}

当我运行以下代码来同步角色的权限时。它给我一个完整性约束违规错误。

守则:

    $role = Role::find($id_role);

    $permissions = Input::get('role_permissions');

    /* 
     * Permissions array is like array('1','2','3')
     * So I convert the value to integer.
     */
    $parameters = array();

    foreach($permissions as $permission) {
        $parameters[] = intval($permission);
    }

    $role->permissions()->sync($parameters);

根据错误消息,我发现SQL查询不正确。

错误meesage:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: 
a foreign key constraint fails (`quidz_host_laravel`.`tbl_link_roles_permissions`, 
CONSTRAINT `tbl_link_roles_permissions_id_permission_foreign` FOREIGN KEY 
(`id_permission`) REFERENCES `tbl_permissions` (`id_permission`)) (SQL: insert into 
`tbl_link_roles_permissions` (`id_role`) values (?)) (Bindings: array ( 0 => 1, ))

它只插入id_role。这不是我想要的。谁能告诉我我做错了什么?

非常感谢。

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。它是在构建关系时传递错误参数引起的。

关系应该像下面的代码:

class Role extends Eloquent
{
    /**
     * Permissions
     */
    public function permissions()
    {
        return $this->belongsToMany('Permission', 'tbl_link_roles_permissions', 'id_role', 'id_permission');
    }
}

class Permission extends Eloquent
{
    /**
     * Roles
     */
    public function roles()
    {
        return $this->belongsToMany('Role', 'tbl_link_roles_permissions', 'id_permission', 'id_role');
    }
}