【发布时间】:2017-05-14 12:34:15
【问题描述】:
我是 OctoberCMS 的新手,我喜欢它的工作方式。目前我已经创建了两个插件,分别称为 Products 和 Product Categories。我使用Builder Plugin 创建了这些插件,这也是一个非常好的插件,可以轻松创建另一个插件。
现在的问题是,在我的 Products Categories 插件中,我只有一个名为 Product Category 的字段,用户可以添加任意数量的类别这个插件很好用。
在我的 Products 插件中,我有一个名为 Product Category 的字段,它是一个下拉字段,我想要我在 中创建的所有类别产品类别 插件,但不知何故我无法实现此功能。这是我到目前为止所尝试的。
Plugin.php(插件\technobrave\products)
<?php namespace Technobrave\Products;
use System\Classes\PluginBase;
use technobrave\productcategory\Models\ProductCategory as ProductCategory;
use technobrave\products\Models\Product as ProductModel;
class Plugin extends PluginBase
{
public function registerComponents()
{
}
public function registerSettings()
{
}
public function boot()
{
ProductModel::extend(function($model){
$model->hasOne['ProductCategory'] = ['technobrave\productcategory\Models\ProductCategory'];
});
}
}
Product.php(插件\technobrave\products\models)
<?php namespace Technobrave\Products\Models;
use Model;
/**
* Model
*/
class Product extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Validation
*/
public $rules = [
'category' => 'required',
'product_brand' => 'required',
'product_channel' => 'required',
'product_type' => 'required',
'client_name' => 'required',
'project_name' => 'required',
'product_description' => 'required',
'banner_image' => 'required',
'product_images' => 'required',
];
public $customMessages = [
'category.required' => 'Please Select Product Category',
'product_brand.required' => 'Please Select Product Brand',
'product_channel.required' => 'Please Select Product Channel',
'product_type.required' => 'Please Select Product Type',
'client_name.required' => 'Please Enter Client Name',
'project_name.required' => 'Please Enter Project Name',
'product_short_description.required' => 'Please Enter Product Short Description',
'product_description.required' => 'Please Enter Product Description',
'banner_image.required' => 'Please select Product Banner Image',
'product_images.required' => 'Please select Product Image(s)',
];
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
//public $timestamps = false;
/**
* @var string The database table used by the model.
*/
public $table = 'technobrave_products_';
public $settingsFields = 'fields.yaml';
public $attachOne = [
'banner_image' => 'System\Models\File'
];
public $attachMany = [
'product_images' => 'System\Models\File'
];
public $belongsTo = [
'ProductCategory'=> ['technobrave\productcategory\Models\ProductCategory']
];
// Here i want my product dropdown categories to be dynamic
public function getCategoryOptions()
{
echo '<pre>';
print_r($ProductCategory);
exit;
//return array();
}
}
但我不断收到致命错误消息:
未定义变量:ProductCategory
对于我放入 Product.php
的特定代码echo '<pre>';
print_r($ProductCategory);
exit;
在上面的 Plugin.php 文件中我有下面的代码
use technobrave\productcategory\Models\ProductCategory as ProductCategory;
所以通过这样做,我试图获取我已经创建的所有类别,并以某种方式试图在我的下拉列表中显示它。
我知道在 OctoberCMS 中创建插件时,我们可以处理 relations 逻辑(即 hasMany、hasOne 等),但现在我想以这种方式与外部插件。我想将这些类别添加到方法getCategoryOptions() 中,我将在我的下拉列表中返回这些类别。
如果可能的话,有人可以按照我想要的方式指导我吗?
谢谢
【问题讨论】:
标签: octobercms octobercms-plugins october-form-controller octobercms-backend