起初:抱歉我的英语不好: - )
初学者需要帮助!
我有3个实体/表格:
表:
contact_person
id, title, firstname, surname, adress_id
address
id, street, zip, city, country_id
country (fix values)
id, name, code
1, Austria, AT
2, Germany, DE
...
实体:
/**
* ContactPerson
*
* @ORM\Table(name="contact_person")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class ContactPerson
{
/**
* @var integer $contactPersonId
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $contactPersonId;
/**
* @ORM\Column(name="title", type="string", columnDefinition="ENUM('m', 'w')")
*/
private $title;
/**
* @ORM\Column(name="firstname", type="string", length=255)
*/
private $firstName;
/**
* @ORM\Column(name="surname", type="string", length=255)
*/
private $surName;
/**
* @ORM\OneToOne(targetEntity="Trac\TracBundle\Entity\Address", cascade={"persist"})
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
*/
protected $address;
...
}
/**
* Address
*
* @ORM\Table(name="address")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Address
{
/**
* @var integer $addresssId
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $addresssId;
/**
* @ORM\Column(name="street", type="string", length=255)
*/
private $street;
/**
* @ORM\Column(name="zip", type="string", length=255)
*/
private $zip;
/**
* @ORM\Column(name="city", type="string", length=255)
*/
private $city;
/**
* @ORM\ManyToOne(targetEntity="Trac\TracBundle\Entity\Country", inversedBy="addresses", cascade={"persist"})
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
*/
private $country;
...
}
/**
* Country
*
* @ORM\Table(name="country")
* @ORM\Entity
*/
class Country
{
/**
* @var integer $countryId
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $countryId;
/**
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\Column(name="code", type="string", length=255)
*/
private $code;
...
}
1表格:
ContactPersonType:
$builder->add('title', 'choice', array('choices' => array('m' => 'male', 'w' => 'female')));
$builder->add('firstname', 'text');
$builder->add('surname', 'text');
$builder->add('address', new \myBundle\Form\AddressType(), array(
'data_class' => 'myBundle\Entity\Address'
));
地址类型:
$builder->add('street', 'text');
$builder->add('zip', 'text');
$builder->add('city', 'text');
$builder->add('country', 'entity', array(
'class' => 'myBundle\Entity\Country',
'property' => 'name'
));
CountryType:
$builder->add('country', 'entity', array(
'class' => 'myBundle\Entity\Country',
'property' => 'name'
));
当我执行表单发布时,控制器将请求数据成功存储在表中。 一切正常!
ContactPersonController:
$form->handleRequest($request);
if($form->isValid()) {
$contact_person = $form->getData();
$em->persist($contact_person);
$em->flush();
}
结果表:
contact_person
id, title, firstname, surname, adress_id
1, Mr, Ted, Tester, 1
address
id, street, zip, city, country_id
1, myStreet, 12345, Berlin, 2
country
id, name, code
1, Austria, AT
2, Germany, DE
问题:
当我将联系人实体存储在会话中并从另一个控制器调用$ em-> persist()时,国家/地图映射失败:
ContactPersonController(将联系人 - 对象存储到会话中):
$form->handleRequest($request);
$session = $request->getSession();
if($form->isValid()) {
$contactPersons[] = $form->getData();
//Add to session
$session->set('contactPersons', $contactPersons);
}
AnotherController:
...
if($form->isValid()) {
$customer = $form->getData();
//Customer
$em->persist($customer);
$em->flush();
//Get contact persons from session
$contactPersons = $session->get('contactPersons');
//Save contact persons to tables
if(is_array($contactPersons) && !empty($contactPersons)) {
foreach($contactPersons as $key => $contactPerson) {
$em->persist($contactPerson);
$em->flush();
}
}
}
...
表格错误的结果
contact_person
id, title, firstname, surname, adress_id
1, Mr, Ted, Tester, 1
address
id, street, zip, city, country_id
1, myStreet, 12345, Berlin, 3
country
id, name, code
1, Austria, AT
2, Germany, DE
3, Germany, DE <= Create a new row... BUT WHY??? it should reference the id 2 by mapping into address (country_id)
寻求帮助,
刚多
答案 0 :(得分:1)
当您在会话中存储doctrine实体时,它们将与EntityManager分离。您需要将merge已存在于数据库中的任何实体返回到EntityManager中。像这样的东西(必要时检查null):
foreach($contactPersons as $key => $contactPerson) {
$mergedCountry = $em->merge($contactPerson->getAddress()->getCountry());
$contactPerson->getAddress()->setCountry($mergedCountry);
$em->persist($contactPerson);
}
$em->flush();