【问题标题】:OctoberCMS call another plugin's data in current plugin's dropdownOctoberCMS 在当前插件的下拉列表中调用另一个插件的数据
【发布时间】:2017-05-14 12:34:15
【问题描述】:

我是 OctoberCMS 的新手,我喜欢它的工作方式。目前我已经创建了两个插件,分别称为 ProductsProduct 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


    【解决方案1】:

    好的。我已经找到了两种方法来实现这一点,下面是那些。

    单向

    Product.php(插件\technobrave\products\models)

    <?php namespace Technobrave\Products\Models;
    
    use Model;
    use technobrave\productcategory\Models\ProductCategory as ProductCategory;
    
    public function getCategoryOptions()
        {      
            $fields =  ProductCategory::lists('category_name','id');         
            print_r($fields);
        }
    

    在上面,我刚刚使用了use technobrave\productcategory\Models\ProductCategory as ProductCategory; 和我的方法getCategoryOptions(),我刚刚添加了这个ProductCategory::lists('category_name','id'); 并返回它以便能够在我的下拉列表中填充动态类别。这很好用。

    方法二

    Product.php(插件\technobrave\products\models)

    <?php namespace Technobrave\Products\Models;
    
    use Model;
    use technobrave\productcategory\Models\ProductCategory as ProductCategory;
    
        public function getCategoryOptions()
        { 
            // getting only active categories
           $get_categories = ProductCategory::all()->where('status',1);    
    
           $fields[''] = 'Select Product Category';
           foreach ($get_categories as $current_category) {
                $fields[$current_category->attributes['id']] = $current_category->attributes['category_name'];
           }
           print_r($fields);
    
    
        }
    

    在上面,我只是在我的方法getCategoryOptions() 中编写了一个查询并获取了记录。

    您可以使用您喜欢的任何方法。此外,如果我能找到更好的方法来实现同样的事情,那就太好了。

    谢谢

    【讨论】:

      猜你喜欢
      • 2017-04-30
      • 2018-07-31
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2017-03-20
      • 1970-01-01
      相关资源
      最近更新 更多