使用Prestashop的后台将一个项目与多个(不同类的)其他项目相关联 [英] Associating an item to multiple other items (of a different class) using Prestashop's backoffice

查看:93
本文介绍了使用Prestashop的后台将一个项目与多个(不同类的)其他项目相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚到达Prestashop 1.5,我正在制作一个非常简单的模块:一周的视频,与需要在其旁边显示的多种产品相关.

Having just arrived at Prestashop 1.5, I am making a very simple module: a video of the week, associated with multiple products that need to appear right next to it.

我决定从后台开始.现在,我可以查看,添加,编辑和删除所有视频"条目,但是我对如何映射视频及其相关产品之间的NN关联有点迷茫……缺少文档也无济于事.

I decided to start from the Backoffice. Right now, I can view, add, edit and remove all the Video entries but I'm a bit lost on how to map the N-N association between a video and its related products... The lack of documentation isn't helping either.

有什么想法可以实现这一目标吗?

Any ideas how to pull this off?

这是我的一些代码,Video类的定义是:

Here's a bit of my code, the Video class is defined by:

class Video extends ObjectModel {

    public $id_video;
    public $title;
    public $url;
    public $active;

    public static $definition = array(
        'table'     => 'video',
        'primary'   => 'id_video',
        'multilang' => false,
        'fields' => array(
            'id_video' => array(
                'type' => ObjectModel :: TYPE_INT
            ),
            'title' => array(
                'type' => ObjectModel :: TYPE_STRING, 
                'required' => true
            ),
            'url' => array(
                'type' => ObjectModel :: TYPE_STRING, 
                'required' => true
            ), 
            'active' => array(
                'type'      => ObjectModel :: TYPE_BOOL, 
                'required'  => true
            )
        ),
    );

(...)

和AdminVideo类在这里:

and the AdminVideo class is here:

class AdminVideoController extends ModuleAdminController {

    public function __construct()
    {
        $this->table = 'video';
        $this->className = 'Video';
        $this->lang = false;

        $this->fields_list['id_video'] = array(
            'title' => $this->l('ID'),
            'align' => 'center',
        );

        $this->fields_list['title'] = array(
            'title' => $this->l('Title'),
            'width' => 'auto'
        );

        $this->fields_list['url'] = array(
            'title' => $this->l('URL'),
            'width' => 'auto'
        );

        $this->fields_list['active'] = array(
            'title' => $this->l('Active'),
            'width' => '70',
            'align' => 'center',
            'active' => 'status',
            'type' => 'bool',
            'orderby' => false
        );

        parent::__construct();
    }

    public function postProcess()
    {
         parent::postProcess();
    }

    public function renderList()
    {
        $this->addRowAction('edit');
        $this->addRowAction('delete');
        $this->addRowAction('details');

        return parent::renderList();
    }

    public function renderForm()
    {
        if (!($obj = $this->loadObject(true)))
            return;

        $this->fields_form = array(
            'legend' => array(
                'title' => $this->l('This weeks video'),
                'image' => '../img/admin/world.gif'
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('Nome'),
                    'name' => 'title',
                    'size' => 33,
                    'required' => true,
                    'desc' => $this->l('Title')
                ),
                array(
                      'type' => 'text',
                      'label' => $this->l('URL'),
                      'name' => 'url',
                      'size' => 33,
                      'required' => true,
                      'desc' => $this->l('Video URL')
                ),
                array(
                    'type' => 'radio',
                    'label' => $this->l('Active:'),
                    'name' => 'active',
                    'required' => false,
                    'class' => 't',
                    'is_bool' => true,
                    'values' => array(
                        array(
                            'id' => 'active_on',
                            'value' => 1,
                            'label' => $this->l('Enabled')
                        ),
                        array(
                            'id' => 'active_off',
                            'value' => 0,
                            'label' => $this->l('Disabled')
                        )
                    ),
                    'desc' => $this->l('Only one video can be active at any given time')
                ),
            )
        );

        if (Shop::isFeatureActive())
        {
            $this->fields_form['input'][] = array(
                'type' => 'shop',
                'label' => $this->l('Shop association:'),
                'name' => 'checkBoxShopAsso',
            );
        }

        $this->fields_form['submit'] = array(
            'title' => $this->l('   Save   '),
            'class' => 'button'
        );

        if (!($obj = $this->loadObject(true)))
            return;

        return parent::renderForm();
    }
  }

另一件事:是否可以在后台内部添加视频预览?我试图回显YouTube的嵌入代码,但它甚至在标头之前被插入.有没有做到这一点的干净方法,还是我必须使用一些jQuery技巧?在postProcess()结束之前,我基本上是在回显YT的嵌入代码.

One other thing: would it be possible to add a preview of the video inside the backoffice? I tried to echo YouTube's embed code, but it gets inserted even before the header. Is there a clean way of doing this or do I have to use some jQuery trickery? I was basically doing an echo of YT's embed code just before the end of postProcess().

提前谢谢!

推荐答案

将视频与产品相关联的最简单方法是在视频"表中添加产品"文本字段,以存储逗号分隔的列表.关联产品的ID(例如:1、10、27).即使有点基本,也应该可以使用.
另外,您可以使用这样的表:

The simplest way to associate the videos to the products is by adding a "products" text field in your "video" table to store a comma separated list of the ids of the associated products (eg.: 1,10,27). Even if it's a bit rudimentary, it should work.
Alternatively, you could use a table like this:

create table video_product (
  id_association int not null auto_increment,
  id_video int,
  id_product int,
  primary key (id_association)
);

此解决方案的问题在于PrestaShop ObjectModel核心不提供任何自动更新或删除相关表的方法(至少据我所知),因此您必须插入代码来管理"video_product"表在视频"类中.
如果要获取有关如何执行此操作的示例,则应查看classes/Product.php脚本,该脚本管理产品表及其所有相关表(类别,标签,功能部件,附件等).
要了解Prestashop数据库的结构,请查看docs/dbmodel.mwb文件,该文件包含数据库的架构;可以使用 MySQL Workbench 应用程序查看此文件.

The problem with this solution is that the PrestaShop ObjectModel core does not provide any method to automatically update or delete the related tables (at least as far as I know), so you have to insert the code to manage the "video_product" table in your "Video" class.
If you want an example of how to do this, you should look at the classes/Product.php script, which manages the product table and all its related tables (categories, tags, features, attachments, etc.).
To have an idea of how the Prestashop database is structured, have a look at the docs/dbmodel.mwb file, which contains the schema of the database; this file can be viewed by using the MySQL Workbench application.

这篇关于使用Prestashop的后台将一个项目与多个(不同类的)其他项目相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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