Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 Embedded Entities
    primarykey
    data
    text
    <p>I have User and Phone entities with a OneToMany relationship between them.</p> <p>My User entity:</p> <pre><code>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-&gt;phone = new ArrayCollection(); } /** * Add phone * * @param Project\UserBundle\Entity\Phone $phone */ public function addPhone(\Project\UserBundle\Entity\Phone $phone) { $this-&gt;phone[] = $phone; } /** * Get phone * * @return Doctrine\Common\Collections\Collection */ public function getPhone() { return $this-&gt;phone; } /* ..... */ } </code></pre> <p>And my Phone entity:</p> <pre><code>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-&gt;user = $user; } /** * Get user * * @return Project\UserBundle\Entity\User */ public function getUser() { return $this-&gt;user; } /* ..... */ } </code></pre> <p>I have deleted part of the code to make it a bit short, I have an Id field and extra other fields related to the entities.</p> <p>In my controller, I'm doing this:</p> <pre><code>public function showFormAction($entity) { /* ..... */ $user = new User(); $phone = new Phone(); $user-&gt;addPhone($phone); $form = $this-&gt;createForm(new UserRegister(), $user); /* ..... */ } </code></pre> <p>In my form class:</p> <pre><code>class UserRegister extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder-&gt;add('email', 'text') -&gt;add('password', 'repeated', array( 'type' =&gt; 'password', 'invalid_message' =&gt; 'The password fields must match.', 'options' =&gt; array('label' =&gt; 'Password'), 'error_bubbling' =&gt; false) ) -&gt;add('first_name', 'text') -&gt;add('last_name', 'text') -&gt;add('phone', 'text' ); } public function getName() { return 'register'; } public function getDefaultOptions(array $options) { return array( 'data_class' =&gt; 'Project\UserBundle\Entity\User', ); } } </code></pre> <p>And in my twig template:</p> <pre><code>&lt;div&gt; {{ form_errors(form.phone) }} {{ form_label(form.phone, null, { 'attr': {'class': 'span-3'} } ) }} {{ form_widget(form.phone, { 'attr': {'class': 'text'} } ) }} &lt;/div&gt; </code></pre> <p>What I'm trying to do basically is to allow a user to have multiple phone numbers, hence having a OneToMany relationship between User and Phone. In my form, I want to show 'number' field of 'Phone' entity in my form and then add a single phone record to the respective user.</p> <p>Currently, with the above code, the form is rendering but showing:</p> <p>"Doctrine\Common\Collections\ArrayCollection@0000000062cf59cf00000000e32fc883" in Phone field. </p> <p>Changing 'phone' to 'collection' in the form class results to a Twig_Runtime_Error "Phone could not be converted to string" 500 internal server error. I have tried to render phone as 'form.phone.number' in order to get number element in Phone entity, however it says:</p> <p>"Method "number" for object "Symfony\Component\Form\FormView" does not exist"</p> <p>I have also tried to change 'phone' type in the form class to 'entity' and added the array to reflect the Entity namespace, however it would always show "You need to manage your Entity" error.</p> <p>I'm kind of lost and not sure how is this supposed to be done, I have read somewhere that I can instantiate both User and Phone objects separately, make the changes, and then persist them one by one. I'm not sure if that's the best practice or not, my approach was to create a User object, add a Phone record to it, and then persist User object.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload