kohana / orm

Kohana ORM
159 stars 108 forks source link

Relation existing checks in Model #112

Closed ghost closed 9 years ago

ghost commented 9 years ago

I want to check existing of some relations in my model. So, for example, we take a default User model. So, for check roles we should do this: ORM::factory('User', 1)->has('roles'). If user has at least 1 role, returns TRUE. So, next, i want to check existing of user tokens (remember me checkbox in authorization), so i do this: ORM::factory('User', 1)->has('user_tokens'). And this return me an exception, because it's unknown from which table we should select number of nodes for checking this relation.

So, we have next code in our model:

class Model_User extends Model_Auth_User 
{

    protected $_has_many = array(
        'user_tokens' => array(
            'model' => 'User_Token',
            'foreign_key' => 'user_id',
        ),
        'roles'  => array(
            'model' => 'Role',
        'through' => 'roles_users'
        )
    );

}

For correct works of that function (has()) we should add to 'user_tokens' relatio name of table to 'through' key, but, by database conception, this is not correct.

And my second question is: how to check one-to-one relation? For example, i has 'user_profile' table (which contains first_name, last_name, address and other none-value information): (the same model)

protected $_has_one = array(
    'profile' => array(
        'model' => 'User_Profile',
        'foreign_key' => 'user_id',
    )
);

And how can i check this relation? Excepting of this ofc:

ORM::factory('User', 1)->profile->count_all() > 0

So, what can you say, community?

dejwid commented 9 years ago

How about these four methods?: ORM::has ORM::has_any ORM::has_many ORM::has_one docs: http://kohanaframework.org/3.3/guide-api/ORM

ghost commented 9 years ago

Yea, all works fine, i print all of user relations by this code:

<table class="table table-responsive table-bordered table-hover">
    <tbody>
        <tr>
            <th>has_one:</th>
            <td>
                <?
                $hasOneRelations = $user->has_one();
                if(count($hasOneRelations) > 0)
                {
                    foreach($hasOneRelations as $name => $data)
                    {
                        echo $name;
                        echo ' => ';
                        echo $user->{$name}->loaded() > 0 ? 'yes' : 'no';
                        echo '<br>';
                    }
                }
                else
                {
                    echo 'Not exist that relations';
                }
                ?>
            </td>
        </tr>
        <tr>
            <th>has_many:</th>
            <td>
                <?
                $hasManyRelations = $user->has_many();
                if(count($hasManyRelations) > 0)
                {
                    foreach($hasManyRelations as $name => $data)
                    {
                        echo $name;
                        echo ' => ';
                        echo $user->{$name}->find()->loaded() ? 'yes' : 'no';
                        echo '<br>';
                    }
                }
                else
                {
                    echo 'Not exist that relations';
                }
                ?>
            </td>
        </tr>
        <tr>
            <th>belongs_to:</th>
            <td>
                <?
                $belongsToRelations = $user->belongs_to();
                if(count($belongsToRelations) > 0)
                {
                    foreach($belongsToRelations as $name => $data)
                    {
                        echo $name;
                        echo ' => ';
                        echo $user->{$name}->find()->loaded() ? 'yes' : 'no';
                        echo '<br>';
                    }
                }
                else
                {
                    echo 'Not exist that relations';
                }
                ?>
            </td>
        </tr>
    </tbody>
</table>