【问题标题】:Magento2 customer account creation, save boolean value in attributeMagento2 客户帐户创建,在属性中保存布尔值
【发布时间】:2018-04-20 07:08:20
【问题描述】:

所以,正如标题所说,我在客户实体中添加了一些新属性,其中一个是布尔值,您可以从setup/InstallData.php 中的代码中看到

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'azienda', [
        'type' => 'int',
        'label' => 'Azienda',
        'input' => 'boolean',
        'source' => '',
        'required' => false,
        'visible' => true,
        'position' => 333,
        'system' => false,
        'backend' => ''
    ]);

我还为"customer_account_create" 创建了前端布局覆盖,一切正常,我添加的其他文本字段已正确保存,但我无法将它保存在我的数据库中这个"azienda" 属性是一个布尔值。我试过复选框、单选按钮、带有“yes”、“true”和“1”的文本字段,无论我在该字段中输入什么,在注册后布尔属性都将始终处于未选中状态(false)。 p>

尝试在实体数组中添加"default" => 1,但它仍然没有将值保存为true/checked

希望我已经很好地解释了我的问题,我觉得这是我做错的小事,但我无法弄清楚它是什么。谢谢你的帮助:)

【问题讨论】:

    标签: php magento magento2


    【解决方案1】:

    好的,我已经设法修复它,最后它不是很难或不可能找到的。

    我从一开始就做了我应该做的,检查“订阅时事通讯”是如何保存的,我注意到 <input type="checkbox">value="1" 属性,所以我把它放在我的复选框,现在它可以工作了。如果有人遇到我同样的问题,请在此处留下代码。

        <div class="field choice newsletter">
            <input type="checkbox" name="azienda" title="<?= $block->escapeHtmlAttr(__('Azienda')) ?>" value="1" id="azienda" class="checkbox">
            <label for="azienda" class="label"><span><?= $block->escapeHtml(__('Azienda')) ?></span></label>
        </div>
    

    据我了解,这是可行的,因为只有在使用 on 值检查复选框时才会将复选框添加到发布请求中,但如果我们添加了发送的 value="1" 属性,并且当 Magento2 收到 azienda -&gt; 1 时它正确地保存在我的用户实体中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多