Closed ghost closed 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
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>
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:
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)
And how can i check this relation? Excepting of this ofc:
So, what can you say, community?