首页 文章

OctoberCMS调用另一个插件's data in current plugin'的下拉列表

提问于
浏览
0

我是OctoberCMS的新手,我喜欢它的工作方式 . 目前我已经创建了两个名为 ProductsProduct Categories 的插件 . 我使用Builder Plugin创建了这些插件,这也是一个非常好的插件,可以轻松创建另一个插件 .

现在的问题是,在我的 Products Categories 插件中,我只有一个名为 Product Category 的字段,用户将添加他/她想要的多个类别,这个插件工作正常 .

在我的 Products 插件中,我有一个名为 Product Category 的字段,这是一个下拉字段,我想要分别在 Product Categories 插件中创建的所有类别,但不知怎的,我无法实现此功能 . 这是我到目前为止所尝试的 .

Plugin.php (plugins \ technobrave \产品)

<?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 (plugins \ 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() 中添加要填写的那些类别,我将在我的下拉列表中返回这些类别 .

有人可以按照我想要的方式指导我吗?

谢谢

1 回答

  • 1

    好 . 我找到了两种方法可以实现这一点,下面是这些 .

    Way One

    Product.php (plugins \ 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'); 并返回它以便能够在我的下拉列表中填充动态类别 . 这很好用 .

    Way Two

    Product.php (plugins \ 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() 中编写了一个查询并获得了记录 .

    您可以使用您喜欢的任何方法 . 另外,如果我找到更好的方法来实现同样的事情,那将会很棒 .

    谢谢

相关问题