douxian6008 2015-04-14 14:20
浏览 44
已采纳

Doctrine 2自定义ObjectMultiCheckbox值

How can I custom values with DoctrineModule\Form\Element\ObjectMultiCheckbox?

I used Zend\Form\Element\MultiCheckbox and I set values like this:

$this->add(array(
    'type' => 'Zend\Form\Element\MultiCheckbox',
    'name' => 'countries',
    'options' => array(
        'label' => 'Select countries',
        'value_options' => array(
            'value' => 1,
            'label' => 'United Kingdom',
            'continent' => 'Europe'
        )
    )
))

But now I need to use Doctrine 2 Multicheckbox and I need to set custom value options. How can i do this?

I have currently only this:

$this->add(array(
    'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
    'name' => 'countries',
    'options' => array(
        'object_manager' => $this->em,
        'target_class'   => 'Module\Entity\Country'
    )
));

I need this for custom view render. I want to show countries like this:

Europe
- Sweden
- United Kingdom
- and others...

America
- Canada
- United States
- other countries...

  • 写回答

1条回答 默认 最新

  • douping7105 2015-06-26 11:06
    关注

    SOLVED!

    I created a new form element:

    ObjectMultiCheckbox:

    namespace Application\Form\Element;
    
    use Zend\Form\Element\MultiCheckbox;
    use Zend\Stdlib\ArrayUtils;
    
    class ObjectMultiCheckbox extends MultiCheckbox
    {
        public function setValue($value)
        {
            if ($value instanceof \Traversable)
            {
                $value = ArrayUtils::iteratorToArray($value);
    
                foreach ($value as $key => $row)
                {
                    $values[] = $row->getId();
                }
    
                return parent::setValue($values);
            }
            elseif ($value == null)
            {
                return parent::setValue(array());
            }
            elseif (!is_array($value))
            {
                return parent::setValue((array)$value);
            }
        }
    }
    

    It's not really pretty, but it handle object to the form as DoctrineModule\Form\Element\ObjectMultiCheckbox.

    My entity which using this code have always identifier 'id' so I can use static code as like this: $row->getId(); It's ugly, but it works!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路