Laravel 5 动态变形方法
Laravel 5 Dynamic morph method
我正在构建一个严重依赖变形关系的包。与此关系一样,需要像这样定义关系:
public function foos()
{
return $this->morphToMany('App\Models\Foo', 'barable');
}
这显然工作正常,这里没有问题。
事实上,有许多这样的关系需要定义。我只想循环遍历它们并自动构建它们,以便更轻松地配置包。
我试过以下方法:
public function __get($name)
{
if($name == 'foos') {
return $this->morphToMany('App\Models\Foo', 'barable');
}
}
这不会启动检索数据的查询。它被调用,但没有 return 数据。
__call 函数对我来说似乎也是合乎逻辑的,但这只是破坏了 Laravel。据我所知,它会拾取 class.
中调用的所有内容
现在的替代方法是包含特征并让程序员在可发布文件中填写这些关系,但感觉不对。
有什么建议吗?
原来这是一个两步回答。您需要一个针对预加载的修复程序和一个针对延迟加载的修复程序。
急切加载程序采用 model.php 中指定的 __call() 函数,如果语句失败则重定向到它。
public function __call($method, $arguments){
if(in_array($method, ['bars'])) {
return $this->morphToMany('App\Bar', 'barable');
}
return parent::__call($method, $arguments);
}
惰性加载程序检查方法是否存在,显然不存在。将它添加到您的模型并添加 "OR" 语句将使这些工作也有效。原函数也驻留在model.php中。添加:
|| in_array($key, $this->morphs)
将使函数按预期工作,从而导致:
public function getRelationValue($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 ($this->relationLoaded($key)) {
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.
if (method_exists($this, $key) || in_array($key, $this->morphs)) {
return $this->getRelationshipFromMethod($key);
}
}
我正在构建一个严重依赖变形关系的包。与此关系一样,需要像这样定义关系:
public function foos()
{
return $this->morphToMany('App\Models\Foo', 'barable');
}
这显然工作正常,这里没有问题。
事实上,有许多这样的关系需要定义。我只想循环遍历它们并自动构建它们,以便更轻松地配置包。
我试过以下方法:
public function __get($name)
{
if($name == 'foos') {
return $this->morphToMany('App\Models\Foo', 'barable');
}
}
这不会启动检索数据的查询。它被调用,但没有 return 数据。
__call 函数对我来说似乎也是合乎逻辑的,但这只是破坏了 Laravel。据我所知,它会拾取 class.
中调用的所有内容现在的替代方法是包含特征并让程序员在可发布文件中填写这些关系,但感觉不对。
有什么建议吗?
原来这是一个两步回答。您需要一个针对预加载的修复程序和一个针对延迟加载的修复程序。
急切加载程序采用 model.php 中指定的 __call() 函数,如果语句失败则重定向到它。
public function __call($method, $arguments){
if(in_array($method, ['bars'])) {
return $this->morphToMany('App\Bar', 'barable');
}
return parent::__call($method, $arguments);
}
惰性加载程序检查方法是否存在,显然不存在。将它添加到您的模型并添加 "OR" 语句将使这些工作也有效。原函数也驻留在model.php中。添加:
|| in_array($key, $this->morphs)
将使函数按预期工作,从而导致:
public function getRelationValue($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 ($this->relationLoaded($key)) {
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.
if (method_exists($this, $key) || in_array($key, $this->morphs)) {
return $this->getRelationshipFromMethod($key);
}
}