我知道在SA中有几个关于此的主题,但我无法弄清楚为什么我的代码不起作用......让我解释一下:
我有一个公司实体,可能有很多相关用户。 当我创建一家公司时,我想创建一个" admin"用户(第一个用户)使用相同的表单。
我的邀请:
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;
}
}
当我提交表单(其中包含用于创建公司和第一个用户的字段)时,公司将保存在DB以及第一个用户中,但User的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 %}
控制器:
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;
}
BTW,我按照本指南创建表单:https://symfony.com/doc/master/form/form_collections.html
答案 0 :(得分:0)
我们错过了表单的一些代码,但这是一个疯狂的猜测...
在我看来,您正试图在您的公司之前坚持User
因此,它还无法设置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 [...]
}
答案 1 :(得分:0)
嗯,答案其实很明显......
错误在于 CompanyController :
$company = new Company();
$user = new User();
$company->getUsers()->add($user);
应该是:
$company = new Company();
$user = new User();
$company->addUser($user);
在原始代码中:
$user->setCompany($this);
从未使用过,因为我将用户“手动”添加到集合中......所以创建的用户和公司之间的链接从未设置过。我错了,因为我所遵循的教程(https://symfony.com/doc/master/form/form_collections.html)略有不同......