1

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

My TCA:

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

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

Mikael
  • 1,209
  • 1
  • 16
  • 47
  • Are you using an Extbase model or not? Because you write "model". If yes, please add "extbase" as tag. I think this question is a bit unclear as is. (I realize this question is old, but well written questions and tags would make it easier for others to search for solutions later. Right now, the answer could be much simpler if Extbase is not considered.). If you provide more context, alternative solutions could also be suggested. – Sybille Peters Nov 08 '22 at 15:55

2 Answers2

4

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.

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;
}

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

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;
    }
}

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

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.

Mathias Brodala
  • 5,905
  • 13
  • 30
  • Good idea, I will try!) But how typo3 will know that need call this method isInterestedInMen() ?? exactly InMen ? Is it depends of something ? – Mikael Jun 20 '17 at 11:31
  • And I don't need separate table, because I need always only three items : man, woman and couple? Can you advice simple solution ? – Mikael Jun 20 '17 at 12:21
  • I've added short examples to use these getters in Extbase/Fluid. As for a possibly simpler solution you can try the CSV approach. – Mathias Brodala Jun 20 '17 at 12:27
  • Tell me please, how fluid form must look like for 3 items ? Becouse it does't work for me. When I choice 3 items it save only number 2 - for one item only ( – Mikael Jun 20 '17 at 14:06
  • and how setter method must be look like ? – Mikael Jun 20 '17 at 14:07
  • I've added examples for setters and Fluid form access. – Mathias Brodala Jun 22 '17 at 08:30
1

Here you can find an example which is extending FileReference of TYPO3. The behavior is almost the same. In your case its just FrontendUser instead of FileReference: Extending sys_file_reference (FAL)

Paul Beck
  • 2,675
  • 1
  • 12
  • 14