我有 User 和 Phone 实体,它们之间存在 OneToMany 关系。
我的用户实体:
namespace Project\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Project\UserBundle\Entity\User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User
{
/* ..... */
/**
* @var Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany(targetEntity="Phone", mappedBy="user")
*/
private $phone;
public function __construct()
{
$this->phone = new ArrayCollection();
}
/**
* Add phone
*
* @param Project\UserBundle\Entity\Phone $phone
*/
public function addPhone(\Project\UserBundle\Entity\Phone $phone)
{
$this->phone[] = $phone;
}
/**
* Get phone
*
* @return Doctrine\Common\Collections\Collection
*/
public function getPhone()
{
return $this->phone;
}
/* ..... */
}
还有我的电话实体:
namespace Project\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Project\UserBundle\Entity\Phone
*
* @ORM\Table(name="user_phone")
* @ORM\Entity
*/
class Phone
{
/* ..... */
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="phone")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @var string $number
*
* @ORM\Column(name="number", type="string", length=128)
*/
private $number;
/**
* Set user
*
* @param Project\UserBundle\Entity\User $user
*/
public function setUser(\Project\UserBundle\Entity\User $user)
{
$this->user = $user;
}
/**
* Get user
*
* @return Project\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/* ..... */
}
我删除了部分代码以使其简短一些,我有一个 Id 字段和与实体相关的其他额外字段。
在我的 Controller 中,我这样做:
public function showFormAction($entity)
{
/* ..... */
$user = new User();
$phone = new Phone();
$user->addPhone($phone);
$form = $this->createForm(new UserRegister(), $user);
/* ..... */
}
在我的表单类中:
class UserRegister extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('email', 'text')
->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'The password fields must match.',
'options' => array('label' => 'Password'),
'error_bubbling' => false)
)
->add('first_name', 'text')
->add('last_name', 'text')
->add('phone', 'text' );
}
public function getName()
{
return 'register';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Project\UserBundle\Entity\User',
);
}
}
在我的 Twig 模板中:
<div>
{{ form_errors(form.phone) }}
{{ form_label(form.phone, null, { 'attr': {'class': 'span-3'} } ) }}
{{ form_widget(form.phone, { 'attr': {'class': 'text'} } ) }}
</div>
我想要做的基本上是允许用户拥有多个电话号码,因此用户和电话之间存在一对多关系。在我的表单中,我想在表单中显示“电话”实体的“号码”字段,然后向相应的用户添加一条电话记录。
目前,使用上面的代码,表单正在呈现但显示:
电话字段中的“Doctrine\Common\Collections\ArrayCollection@0000000062cf59cf00000000e32fc883”。
在表单类中将 'phone' 更改为 'collection' 会导致 Twig_Runtime_Error "Phone could not be convert to string"500 内部服务器错误。我试图将电话呈现为“form.phone.number”,以便在电话实体中获取数字元素,但是它说:
“对象“Symfony\Component\Form\FormView”的方法“编号”不存在”
我还尝试将表单类中的“电话”类型更改为“实体”并添加数组以反射(reflect)实体命名空间,但它始终会显示“您需要管理您的实体”错误。
我有点迷茫,不知道这应该怎么做,我在某处读到过,我可以分别实例化 User 和 Phone 对象,进行更改,然后一一保留它们。我不确定这是否是最佳实践,我的方法是创建一个 User 对象,向其中添加一个 Phone 记录,然后保留 User 对象。
最佳答案
试试
/**
* @var Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany(targetEntity="Phone", mappedBy="user", cascade={"persist"})
*/
private $phone;
对于具有 OneToMany 关系的 Symfony Embed-Form,需要在注释中添加级联 ={"persist"}
关于forms - Symfony2 嵌入式实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7372220/