十月cms插件编写:多关系问题-类未找到


OctoberCMS plugin authoring: Problems with has-many relationships - class not found

我真的很努力让十月cms的关系工作在一个插件,我正在写。我有两个表:products和product_images。product和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']
    ];
    ...

我的产品模型的定义是这样的:

<?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/公共/供应商/10月/下雨/src/数据库/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。我把它打印出来以防万一。

有什么建议吗?

我解决了。我在我的belongsTo和hasMany定义中使用正斜杠而不是反斜杠:

老(破碎):

public $belongsTo = [
    'products' => ['Bt/Shop/Models/Products']
];
新(工作):

public $belongsTo = [
    'products' => ['Bt'Shop'Models'Products']
];

欢呼,Bret