Kohana “has many” issue-Collection of common programming errors

I have a model named permission.

Permission has many roles permission has many users permission has many denied users

Below is from the permissions model:

protected $_has_many = array(
    'user' => array('through' => 'user_permission'),
    'permissiondeny' => array('model' => 'user', 'through' => 'user_permissiondeny','foreign_key' => 'permissiondeny_id'),
    'role' => array('through' => 'role_permission'),
);

The user and role relationships work as expected. I can select the deny permission, but when I try to add a new one I get the following php error:

PHP Fatal error: Uncaught Database_Exception [ 1110 ]: Column ‘permissiondeny_id’ specified twice [ INSERT INTO user_permissiondeny (permissiondeny_id, permissiondeny_id) VALUES (‘1’, 1) ] ~ MODPATH/database/classes/kohana/database/mysql.php [ 194 ]

Any suggestions as to what I’m missing?

  1. Since you are not following the naming conventions in ORM, you need to change your has_many declaration:

    protected $_has_many = array(
      'user' => array('through' => 'user_permission'),
      'permissiondeny' => array('model' => 'user', 'through' => 'user_permissiondeny','foreign_key' => 'permissiondeny_id', 'far_key' => 'permission_id'),
      'role' => array('through' => 'role_permission'),
    );
    

    note the far_key part, you need to specify the name of the column that you’ll be looking for. may it be user_id, role_id, permission_id, etc

Originally posted 2013-11-26 18:03:10.