Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

many-to-many relationship on kohana 3.2

I've got some trouble trying to make this work. I have 2 models, User_Pro and Category. I read the relationships documentation on kohana guide and I understand i need to define a $_has_many relationship on the User_Pro and Category models and create the model user_pro_categories with the belongs_to field.

User_Pro model:

protected $_has_many = array(
    'categories' => array(
        'model'   => 'TM_Category',
        'through' => 'user_pro_categories',
            'foreign_key' => 'id_user_pro',
    ),
);

Category model:

protected $_has_many = array(
    'users_pro' => array(
        'model'   => 'TM_User_Pro',
        'through' => 'user_pro_categories',
        'foreign_key' => 'id_category',
    ),
);

user_pro_categories model:

    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',
        ),
);

The error I'm getting is:

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' ]

It's like it doesn't care about the fk's i defined and it want's to use the suffix thing... any idea?

like image 554
Xavier Avatar asked Dec 16 '22 07:12

Xavier


1 Answers

Here is the example to give you quick idea how Kohana ORM works. And wish it will be helpful for others too.

Student Model

<?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
        ),
    );

}

Course Model

<?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 Script

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());
    }

Running code above will create following SQL query.

SELECT `course`.* FROM `courses` AS `course` JOIN `students_courses` ON (`students_courses`.`id_for_course` = `course`.`idcourse`) WHERE `students_courses`.`id_for_student` = '1'
like image 174
Kowser Avatar answered Jan 05 '23 10:01

Kowser