如何访问模型 hasMany 与 where 条件的关系? [英] How to access model hasMany Relation with where condition?
问题描述
我使用关系的条件/约束创建了一个模型游戏,如下所示:
I created a model Game using a condition / constraint for a relation as follows:
class Game extends Eloquent {
// many more stuff here
// relation without any constraints ...works fine
public function videos() {
return $this->hasMany('Video');
}
// results in a "problem", se examples below
public function available_videos() {
return $this->hasMany('Video')->where('available','=', 1);
}
}
以某种方式使用它时:
$game = Game::with('available_videos')->find(1);
$game->available_videos->count();
一切正常,因为角色是结果集合.
everything works fine, as roles is the resulting collection.
我的问题:
当我尝试在不急切加载的情况下访问它时
when I try to access it without eager loading
$game = Game::find(1);
$game->available_videos->count();
抛出异常,因为它说在非对象上调用成员函数 count()".
an Exception is thrown as it says "Call to a member function count() on a non-object".
使用
$game = Game::find(1);
$game->load('available_videos');
$game->available_videos->count();
工作正常,但对我来说似乎很复杂,因为我不需要加载相关模型,如果我不在我的关系中使用条件.
works fine, but it seems quite complicated to me, as I do not need to load related models, if I do not use conditions within my relation.
我错过了什么吗?如何确保在不使用预先加载的情况下可以访问 available_videos?
Have I missed something? How can I ensure, that available_videos are accessible without using eager loading?
对于任何感兴趣的人,我也在 http://forums.laravel.io/viewtopic 上发布了这个问题.php?id=10470
For anyone interested, I have also posted this issue on http://forums.laravel.io/viewtopic.php?id=10470
推荐答案
以防万一其他人遇到同样的问题.
Just in case anyone else encounters the same problems.
请注意,关系必须是驼峰式的.所以在我的例子中 available_videos() 应该是 availableVideos().
Note, that relations are required to be camelcase. So in my case available_videos() should have been availableVideos().
你可以很容易地找到调查 Laravel 源代码:
You can easily find out investigating the Laravel source:
// IlluminateDatabaseEloquentModel.php
...
/**
* Get an attribute from the model.
*
* @param string $key
* @return mixed
*/
public function getAttribute($key)
{
$inAttributes = array_key_exists($key, $this->attributes);
// If the key references an attribute, we can just go ahead and return the
// plain attribute value from the model. This allows every attribute to
// be dynamically accessed through the _get method without accessors.
if ($inAttributes || $this->hasGetMutator($key))
{
return $this->getAttributeValue($key);
}
// If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice.
if (array_key_exists($key, $this->relations))
{
return $this->relations[$key];
}
// If the "attribute" exists as a method on the model, we will just assume
// it is a relationship and will load and return results from the query
// and hydrate the relationship's value on the "relationships" array.
$camelKey = camel_case($key);
if (method_exists($this, $camelKey))
{
return $this->getRelationshipFromMethod($key, $camelKey);
}
}
这也解释了为什么我的代码可以工作,每当我之前使用 load() 方法加载数据时.
This also explains why my code worked, whenever I loaded the data using the load() method before.
无论如何,我的示例现在可以正常运行,并且 $model->availableVideos 总是返回一个集合.
Anyway, my example works perfectly okay now, and $model->availableVideos always returns a Collection.
这篇关于如何访问模型 hasMany 与 where 条件的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!