TYPO3 向模型添加复选框字段 [英] TYPO3 add checkbox field to model

查看:26
本文介绍了TYPO3 向模型添加复选框字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 TYPO3 版本 7.6.18.我在 fe_users 表中添加了 'interest' int 字段.

I have TYPO3 version 7.6.18. I added 'interest' int field to fe_users table.

我的 TCA:

'interest' => [
    'exclude' => 1,
    'label' => 'Interest',
    'config' => [
        'type' => 'check',
        'default' => '0',
        'items' => [
            ['Mann', 0 ],
            ['Frau', 1],
            ['Paar', 2]
        ]
    ]
],

请帮帮我.我必须建模的是,该用户可以在其个人资料的前端设置兴趣复选框?getter 和 setter 方法必须是怎样的?我必须使用什么对象,请告诉我

Help me please. What I must to model, that user can set interest checkbow in frontend in his profile ? How getters and setters method must be look like ? What object I must use, tell me please

推荐答案

这有点棘手,因为 TYPO3 将多个复选框存储为单个整数值位掩码.因此,在某些时候,如果您想使用它,您需要再次拆分这个组合值.顺便说一句,您的复选框值未使用,因为 TYPO3 自动将所有复选框存储为位 1 或 0,具体取决于它们是否被选中.

This is a bit tricky since TYPO3 stores multiple checkboxes as a single integer value bitmask. Thus at some point you'll need to split this combined value again if you want to use it. BTW, your checkbox values are unused since TYPO3 automatically stores all checkboxes as bit 1 or 0 depending on whether they are checked or not.

一个简单的解决方案是将此值映射到模型中的 integer,然后为每个可能的值提供 getter:

A simple solution would be mapping this value to an integer in your model and then provide getters for each possible value:

/**
 * @var integer
 */
protected $interests;

/**
 * @return bool
 */
public function isInterestedInMen()
{
    return $this->interests & 0b00000001;
}

/**
 * @return bool
 */
public function isInterestedInWomen()
{
    return $this->interests & 0b00000010;
}

/**
 * @return bool
 */
public function isInterestedInPairs()
{
    return $this->interests & 0b00000100;
}

然后您可以在 Extbase 中使用 $object->isInterestedInPairs() 或在 Fluid 中使用 {object.interestedInPairs}.

You could then use $object->isInterestedInPairs() in Extbase or {object.interestedInPairs} in Fluid.

以下是如何实现 setter 的示例:

Here's an example how setters could be implemented:

/**
 * @var integer
 */
protected $interests;

/**
 * @param bool
 */
public function setInterestedInMen($interestedInMen)
{
    if ($interestedInMen) {
        $this->interests |= 0b00000001;
    } else {
        $this->interests &= ~0b00000001;
    }
}

写这些,例如通过 Fluid 表单,您只需使用 <f:form.checkbox property="interestedInMen" value="1"/>.

To write to these e.g. via Fluid forms you would simply use <f:form.checkbox property="interestedInMen" value="1" />.

但是正如您所看到的,这很快变得笨拙且难以理解,因此我建议为兴趣创建一个单独的表和模型,然后可以在后端轻松维护,或者至少切换到 select 字段并使用字符串值,然后在本地存储为 CSV.然后可以将其映射到模型中的 string 并通过 explode() 传递以获得单独的值.但同样,我建议查看单独的表/模型和关系方法.

But as you can see this quickly becomes unwieldy and very hard to understand thus I'd suggest either creating a separate table and model for the interests which can then easily be maintained in the backend or at least switch to the select field and use string values which are then stored as CSV locally. This can then be mapped to string in the model and passed through explode() to get separate values. But again, I recommend looking at the separate table/model and relation approach.

这篇关于TYPO3 向模型添加复选框字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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