我有两个实体Organization和CustomValue。关系oneToMany。我一直在想2天为什么我的嵌入形式不起作用。它在DB中创建两个表,但没有外键。我哪里错了?我还关系manyToMany,它在组织实体上运作良好。
组织:
BaseBundle\Entity\Organization:
type: entity
table: base_organizations
gedmo:
soft_deleteable:
field_name: deletedAt
time_aware: false
id:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
childOrganizations:
targetEntity: Organization
mappedBy: parentOrganization
customValues:
targetEntity: CustomValue
mappedBy: organization
nullable: false
cascade: ["persist", "merge"]
manyToOne:
parentOrganization:
targetEntity: Organization
inversedBy: childOrganizations
nullable: true
manyToMany:
addresses:
targetEntity: Address
inversedBy: organizations
joinTable:
name: base__organizations_addresses
joinColumns:
organization_id:
referencedColumnName: id
inverseJoinColumns:
address_id:
referencedColumnName: id
cascade: ["persist","merge"]
CustomValues:
BaseBundle\Entity\CustomValue:
type: entity
table: base_custom_values
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 64
value:
type: string
length: 64
manyToOne:
organization:
targetEntity: Organization
inversedBy: customValues
nullable: true
OrganizationType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, array('max_length' => 100))
->add('type')
->add(
'parentOrganization',
'entity',
array(
'class' => "BaseBundle:Organization",
'property' => 'name',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('o')
->select('o')
->add('groupBy', 'o.name');
},
'required' => false
))
->add('addresses', 'collection', array(
'type' => new AddressType(),
'allow_add' => true,
'by_reference' => false
))
->add('customValues', 'collection', array(
'type' => new CustomValueType(),
'allow_add' => true,
'by_reference' => false
));
}
OrganizationController.php:
public function createAction(Request $request)
{
$entity = new Organization();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$this->get('session')->getFlashBag()->add(
'success',
'Your changes were saved!'
);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('organization_show', array('id' => $entity->getId())));
}
return $this->render('BaseBundle:Organization:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
private function createCreateForm(Organization $entity)
{
$form = $this->createForm(new OrganizationType(), $entity, array(
'action' => $this->generateUrl('organization_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
public function newAction()
{
$entity = new Organization();
$cv = new CustomValue();
$entity -> getCustomValues()->add($cv);
$form = $this->createCreateForm($entity);
return $this->render('BaseBundle:Organization:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
对于关系组织< - >解决它有效,但这里没有。我不知道,有什么不对。你看到了什么吗?
答案 0 :(得分:0)
所以表结构中缺少外键,或者那里有外键,但是关系id仍为null? 我的第一个提示是尝试从Organization类中删除setter-getter-add-remove函数,然后运行app / console doctrine:generate:entities BaseBundle:Organization。我遇到了类似的问题,结果发现我的一个访问函数中有一个拼写错误。它只是一个提示:(
答案 1 :(得分:0)
我已经找到了解决方案。问题是,我必须自己在customValue中添加引用。这意味着我必须添加这些行:
foreach ( $entity->getCustomValuesn() as $cv ) {
$cv->setOrganization($entity);
}
这是控制器:
if ($form->isValid()) {
foreach ( $entity->getCustomValuesn() as $cv ) {
$cv->setOrganization($entity);
}
$this->get('session')->getFlashBag()->add(
'success',
'Your changes were saved!'
);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
这是一些解释,我发现:
https://github.com/symfony/symfony/issues/3201
http://forum.symfony-project.org/viewtopic.php?f=23&t=35914