Answer the question
In order to leave comments, you need to log in
[ORM] How to connect tables users and types by has_many?
Hi all! Tell me the answer to this question - I read manuals on kohan everywhere and I can’t find anywhere how to do it right - everywhere in the examples there is only an article, authors of films and so on. I already understood the essence of forming a model orm for tables and relationships with other tables. But nowhere is it discussed how to properly organize the relationship if I have a field in the users table type_id field
in which I make a comparison with the types['id','name'] table through the types_users ['user_id', 'type_id'] table and the relationship has_many.
I created a model for the types table in the models folder
<?php
defined('SYSPATH') OR die('No direct access allowed.');
class Model_Type extends ORM {
protected $_table_name = 'types';
protected $_has_many = array(
'users' => array('model' => 'User','through' => 'types_users'),
);
}
protected $_has_many = array(
'user_tokens' => array('model' => 'User_Token'),
'roles' => array('model' => 'Role', 'through' => 'roles_users'),
'types' => array('model' => 'Type','through' => 'types_users'),
);
class Model_Usertype extends Model_User{
protected $_table_name = 'users';
protected $_has_many = array(
'types' => array('model' => 'Type','through' => 'types_users'),
);
}
Answer the question
In order to leave comments, you need to log in
Using flex. I am attaching a good reference - https://html5book.ru/css3-flexbox/
And how does this affect the fact that he is looking for a key with the model name + _id i.e. usertype_id.
The error didn't go away.
You can write
protected $_primary_key = 'user_id';
but then he already does not find it in the table yuzers.
----------------------------------
another lyrical digression - already answered the question - had to edit - comments can not be added here after the answer I
found the solution:
has_many as it was and remained - added foreign key
<?php
/**
* Created by PhpStorm.
* User: alexeyv
* Date: 31/7/2558
* Time: 0:15 น.
*/
defined('SYSPATH') OR die('No direct access allowed.');
class Model_Usertype extends Model_User{
protected $_table_name = 'users';
protected $_primary_key = 'id';
protected $_has_many = array(
'types' => array('model' => 'Type','through' => 'types_users', 'foreign_key' => 'user_id',),
);
}
Please note that all the functionality of the Model_User model is actually located in Model_Auth_User, and only inheritance is in Model_User. Copy Model_User to application and add the necessary connections, methods, etc. there. One BUT - if you add the $_has_many property to Model_User, it will overwrite the parent property. There are two ways out:
1. Copy properties from Model_Auth_User and add your own. This is a feature of the Kohana Cascading File System, you can read it in the official dock or here (or somewhere else on the Internet, in Russian about the basics of Kohana, everything has long been chewed up).
2. Properties can be added dynamically, look at the _initialize() method which is called for any ORM model. In your case it will be something like:
protected function _initialize()
{
// сперва пусть отработают родители
parent::_initialize();
$this->_has_many['types'] = array('model' => 'Type','through' => 'types_users');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question