V
V
vegarulez2015-07-30 20:31:08
css
vegarulez, 2015-07-30 20:31:08

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

}

And according to the logic of things, I should also create a User model - and connect it with my own - but the User model already exists.
I had to go into the folder with modules\orm\classes\model\auth\user\user.php
And there, in the user model, write the connection:
protected $_has_many = array(
    'user_tokens' => array('model' => 'User_Token'),
    'roles'       => array('model' => 'Role', 'through' => 'roles_users'),
        'types'       => array('model' => 'Type','through' => 'types_users'),
  );

And everything is up and running and working. But you can’t do it either, tell me how then to connect the user model and type, to climb into the modules folder.
------------------
lyrical digression - while writing the question, the idea occurred to me
to make a model extending the user class in the folder with models - and work with it already - which was done - but then he began to swear at the fact that he was looking for the id key by the name of the model + _id - and there is no such key in the tables. Next, I give the code of this model - tell me how to connect it correctly.
-------------------
Trying to make through
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

3 answer(s)
K
Kirill Kotov, 2018-12-27
@Dezzer

Using flex. I am attaching a good reference - https://html5book.ru/css3-flexbox/

V
vegarulez, 2015-07-30
@vegarulez

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

It seems to be up and running.
If you have something to add - please write. otherwise I study kohana for only a week - one and a half, and then raids.

I
Ivan, 2015-08-01
@dohlik

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 question

Ask a Question

731 491 924 answers to any question