我有用户和电话实体之间有OneToMany关系。Symfony2嵌入式实体
我的用户实体:
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字段和额外的其他领域与实体有关。
在我的控制,我这样做:
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',
);
}
}
在我的树枝模板:
<div>
{{ form_errors(form.phone) }}
{{ form_label(form.phone, null, { 'attr': {'class': 'span-3'} }) }}
{{ form_widget(form.phone, { 'attr': {'class': 'text'} }) }}
</div>
我是什么试图基本上是允许用户有多个电话号码,因此在用户和电话之间具有OneToMany关系。在我的表单中,我想在我的表单中显示“电话”实体的“号码”字段,然后将单个电话记录添加到相应的用户。
目前,与上面的代码,形式呈现,但显示:
“学说\ COMMON \收藏\ ArrayCollection的@ 0000000062cf59cf00000000e32fc883” 在手机领域。
将表单类中的'phone'更改为'collection'结果为Twig_Runtime_Error“Phone无法转换为字符串”500内部服务器错误。我试图将手机渲染为'form.phone.number',以便获得Phone实体中的数字元素,但它说:
“对象”Symfony \ Component \ Form \ FormView“的方法”数字“不存在“
我也尝试将表单类中的'phone'类型更改为'entity',并添加了数组以反映Entity名称空间,但它总是会显示”您需要管理您的实体“错误。
我有点迷茫,不知道这应该怎么做,我读过的地方,我可以分别实例化用户和电话对象,进行更改,然后逐个坚持他们。我不确定这是否是最佳做法,我的方法是创建一个用户对象,向其中添加电话记录,然后保留用户对象。