【发布时间】:2013-12-27 18:34:33
【问题描述】:
我在 wordpress 插件中使用 Laravel 的 Eloquent。
产品型号:
<?php namespace GD;
use Country;
class Product extends \Illuminate\Database\Eloquent\Model
{
public function country()
{
return $this->belongsTo('Country', 'CountryId');
}
}
国家模式:
<?php namespace GD;
use Product;
class Country extends \Illuminate\Database\Eloquent\Model
{
public function products()
{
return $this->hasMany('Product');
}
}
我可以使用标准 Laravel 语法查询任何模型:
$products = $this->product->where('MetalId', '=', 1)
->where('ProductTypeId', '=', '2')
->orderBy('Name')->orderBy('CountryId')
->get();
但是我无法急切/延迟加载相关模型:
$products = $this->product->with('country')->where('MetalId', '=', 1)
->where('ProductTypeId', '=', '2')
->orderBy('Name')->orderBy('CountryId')
->get();
错误信息
Fatal error: Class 'Country' not found in .../vendor/illuminate/database/Illuminate/Database/Eloquent/Model.php on line 593
所以我认为这一定是命名空间问题,所以我将模型代码更新为:
return $this->belongsTo('\\GD\\Country', 'CountryId');
and
return $this->hasMany('\\GD\\Product');
但是,当我在产品模型上运行查询并转储结果时,我得到:
["relations":protected]=>
array(1) {
["country"]=>
NULL
}
【问题讨论】:
-
您是否配置了类自动加载器?如果另一个插件也包含 Eloquent 文件,你将如何处理这种情况 - 特别是如果它是 Eloquent 的不同版本?
标签: wordpress laravel lazy-loading eloquent eager-loading