Symfony2嵌入式实体

Azi*_*ari 5 forms doctrine symfony

我有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;
    }

    /* ..... */

}
Run Code Online (Sandbox Code Playgroud)

和我的电话实体:

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;
    }

    /* ..... */
}
Run Code Online (Sandbox Code Playgroud)

我删除了部分代码以使其有点短,我有一个Id字段和与实体相关的其他字段.

在我的控制器中,我这样做:

public function showFormAction($entity)
{
    /* ..... */
    $user  = new User();
    $phone = new Phone();
    $user->addPhone($phone);

    $form = $this->createForm(new UserRegister(), $user);
    /* ..... */
}
Run Code Online (Sandbox Code Playgroud)

在我的表单类中:

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',
        );
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的树枝模板中:

<div>
    {{ form_errors(form.phone) }}
    {{ form_label(form.phone, null, { 'attr': {'class': 'span-3'} } ) }}
    {{ form_widget(form.phone, { 'attr': {'class': 'text'} } ) }}
</div>
Run Code Online (Sandbox Code Playgroud)

我正在尝试做的基本上是允许用户拥有多个电话号码,因此在用户和电话之间具有OneToMany关系.在我的表单中,我想在我的表单中显示"电话"实体的"号码"字段,然后将单个电话记录添加到相应的用户.

目前,使用上面的代码,表单正在呈现,但显示:

"电话"字段中的"Doctrine\Common\Collections\ArrayCollection @ 0000000062cf59cf00000000e32fc883".

在表单类中将"phone"更改为"collection"会导致Twig_Runtime_Error"Phone无法转换为字符串"500内部服务器错误.我试图将手机渲染为'form.phone.number'以获取Phone实体中的数字元素,但是它说:

"方法"编号"对象"Symfony\Component\Form\FormView"不存在"

我还尝试将表单类中的"phone"类型更改为"entity",并添加了数组以反映Entity名称空间,但它始终显示"您需要管理实体"错误.

我有点迷茫并且不确定这应该怎么做,我已经读过某个地方,我可以单独实例化User和Phone对象,进行更改,然后逐个持久化.我不确定这是否是最好的做法,我的方法是创建一个User对象,向其添加一个Phone记录,然后保留User对象.