OctoberCMS在当前插件的下拉列表中调用另一个插件的数据 [英] OctoberCMS call another plugin's data in current plugin's dropdown

查看:98
本文介绍了OctoberCMS在当前插件的下拉列表中调用另一个插件的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OctoberCMS的新手,我喜欢它的工作方式.目前,我已经创建了两个名为产品产品类别的插件.我使用 Builder插件创建了这些插件,这也是一个很好的插件,可以使用舒适.

I am new to OctoberCMS and i love the way it works. Currently i have created two plugins called as Products and Product Categories. I have created these plugins using Builder Plugin which is also a very good plugin to create another plugins with ease.

现在的事情是,在我的产品类别插件中,我仅拥有一个名为产品类别的字段,用户将根据需要添加任意多个类别并且此插件可以正常工作.

Now the thing is, In my Products Categories plugin, i simply have a single field called as Product Category and user will add as many categories as he/she wants and this plugin works fine.

在我的产品插件中,我有一个名为产品类别的字段,这是一个下拉字段,我希望我在中创建的所有这些类别产品类别插件,但是我无法以某种方式实现此功能.到目前为止,这是我尝试过的.

And in my Products plugin, i have a field called as Product Category which is a dropdown field and i want all those categories which i have created in Product Categories plugins respectively but somehow i am unable to implement this feature. This is what i have tried so far.

Plugin.php (插件\ technobrave \产品)

Plugin.php (plugins\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 \产品\模型)

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();
    }

}

但是我一直在收到致命错误消息,说:

But i am keep getting fatal error message saying:

未定义变量:ProductCategory

Undefined variable: ProductCategory

对于我放入 Product.php

echo '<pre>';
print_r($ProductCategory);
exit;

Plugin.php 文件的上方,我有下面的代码

in above Plugin.php file i have below code

use technobrave\productcategory\Models\ProductCategory as ProductCategory;

因此,通过这样做,我试图获取我已经创建的所有类别,并以某种方式尝试在下拉列表中显示它.

so by doing this, i am trying to get all the categories which i have already created and somehow trying to show it in my dropdown.

我在 OctoberCMS 中知道在创建插件时,我们可以处理关系逻辑(即hasMany,hasOne等),但现在我想通过外部实现这种方式插件.我想将这些类别添加到方法getCategoryOptions()中,我将在我的下拉列表中返回这些类别.

I know in OctoberCMS while creating plugins, we can deal with relations logic (i.e. hasMany, hasOne etc) but for now i want to achieve in this way with external plugins. I want add those categories to be filled in the method getCategoryOptions() and i will return those in my dropdown.

如果可以的话,有人可以指导我吗?

Can someone guide me if its possible in the way which i want ?

谢谢

推荐答案

好.我发现了两种方法可以实现这一目标,下面是这些方法.

Ok. I have found out two ways to be able to achieve this and here below are those.

一路通行

Product.php (插件\ technobrave \产品\模型)

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');并返回了它,以便能够在下拉菜单中填充动态类别.这很好.

Here above, i have just used use technobrave\productcategory\Models\ProductCategory as ProductCategory; and in my method getCategoryOptions(), i have just added this ProductCategory::lists('category_name','id'); and returned it to be able to fill dynamic categories in my dropdown. This works well.

第二种方式

Product.php (插件\ technobrave \产品\模型)

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

Here above, i simply written a query in my method getCategoryOptions() and got the records.

您可以使用自己喜欢的任何一种方法.此外,如果我能找到更好的方法来实现同一件事,那就太好了.

You can use whichever method which you prefer. Additionally, it would be great if i find better ways to implement the same thing.

谢谢

这篇关于OctoberCMS在当前插件的下拉列表中调用另一个插件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆