当单元格与另一个表相关时,如何创建新对象?就我而言,存在一个包含状态的表,例如 id=1,state=active;id=2,state=inactive。
我的实体/States.php
class States
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
....
实体/用户.php
....
/**
* Set state
*
* @param \Frontend\AccountBundle\Entity\States $state
* @return User
*/
public function setState(\Frontend\AccountBundle\Entity\States $state = null)
{
$this->state = $state;
return $this;
}
我的帐户控制器:
....
$user = new User();
$em = $this->get('doctrine')->getEntityManager();
$state = $this->getDoctrine()->getRepository('FrontendAccountBundle:States')->find(1);
$user->setEmail($formData->getEmail());
$user->setStateId(1);
$em->persist($user);
$em->flush();
这是行不通的,而且方法很复杂:http ://symfony.com/doc/current/book/doctrine.html#relationship-mapping-metadata 。在 symfony1.4 中这太容易了。