我真的很难让OctoberCMS关系在我正在编写的插件中工作。我有两个表:products和product_images。产品和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']
];
我得到的错误是:
Class&#39; ProductImages&#39;找不到
/var/www/mysite/public/vendor/october/rain/src/Database/Model.php line 772
我相信当Product定义了很多关系时,代码不知道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。我把它打印出来以确定。
有什么建议吗?
答案 0 :(得分:1)
我解决了。我在belongsTo中使用正斜杠而不是反斜杠,并且有多个定义:
旧(坏):
public $belongsTo = [
'products' => ['Bt/Shop/Models/Products']
];
新(工作):
public $belongsTo = [
'products' => ['Bt\Shop\Models\Products']
];
干杯, 布雷特