OctoberCMS 插件创作:有很多关系的问题 - class 未找到
OctoberCMS plugin authoring: Problems with has-many relationships - class not found
我真的很难让 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 'ProductImages' not found
/var/www/mysite/public/vendor/october/rain/src/Database/Model.php line
772
我相信当定义 Product hasMany 关系时,代码不知何故不知道 ProductImages class。 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']
];
干杯,
布雷特
我真的很难让 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 'ProductImages' not found
/var/www/mysite/public/vendor/october/rain/src/Database/Model.php line 772
我相信当定义 Product hasMany 关系时,代码不知何故不知道 ProductImages class。 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']
];
干杯, 布雷特