php - Doctrine One To Many 级联持续存在,外键为空

标签 php symfony doctrine

我知道 SA 中有几个关于此的主题,但我无法弄清楚为什么我的代码不起作用......让我解释一下:

我有一个公司实体,它可能有很多相关用户。 当我创建公司时,我想使用相同的表单创建一个“管理员”用户(第一个用户)。

我的实体:

class Company
{
    **
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
    */
    private $id;
    ...

    /**
     * A company has many users.
     * @ORM\OneToMany(targetEntity="User", mappedBy="company", cascade={"persist"})
     */
    private $users;
    ...

    public function __construct() {
        $this->users = new ArrayCollection();
    }

    public function addUser(User $user)
    {
        $user->setCompany($this);
        $this->users->add($user);
        return $this; // doesn't appear in the documentation but found in SA... doesn't change anything
    }

    public function removeUser(User $user)
    {
        // ...
    }

}


class User
{
    **
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
    */
    private $id;
    ...

    /**
     * Many users belong to a company.
     * @ORM\ManyToOne(targetEntity="Company", inversedBy="users")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
     */
    private $company;
    ...

    /**
     * @param mixed $company
     */
    public function setCompany ($company)
    {
        $this->company = $company;
    }
}

当我提交表单(其中包含用于创建公司和第一个用户的字段)时,公司以及第一个用户都保存在数据库中,但用户的 company_id 设置为 NULL。我必须这样做才能使其正常工作(下面的代码在专门用于管理公司的服务中):

public function createCompany($company)
    {
    ...
    $company->getUsers()->get(0)->setCompany($company); // <- HERE (btw is there a way to access the first user without using get(0) ?)
    ...

    $this->entityManager->persist($company);
    $this->entityManager->flush();
    }

我不应该那样做,对吧? 我以为

$user->setCompany($this); 

在 addUser 中会自动执行...

我哪里错了?

编辑:

公司表格: (再一次,我没有把所有的代码都写清楚,我只是贴出有用的行):

class RegisterCompanyForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('name',null,[
            'translation_domain' => 'forms',
            'label' => 'register_company.name.label'
        ])
        ...
        ->add('users', CollectionType::class, [
            'entry_type' => RegisterUserForm::class,
            'entry_options' => array('label' => false),
            'allow_add' => true,
            'by_reference' => false,    
        ])
        ; 
    }
}

用户表单:

class RegisterUserForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('givenName',TextType::class,[
            'translation_domain' => 'forms',
            'label' => 'register_user.givenName.label'
        ])
        ->add('familyName',null,[
            'translation_domain' => 'forms',
            'label' => 'register_user.familyName.label'
        ])
        ...
        ->add('password',null,[
            'translation_domain' => 'forms',
            'label' => 'register_user.password.label'
        ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\User',
        ));
    }
}

我的表单 Twig 模板:

{% extends 'layout/layout.html.twig' %}
{% trans_default_domain 'ui' %}

{% block title %}My Form {% endblock %}

{% block content %}
    <div class="container">
        <div class="starter-template">
            <h1>{% trans %}title.register{% endtrans %}</h1>
            {{ form_start(form) }}
            {{ form_errors(form) }}
            {{ form_row(form.name) }}
            ...
            {% for user in form.users %}
            {{ form_row(user.givenName) }}
            {{ form_row(user.familyName) }}
            ...
            {{ form_row(user.password) }}
            {% endfor %}
            {{ form_end(form) }}
        </div>
    </div>
{% endblock %}

Controller :

class CompanyController extends Controller
{
    public function inscription(CompanyManager $companyManager, Request 
$request)
    {
        $company = new Company();      
        $user = new User();
        $company->getUsers()->add($user);
        $form = $this->createForm(RegisterCompanyForm::class, $company);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // saving to DB is managed by a service
            $companyManager->createCompany($company);
        }

        return $this->render('views/company/register.html.twig', [
                'form'  => $form->createView()
        ]);
    }
}

CompanyManager 服务中:

public function createCompany($company)
    {

        // i'd like to avoid the next line !!! because i think it shouldn't be there...
        $company->getUsers()->get(0)->setCompany($company);

        $this->entityManager->persist($company);
        $this->entityManager->flush();
        return true;
    }

顺便说一句,我按照本指南创建了我的表格:https://symfony.com/doc/master/form/form_collections.html

最佳答案

我们遗漏了一些表单代码,但这里是一个疯狂的猜测......

在我看来,您试图在公司之前坚持您的用户
因此,它无法设置 company_id,因为您还没有持久化 Company
试试这个。

public function createCompany($company) {
    // Code folding [...]
    $this->entityManager->persist($company);
    $this->entityManager->flush();
    $this->entityManager->refresh($company); // <== Refreshing persisted eneity to get the new created ID.

    $company->getUsers()->get(0)->setCompany($company);
    // Code folding [...]
}

关于php - Doctrine One To Many 级联持续存在,外键为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49360990/

相关文章:

php - 从 Controller 访问 Liip Imagine bundle - 将服务分配给变量(Symfony 4)

php - 表列的背景颜色

php - 在 CentOS 5.5 上为 php 5.3 安装 ldap 时出错

php - 如何在创建/更新 Symfony2 表单中管理链接实体

php - 制定复杂的 Doctrine2 DQL 查询

Doctrine 2 值对象

php - 通过 php 定义 CSS 颜色

php - Symfony2 : $form->isValid() is always true

symfony - NelmioApiDocBundle 不起作用 "No operations defined in spec!"

php - Doctrine 与 Propel 有很好的比较吗?