在prestashop中的Admin客户详细信息表单中添加多选下拉列表

时间:2015-10-09 06:43:14

标签: admin prestashop-1.6

我在客户添加表单的管理员中添加了一个多选下拉字段。我在数据库中创建了新字段。但我没有得到如何将这个新字段的值存储到数据库中。请帮忙

我已将此代码添加到AdminCustomersController.php

阵列(

                 'type' => 'select',
                'multiple' => true,
                'label' => $this->l('Branch Location'),
                'name' => 'branch_location',
                'required' => false,
                'options' => array(
                    'query' => $list_branch_arr,
                    'id' => 'branch_location',
                    'name' => 'name'
                ),
                'col' => '4',
                'hint' => array(
                    $this->l('Please choose the branch name from the branch list.')
                )

但不知道如何将所选值插入数据库

2 个答案:

答案 0 :(得分:0)

您必须将新字段添加到客户模型。 /prestashop/classess/Customer.php

    /** @var string Object last modification date */
public $date_upd;

添加:

public $branch_location;

并修改:

public static $definition = array(
    'table' => 'customer',
    'primary' => 'id_customer',
    'fields' => array(
        'secure_key' =>                 array('type' => self::TYPE_STRING, 'validate' => 'isMd5', 'copy_post' => false),
        'lastname' =>                   array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
        'firstname' =>                  array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
        'email' =>                      array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),
        'passwd' =>                     array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 32),
        'last_passwd_gen' =>            array('type' => self::TYPE_STRING, 'copy_post' => false),
        'id_gender' =>                  array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
        'birthday' =>                   array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate'),
        'newsletter' =>                 array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
        'newsletter_date_add' =>        array('type' => self::TYPE_DATE,'copy_post' => false),
        'ip_registration_newsletter' => array('type' => self::TYPE_STRING, 'copy_post' => false),
        'optin' =>                      array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
        'website' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isUrl'),
        'company' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
        'siret' =>                      array('type' => self::TYPE_STRING, 'validate' => 'isSiret'),
        'ape' =>                        array('type' => self::TYPE_STRING, 'validate' => 'isApe'),
        'outstanding_allow_amount' =>   array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'copy_post' => false),
        'show_public_prices' =>         array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'id_risk' =>                    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
        'max_payment_days' =>           array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
        'active' =>                     array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'deleted' =>                    array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'note' =>                       array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'size' => 65000, 'copy_post' => false),
        'is_guest' =>                   array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'id_shop' =>                    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
        'id_shop_group' =>              array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
        'id_default_group' =>           array('type' => self::TYPE_INT, 'copy_post' => false),
        'id_lang' =>                    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
        'date_add' =>                   array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
        'date_upd' =>                   array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
    ),
);

到:

    public static $definition = array(
    'table' => 'customer',
    'primary' => 'id_customer',
    'fields' => array(
        'secure_key' =>                 array('type' => self::TYPE_STRING, 'validate' => 'isMd5', 'copy_post' => false),
        'lastname' =>                   array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
        'firstname' =>                  array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
        'email' =>                      array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),
        'passwd' =>                     array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 32),
        'last_passwd_gen' =>            array('type' => self::TYPE_STRING, 'copy_post' => false),
        'id_gender' =>                  array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
        'birthday' =>                   array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate'),
        'newsletter' =>                 array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
        'newsletter_date_add' =>        array('type' => self::TYPE_DATE,'copy_post' => false),
        'ip_registration_newsletter' => array('type' => self::TYPE_STRING, 'copy_post' => false),
        'optin' =>                      array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
        'website' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isUrl'),
        'company' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
        'siret' =>                      array('type' => self::TYPE_STRING, 'validate' => 'isSiret'),
        'ape' =>                        array('type' => self::TYPE_STRING, 'validate' => 'isApe'),
        'outstanding_allow_amount' =>   array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'copy_post' => false),
        'show_public_prices' =>         array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'id_risk' =>                    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
        'max_payment_days' =>           array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
        'active' =>                     array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'deleted' =>                    array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'note' =>                       array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'size' => 65000, 'copy_post' => false),
        'is_guest' =>                   array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
        'id_shop' =>                    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
        'id_shop_group' =>              array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
        'id_default_group' =>           array('type' => self::TYPE_INT, 'copy_post' => false),
        'id_lang' =>                    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
        'date_add' =>                   array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
        'date_upd' =>                   array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
        'branch_location' =>            array('type' => self::TYPE_INT),
    ),
);

答案 1 :(得分:0)

答案是不正确的..由于它不仅在数据库中定义了字段,还必须以特殊的方式捕获和存储值,在本例中我将示例存储为“1,2,3,6,8 “使用单个字段

完整的代码和所有步骤:https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk

这里我只放了最重要的部分..

正如前面提到的那样链接,在模型定义,类和表sql中添加了新的fiel

此方法允许以“1,2,3”存储在数据库中,因此您只能使用单个字段来关联多个选定值,更好的可能是使用groupbox 但非常困难,看一下prestachop的controllers目录中的AdminCustomers控制器类,它有一个多选组,它使用存储在单个字段中的关系表事件

然后在帮助器表单中,列表输入数组将select定义为:

在开始时不要忘记添加该行:

// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['id_employee[]'] = explode(',',$obj->id_employee);

这个$ obj是从该对象转到编辑时加载的先前存储值的表示,获取多选的字段的存储值,存储为“1,3,4,6”

并在字段形式的输入辅助列表中将选择倍数定义为:

            array(
                 'type' => 'select',
                'label' => $this->l('Select and employee'),
                'name' => 'id_employee_tech',
                'required' => false,
                'col' => '6',
                'default_value' => (int)Tools::getValue('id_employee_tech'),
                'options' => array(
                    'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
                    'id' => 'id_employee',
                    'name' => 'firstname',
                    'default' => array(
                        'value' => '',
                        'label' => $this->l('ninguno')
                    )
                )
            ),

然后再覆盖后期处理

public function postProcess()
{
    if (Tools::isSubmit('submitTallerOrden'))
    {
        $_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
    }
    parent::postProcess();
}

这使得存储在数据库中为“1,2,3”