如何访问模型有多少关系与条件? [英] 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);
}
}
当使用它的某种方式如下:
When using it somehow like this:
$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.
我错过了什么吗?如何确保可用的_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.
请注意,这种关系需要是camelcase。所以在我的情况下,available_videos()应该是可用的()。
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:
// Illuminate\Database\Eloquent\Model.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.
这篇关于如何访问模型有多少关系与条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!