Search code examples
phpdoctrine-ormzend-framework2

Doctrine ObjectMultiCheckBox form many-to-many grouped


I have a database like this

model database

And I need to make a form with checkboxes to save this to this database. Assume these are what my records look like

db records

For now, I have a form like this in which the user can't know witch action corresponds to which function

actual form

I'd like something like this where action and function appear together

enter image description here

Does anyone know how to do this?

My PHP code for creating the checkboxes is as follows:

        $this->add(array(
        'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
        'name' => 'functionAction',
        'options' => array(
            'label' => 'Functions:',
            'object_manager' => $this->getObjectManager(),
            'target_class' => 'Acl\Entity\FunctionAction',
            'label_generator' => function ($funcAction)
            {
                return $funcAction->getAction()->getDescription();
            },
            'is_method' => true,
            'find_method' => array(
                'name' => 'findByRole',
                'params' => array(
                    'role' => $this->getUser()->getRole()
                )
            )
        )
    ));

Solution

  • SOLUTION

    Options:

    $options = array(
        'head_check_callback' => function ($last, $current)
        {
            return ! $last || $last->getFunction()->getId() != $current->getFunction()->getId();
        },
        'head_label_callback' => function (\Acl\Entity\FunctionAction $current)
        {
            $nome = $current->getFunction()->getDescription();
            return $current ? $nome : '';
        }
    );
    

    To render in view

    $this->FormGroupMultiCheckbox()->setOptions($options)->render($this->form->get('functionAction'));
    

    Edited CheckBox class

    http://pastebin.com/UcDN8xVp