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 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备