2011-11-21 68 views
4

我在嘗試做這項工作時遇到了一些麻煩。我有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,它想要使用後綴的東西......任何想法?

+0

您是否在模型中聲明瞭正確的主鍵? 'protected $ _primary_key ='strange_primary_key';' – Thorsten

回答

11

下面是讓您快速瞭解Kohana ORM如何工作的示例。並希望它也會對其他人有所幫助。

學生模型

<?php defined('SYSPATH') or die('No direct script access.'); 

class Model_Student extends ORM { 

    protected $_primary_key = 'idstudent'; // take a look 

    protected $_has_many = array(
     'courses'=> array(
      'model' => 'course',    // Course model 
      'through' => 'students_courses', // many-to-may through 
      'far_key' => 'id_for_course',  // "column name" relating to the Course Model in "students_courses" table 
      'foreign_key' => 'id_for_student' // "column name" relating to the Student Model in "students_courses" table 
     ), 
    ); 

} 

課程模式

<?php defined('SYSPATH') or die('No direct script access.'); 

class Model_Course extends ORM { 

    protected $_primary_key = 'idcourse'; // take a look 

    protected $_has_many = array(
     'students'=> array(
      'model'   => 'student', 
      'far_key'  => 'id_for_student', 
      'through'  => 'students_courses', 
      'foreign_key' => 'id_for_course' 
     ), 
    ); 

} 

SQL腳本

CREATE TABLE IF NOT EXISTS `students` (
    `idstudent` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `name` varchar(45) DEFAULT NULL, 
    PRIMARY KEY (`idstudent`) 
) ENGINE=MyISAM; 

INSERT INTO `students` (`idstudent`, `name`) VALUES 
(1, 's1'), 
(2, 's2'); 
/* column idcourse and PR idcourseS ? */ 
CREATE TABLE IF NOT EXISTS `courses` (
    `idcourse` int(11) NOT NULL, 
    `name` varchar(45) DEFAULT NULL, 
    PRIMARY KEY (`idcourse`) 
) ENGINE=MyISAM; 

INSERT INTO `courses` (`idcourse`, `name`) VALUES 
(1, 'c1'), 
(2, 'c2'), 
(3, 'c3'); 

CREATE TABLE IF NOT EXISTS `students_courses` (
    `id_for_student` int(10) unsigned NOT NULL, 
    `id_for_course` int(10) unsigned NOT NULL 
) ENGINE=MyISAM; 

INSERT INTO `students_courses` (`id_for_student`, `id_for_course`) VALUES 
(1, 1), 
(1, 3); 

$student = new Model_Student(1); 
    $courses = $student->courses->find_all(); 
    echo Debug::vars($courses); 
    foreach($courses as $course) { 
     echo Debug::vars($course->object()); 
    } 

上面的運行代碼將創建以下SQL查詢。

SELECT `course`.* FROM `courses` AS `course` JOIN `students_courses` ON (`students_courses`.`id_for_course` = `course`.`idcourse`) WHERE `students_courses`.`id_for_student` = '1' 
+1

非常感謝! far_key字段正是我需要運行的。 – Xavier

1

您不需要爲多對多關係的數據透視表創建模型。只需在兩個模型中定義through選項,確保您的主鍵/外鍵遵循Kohana慣例,並且您準備好了。下面是來自Kohana ORM用戶和角色模型的示例:

class Model_User 
{ 
    protected $_has_many = array(
     'roles' => array('model' => 'role', 'through' => 'roles_users'), 
    ); 
} 

class Model_Role 
{ 
    protected $_has_many = array(
       'users' => array('model' => 'user', 'through' => 'roles_users') 
     ); 
}