dongmubi4444 2017-02-14 22:34
浏览 60
已采纳

从zend框架3中的select form元素获取Doctrine \ Collection

I'm trying to implement https://github.com/ZF-Commons/zfc-rbac in my module. At the moment I'm stuck, because I get following exception:

Argument 1 passed to User\Entity\User::setRoles() must implement interface Doctrine\Common\Collections\Collection, array given ...

Line, that causes the error in UserManager class: $user->setRoles($data['role']);

So it's clear, that setter in the entity is wrong or form element of type select which returns the $data['role'] element.

Code of setter:

public function setRoles(Collection $roles)
{
    $this->roles->clear();
    foreach ($roles as $role) {
        $this->roles[] = $role;
    }
}

Code of select element in UserForm:

$this->add([            
        'type'  => 'DoctrineModule\Form\Element\ObjectSelect',
        'name' => 'role',
        'attributes' => [
            'multiple' => true,
        ],
        'options' => [
            'object_manager' => $this->entityManager,
            'target_class' => 'User\Entity\Role',
            'label' => 'Role',
            'value_options' => $roles,
            'disable_inarray_validator' => true, //TODO: create validator
            'required' => true,
        ],
    ]);

So how cane I make select element return Doctrine\Common\Collections\Collection?

I tried to use ArrayCollection from Doctrine namespace, but it didn't work. Do I have to make separate class, that implements Collection interface and use it with select element? Or maybe there is some more conveniant ways to achieve this?

I also tried with removed @var, @param annotations and types in the Entity, but in this case I've got following message:

Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "User\Entity\User#$roles", got "string" instead.

  • 写回答

1条回答 默认 最新

  • doumingchen3628 2017-02-16 10:12
    关注

    I found a solution to my problem. The setter in User entity is correct I just had to make small helper function, that will convert array from select input into array of Role objects:

    private function getRolesFromArray($array){
        $roles = $this->entityManager->getRepository(Role::class)
                ->findBy(['id'=> $array]);
        return new ArrayCollection($roles);
    }
    

    So ArrayCollection works! Also there is properly made select field in Form object:

    $this->add([            
            'type'  => 'DoctrineModule\Form\Element\ObjectSelect',
            'name' => 'role',
            'attributes' => [
                'multiple' => true,
            ],
            'options' => [
                'object_manager' => $this->entityManager,
                'target_class' => 'User\Entity\Role',
                'label' => 'Role',
                'required' => true,
            ],
        ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀