我真的很难让 OctoberCMS 关系在我正在编写的插件中工作。我有两个表:products 和 product_images。products 和 product_images 之间存在一对多的关系。
在我的产品模型中,我有:
public $hasMany = [
'product_images' => ['Bt/Shop/Models/ProductImages']
];
我有一个名为 ProductImages 的模型,位于 plugins/bt/shop/models/ProductImages.php 中。模型定义为:
<?php namespace Bt\Shop\Models;
use Model;
class ProductImages extends Model
{
public $table = 'bt_shop_product_images';
protected $dates = ['published_at'];
public static $allowedSortingOptions = array(
'name asc' => 'Name (ascending)',
'name desc' => 'Name (descending)',
'updated_at asc' => 'Updated (ascending)',
'updated_at desc' => 'Updated (descending)',
'published_at asc' => 'Published (ascending)',
'published_at desc' => 'Published (descending)',
);
public $preview = null;
public $belongsTo = [
'products' => ['Bt/Shop/Models/Products']
];
...
我的 Products 模型的定义如下所示:
<?php namespace Bt\Shop\Models;
use Model;
class Products extends Model
{
public $table = 'bt_shop_products';
protected $dates = ['published_at'];
public static $allowedSortingOptions = array(
'name asc' => 'Name (ascending)',
'name desc' => 'Name (descending)',
'updated_at asc' => 'Updated (ascending)',
'updated_at desc' => 'Updated (descending)',
'published_at asc' => 'Published (ascending)',
'published_at desc' => 'Published (descending)',
);
public $preview = null;
public $hasMany = [
'product_images' => ['Bt/Shop/Models/ProductImages']
];
我得到的错误是:
找不到类“ProductImages”
/var/www/mysite/public/vendor/october/rain/src/Database/Model.php 第 772 行
我相信在定义 Product hasMany 关系时,代码不知何故不知道 ProductImages 类。Model.php 中的代码,第 772 行是:
public function hasMany($related, $primaryKey = null, $localKey = null, $relationName = null)
{
if (is_null($relationName))
$relationName = $this->getRelationCaller();
$primaryKey = $primaryKey ?: $this->getForeignKey();
$localKey = $localKey ?: $this->getKeyName();
$instance = new $related;
return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$primaryKey, $localKey, $relationName);
}
在我的例子中,名为 $related 的变量等于 Bt/Shop/Models/ProductImages。我打印出来确定。
有什么建议么?