将 Doctrine/Symfony 4 中建立的关联插入 DB

Inserting into DB the association established in Doctrine/Symfony 4

我在 Symfony 4 中的用户 class:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;

    /**
     * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
     * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
     */
    class User implements UserInterface
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;

    // ... rest of the User Entity
    }

我创建了另一个名为 "Creator" 的实体,如下所示:

// src/Entity/Creator.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 */
class Creator
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="creator")
     */
    private $user;


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

    public function createCreator($userid)
    {
        $this->user->id = $userid;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    // ... getter and setter methods
}

两者之间的关联是当用户注册为"creator"时(在仅注册为"regular user"的上下文之外),插入将创建者与用户相关联。

插入数据库是这样的:

$user    = new User();
$creator = new Creator();
$form    = $this->createForm(CreatorRegistrationForm::class, [$user, $creator]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    $user
        ->setEmail($form->get('email')->getData())
        ->setPassword(
                $passwordEncoder->encodePassword(
                    $user,
                    $form->get('password')->getData()
                )
            )
            ->setFirstName($form->get('firstname')->getData())
            ->setLastName($form->get('lastname')->getData())
            ->setBirthday($form->get('birthday')->getData());

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush(); //This insert is successful :)

            $userID = $user->getId();

            $creator->createCreator($userID);

            $entityManager->persist($creator);
            $entityManager->flush();
            // This does nothing.
            // Can you only persist/flush once? If so, what is the right syntax?

            return $guardHandler->authenticateUserAndHandleSuccess(
                $user,
                $request,
                $authenticator,
                'main' // firewall name in security.yaml
            );
        }

这是通过学说在数据库中创建关联的正确方法吗?仍在尝试了解协会的运作方式。您是否必须像上面那样明确声明关联?这个 returns 一个 Warning: Creating default object from empty value 的错误,它指出:

public function createCreator($userid)
{        
    $this->user->id = $userid; //this here is what the error points to.

    return $this;    
}

执行此操作的正确方法是什么?

Doctrine 期望 Creator->user 成为 User class 的对象。

所以您可能需要一个 Creator::setUser(User) 方法,并执行如下操作:

//...
$entityManager->persist($user);
$creator->setUser($user);
$entityManager->persist($creator);

$entityManaer->flush();