Symfony 4个单一形式的多个实体 [英] Symfony 4 multiple entities in single form

查看:64
本文介绍了Symfony 4个单一形式的多个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试了好几个小时才能使我的多实体表单正常工作,但这确实让我伤脑筋,而且我发现的所有示例都无济于事.

Been trying for hours and hours to get my multi entity form to work, but it really breaks my head and none of the examples I've found work.

我检查了收藏表格类型文档表单集合,以及我有一个 User 实体,一个 UserRole 实体和一个 Role 实体.UserRole包含一个用户ID和一个角色ID.只是一个链接表.

I have a User entity, UserRole entity and a Role entity. UserRole contains a userID and a roleID. Just a linking table.

该表单显示了用于创建用户的字段,我希望能够为新用户选择一个新角色.因此,我尝试使用EntityType,一个select下拉列表很好地显示了所有角色(仅当我添加映射选项=> false时才显示),但是在表单提交后不进行处理.它的数据不在$ form-> getData()中,已创建用户,从未创建过user_role条目.如果我在没有映射的=> false的情况下尝试尝试,则会抛出提示:

The form shows fields to create a User and I want to be able to as well select a new Role for the new user. So I've tried to use the EntityType, a select dropdown shows with all the roles nicely (only if i add the option mapped => false), but doesn't process after form submit. It's data is not in the $form->getData(), the user gets created, the user_role entry never created. If I try it without the mapped => false it throws me:

Could not determine access type for property "user_roles" in class "App\Entity\User": The property "user_roles" in class "App\Entity\User" can be defined with the methods "addUserRole()", "removeUserRole()" but the new value must be an array or an instance of \Traversable, "App\Entity\Role" given..

代码:

$form = $this->createFormBuilder(new User)
    ... //other add entries
    ->add('user_roles', EntityType::class, array(
        'label' => 'Group (role)',
        'class' => Role::class,
        'choice_label' => 'name',
        // 'mapped' => false, // Form works when false, but doesn't save/create UserRole entry
    ))
->getForm();

$form->handleRequest($request);

使用CollectionType根本不显示选择下拉列表.代码:

Using the CollectionType it's not showing a select dropdown at all. Code:

$form = $this->createFormBuilder($user)
    .... //other add entries
    ->add('user_roles', CollectionType::class, array(
        'entry_type' => ChoiceType::class,
        'entry_options' => array(
            'choices' => $roleChoices,
        ),
    ))
->getForm();

$form->handleRequest($request);

我是否在Controller的代码中缺少某些内容?还是我误解了Form类型的使用?我真的不知道我在做什么错.

Am I missing something in my Controller's code or do I misunderstand the use of the Form types? I really have no clue what I'm doing wrong.

用户实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use JMS\Serializer\Annotation\Exclude;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class User implements UserInterface
{
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Exclude
     */
    private $apiToken;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="json_array")
     */
    private $roles = [];

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $first_name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $middle_name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $last_name;

    /**
     * @ORM\Column(type="boolean")
     */
    private $enabled;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $blocked_at;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Project", mappedBy="created_by")
     */
    private $projects;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\UserRole", mappedBy="user", fetch="EAGER")
     */
    private $user_roles;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="created_by")
     */
    private $categories;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ProjectFileIos", mappedBy="created_by")
     */
    private $projectFileIos;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ProjectFileAndroid", mappedBy="created_by")
     */
    private $projectFileAndroid;

    /**
     * Generate full name
     */
    private $full_name;

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     * @Exclude
     */
    private $password;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ProjectUser", mappedBy="user", fetch="EAGER")
     */
    private $projectUsers;

    /**
     * @ORM\Column(type="datetime")
     */
    private $created_at;

    /**
     * @ORM\Column(type="datetime")
     */
    private $updated_at;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="project")
     */
    private $created_by;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="project")
     * @ORM\JoinColumn(nullable=true)
     */
    private $last_updated_by;

    public function __construct()
    {
        $this->user_roles = new ArrayCollection();
        $this->user_role = new ArrayCollection();
        $this->categories = new ArrayCollection();
        $this->projectFileIos = new ArrayCollection();
        $this->projectFileAndroid = new ArrayCollection();
        $this->projectUsers = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getApiToken(): ?string
    {
        return $this->apiToken;
    }

    public function setApiToken(string $apiToken): self
    {
        $this->apiToken = $apiToken;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUsername(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getPassword(): string
    {
        return (string) $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getSalt()
    {
        // not needed when using the "bcrypt" algorithm in security.yaml
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getFirstName(): ?string
    {
        return $this->first_name;
    }

    public function setFirstName(string $first_name): self
    {
        $this->first_name = $first_name;

        return $this;
    }

    public function getMiddleName(): ?string
    {
        return $this->middle_name;
    }

    public function setMiddleName(string $middle_name): self
    {
        $this->middle_name = $middle_name;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->last_name;
    }

    public function setLastName(string $last_name): self
    {
        $this->last_name = $last_name;

        return $this;
    }

    public function getEnabled(): ?bool
    {
        return $this->enabled;
    }

    public function setEnabled(bool $enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    public function getBlockedAt(): ?\DateTimeInterface
    {
        return $this->blocked_at;
    }

    public function setBlockedAt(?\DateTimeInterface $blocked_at): self
    {
        $this->blocked_at = $blocked_at;

        return $this;
    }

    /**
     * @return Collection|UserRole[]
     */
    public function getUserRoles(): ?Collection
    {
        return $this->user_roles;
    }
    public function getUserRole(): ?Collection
    {
        return $this->user_role;
    }

    public function addUserRole(UserRole $userRole): self
    {
        if (!$this->user_role->contains($userRole)) {
            $this->user_role[] = $userRole;
            $user_role->setUserId($this);
        }

        return $this;
    }

    public function removeUserRole(UserRole $userRole): self
    {
        if ($this->user_role->contains($userRole)) {
            $this->user_role->removeElement($userRole);
            // set the owning side to null (unless already changed)
            if ($user_role->getUserId() === $this) {
                $user_role->setUserId(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Project[]
     */
    public function getProjects(): Collection
    {
        return $this->projects;
    }

    public function addProject(Project $project): self
    {
        if (!$this->project->contains($project)) {
            $this->project[] = $project;
            $project->setUserId($this);
        }

        return $this;
    }

    public function removeProject(Project $project): self
    {
        if ($this->project->contains($project)) {
            $this->project->removeElement($project);
            // set the owning side to null (unless already changed)
            if ($project->getUserId() === $this) {
                $project->setUserId(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Category[]
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function addCategory(Category $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
            $category->setCreatedBy($this);
        }

        return $this;
    }

    public function removeCategory(Category $category): self
    {
        if ($this->categories->contains($category)) {
            $this->categories->removeElement($category);
            // set the owning side to null (unless already changed)
            if ($category->getCreatedBy() === $this) {
                $category->setCreatedBy(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|ProjectFileIos[]
     */
    public function getProjectFileIos(): Collection
    {
        return $this->projectFileIos;
    }

    public function addProjectFileIo(ProjectFileIos $projectFileIo): self
    {
        if (!$this->projectFileIos->contains($projectFileIo)) {
            $this->projectFileIos[] = $projectFileIo;
            $projectFileIo->setCreatedBy($this);
        }

        return $this;
    }

    public function removeProjectFileIo(ProjectFileIos $projectFileIo): self
    {
        if ($this->projectFileIos->contains($projectFileIo)) {
            $this->projectFileIos->removeElement($projectFileIo);
            // set the owning side to null (unless already changed)
            if ($projectFileIo->getCreatedBy() === $this) {
                $projectFileIo->setCreatedBy(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|ProjectFileAndroid[]
     */
    public function getProjectFileAndroid(): Collection
    {
        return $this->projectFileAndroid;
    }

    public function addProjectFileAndroid(ProjectFileAndroid $projectFileAndroid): self
    {
        if (!$this->projectFileAndroid->contains($projectFileAndroid)) {
            $this->projectFileAndroid[] = $projectFileAndroid;
            $projectFileAndroid->setCreatedBy($this);
        }

        return $this;
    }

    public function removeProjectFileAndroid(ProjectFileAndroid $projectFileAndroid): self
    {
        if ($this->projectFileAndroid->contains($projectFileAndroid)) {
            $this->projectFileAndroid->removeElement($projectFileAndroid);
            // set the owning side to null (unless already changed)
            if ($projectFileAndroid->getCreatedBy() === $this) {
                $projectFileAndroid->setCreatedBy(null);
            }
        }

        return $this;
    }

    public function getFullName()
    {
        $lastName = $this->middle_name ? $this->middle_name . ' ' : '';
        $lastName .= $this->last_name;
        return $this->first_name . ' ' . $lastName;
    }

    /**
     * Triggered after entity has been loaded into the current EntityManager from de database
     * or after refresh operation applied to it
     * @ORM\PostLoad
     */
    public function postLoad()
    {
        $this->full_name = $this->getFullName();
    }

    /**
     * @return Collection|ProjectUser[]
     */
    public function getProjectUsers(): Collection
    {
        return $this->projectUsers;
    }

    public function addProjectUser(ProjectUser $projectUser): self
    {
        if (!$this->projectUsers->contains($projectUser)) {
            $this->projectUsers[] = $projectUser;
            $projectUser->setUser($this);
        }

        return $this;
    }

    public function removeProjectUser(ProjectUser $projectUser): self
    {
        if ($this->projectUsers->contains($projectUser)) {
            $this->projectUsers->removeElement($projectUser);
            // set the owning side to null (unless already changed)
            if ($projectUser->getUser() === $this) {
                $projectUser->setUser(null);
            }
        }

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->created_at;
    }

    public function setCreatedAt(\DateTimeInterface $created_at): self
    {
        $this->created_at = $created_at;

        return $this;
    }

    public function getUpdatedAt(): ?\DateTimeInterface
    {
        return $this->updated_at;
    }

    public function setUpdatedAt(\DateTimeInterface $updated_at): self
    {
        $this->updated_at = $updated_at;

        return $this;
    }

    public function getCreatedBy(): ?User
    {
        return $this->created_by;
    }

    public function setCreatedBy(?User $created_by): self
    {
        $this->created_by = $created_by;

        return $this;
    }

    public function getLastUpdatedBy(): ?User
    {
        return $this->last_updated_by;
    }

    public function setLastUpdatedBy(?User $last_updated_by): self
    {
        $this->last_updated_by = $last_updated_by;

        return $this;
    }


    /**
     * Triggered on insert
     * @ORM\PrePersist
     */
    public function onPrePersist()
    {
        $this->enabled = true;
        $this->created_at = new \DateTime("now");
        $this->updated_at = new \DateTime();
        $this->roles = 'a:1:{i:0;s:9:"ROLE_USER";}';
    }

    /**
     * Triggered on update
     * @ORM\PreUpdate
     */
    public function onPreUpdate()
    {
        $this->updated_at = new \DateTime("now");
    }
}

推荐答案

在Symfony中,要获取未映射的表单数据,请尝试这样做.

In Symfony, to get non-mapped form data, try doing like this.

$data = $form->getData();    
$roles = $form->get("user_roles")->getData();

另外,请注意一件事.在下面的代码块中,该类不应该是 UserRole :: class 而不是 Role :: class .

Also, noted one thing. Shouldn't the class be UserRole::class instead of Role::class, in the code block below.

$form = $this->createFormBuilder(new User)
... //other add entries
->add('user_roles', EntityType::class, array(
    'label' => 'Group (role)',
    'class' => Role::class,
    'choice_label' => 'name',
    // 'mapped' => false, // Form works when false, but doesn't save/create UserRole entry
))
->getForm();

希望这会有所帮助,干杯..

Hope this helps, Cheers..

这篇关于Symfony 4个单一形式的多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆