S
S
Sergei Iamskoi2016-01-15 09:12:37
Yii
Sergei Iamskoi, 2016-01-15 09:12:37

How to make a connection in the form of fields, not an object in Yii?

I am learning Yii2. I read about connections, everything seems to be clear. But the question arose how to do the following thing:
There is a table users (id, phone, password, type), there are two related tables: profile_i (id_user, snils, passport) and profile_o (id_user, inn, kpp); Tables are linked depending on the type in users: if the type is one, then one, if the type is different, then another. But here's how to CORRECTLY implement so that access to the fields is not $user->profile->snils or $user->profile->inn, but like the main one: $user->inn, $user->kpp, $user -> snils etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-01-15
@p0vidl0

Declare the appropriate getters in the User class. PHPDoc can describe "virtual" properties so that the IDE doesn't warn you about using magic.

/**
...
 * @property string $inn
 * @property string $snils
 */
class User extends ActiveRecord 
{
...
    public function getInn()
    {
        return $this->profile->inn;
    }  
    
    public function getSnils ()
    {
        return $this->profile->snils ;
    }   
...
}

A
Alexander N++, 2016-01-15
@sanchezzzhak


so that there are no 101 requests, you need to use joinWith(['profile', 'profile.photos' ],true) when connecting the connection .
Then the main overgrowth will be with join and the links will be loaded through where IN ( ... )
I do not recommend implementing through getters, what you want to do,
you can do it like this
$profile = $user->profile;
$profile->inn;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question