我在尝试做这项工作时遇到了一些麻烦。我有2个模型,User_Pro和Category。我阅读了kohana指南中的关系文档,我知道我需要在User_Pro和Category模型上定义$_has_many
关系,并使用belongs_to字段创建model_user_pro_categories。
kohana的多对多关系3.2
User_Pro型号:
protected $_has_many = array(
'categories' => array(
'model' => 'TM_Category',
'through' => 'user_pro_categories',
'foreign_key' => 'id_user_pro',
),
);
分类模型:
protected $_has_many = array(
'users_pro' => array(
'model' => 'TM_User_Pro',
'through' => 'user_pro_categories',
'foreign_key' => 'id_category',
),
);
user_pro_categories型号:
protected $_belongs_to = array(
'user_pro' => array(
'model' => 'TM_User_Pro',
'foreign_key' => 'id_user_pro',
),
'category' => array(
'model' => 'TM_Category',
'foreign_key' => 'id_category',
),
);
我得到的错误是:
Database_Exception [ 1054 ]: Unknown column 'tm3_user_pro_categories.category_id' in
'on clause' [ SELECT `tm3_tm_category`.* FROM `tm3_categories` AS `tm3_tm_category` JOIN
`tm3_user_pro_categories` ON (`tm3_user_pro_categories`.`category_id` = `tm3_tm_category`.`id_category`)
WHERE `tm3_user_pro_categories`.`id_user_pro` = '2' ]
这就像它不关心我定义的fk,它想要使用后缀的东西......任何想法?
您是否在模型中声明了正确的主键? 'protected $ _primary_key ='strange_primary_key';' – Thorsten