Prestashop:如何使用ObjectModel定义多个主键

时间:2014-07-27 10:19:54

标签: mysql primary-key prestashop composite-primary-key object-model

我正在定义扩展ObjectModel的新实体。我有一张包含2个主键的表:

CREATE TABLE `ec_product_order_attachment` (
  `id_product` int(10) unsigned NOT NULL,
  `id_order_attachment` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_product`,`id_order_attachment`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

但是,我似乎无法在ObjectModel中定义一个包含2个主键的表。

你知道这样做吗?

更新(已解决)

用户PrestaShop-Developer.com给了我一个很好的建议。我重新设计了Order_Attachment自定义实体类,它工作得很好。所以只是为了记录,我复制粘贴我完成的课程。只是说,我为自定义模块创建了这个类,该模块向管理编辑产品页面添加了一个选项卡,以添加保存特殊附件的功能,这些附件将与订单完整邮件一起发送。所以我需要2个新表:order_attachment和product_order_attachment将产品链接到特殊的order_attachments。

class Order_Attachment extends ObjectModel{
    public $name;
    public $mime;
    public $attachment_type;
    public $id_order_attachment;

    //validation options are from Prestashop's Validate class
    public static $definition = array(
        'table' => 'order_attachment',
        'primary' => 'id_order_attachment',
        'multilang' => FALSE,
        'fields' => array(
            'name' => array('type' => self::TYPE_STRING, 'size' => 128),
            'mime' => array('type' => self::TYPE_STRING, 'size' => 128),
            'attachment_type' =>  array('type' => self::TYPE_STRING, 'size' => 128)
        )
    );

    //get current order attachments for a specific product
    public static function get_product_order_attachments($id_product = false){

        $id_p = ($id_product) ? $id_product : Tools::getValue('id_product');

        $db = Db::getInstance();
        $sql = 'SELECT *
                FROM ' . _DB_PREFIX_ . 'order_attachment oa
                INNER JOIN ' . _DB_PREFIX_ . 'product_order_attachment poa ON oa.id_order_attachment = poa.id_order_attachment
                WHERE poa.id_product = ' . $id_p;
        return $db->executeS($sql);
    }

    public function save($null_values = false, $autodate = true){

        $result1 = parent::save();

        $result2 = Db::getInstance()->insert('product_order_attachment',array(
            'id_product' => Tools::getValue('id_product'),
            'id_order_attachment' => $this->id
        ));

        return ($result1 && $result2);
    }

    public function delete($id_product = false){

        $id_p = ($id_product) ? $id_product : Tools::getValue('id_product');

        //destroy file associated with the attachment
        if(file_exists(_PS_UPLOAD_DIR_ . $this->name)) unlink(_PS_UPLOAD_DIR_ . $this->name);

        parent::delete();
        Db::getInstance()->delete('product_order_attachment','id_product = ' . $id_p . ' AND id_order_attachment = ' . $this->id);
    }

}//END CLASS

1 个答案:

答案 0 :(得分:1)

无法在PrestaShop ObjectModel中定义多列密钥。根据定义,这是错误的。您的ObjectModel应该是一个独立的实体。

管理此表可以完成:

  1. 在OrderAttachment ObjectModel中(如果有的话)
  2. 使用Product ObjectModel(使用覆盖,添加所需方法)
  3. 您可以拥有ProductOrderAttachment类(而非ObjectModel)和 那里有静态方法。
  4. 但是,如果你有OrderAttachment实体,1)是最好的方法。如果你没有,你可以考虑添加它,因为它似乎是#34;独立的实体"。