Reputation: 5084
Hi I am trying to create such mapping
class Users
{
/**
* @var integer
*
* @ORM\Column(name="id_users", type="smallint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100, nullable=true)
*/
private $name;
/**
*
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="UsersEmailAddresses", mappedBy="users")
* @ORM\JoinColumn(name="id_users", referencedColumnName="users_id")
*/
private $email;
and
class UsersEmailAddresses
{
/**
* @var integer
*
* @ORM\Column(name="id_users_email_adresses", type="smallint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=100, nullable=true)
*/
private $email;
/**
* @var \Users
* @ORM\Column(name="users_id")
* @ORM\OneToMany(targetEntity="Users")
*/
private $users;
And when I am trying to display many emails with one user like
<td>{{ entity.name }}</td>
<td><ul>
{% for e in entity.email %}
<li>{{ e.email }}</li>
{% endfor %}
</ul></td>
im getting error like An exception has been thrown during the rendering of a template ("Notice: Undefined index: users in /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1575") in /var/www/symfony/src/Test/UserBundle/Resources/views/Users/index.html.twig at line 22
22 line is the for
loop
Upvotes: 3
Views: 4361
Reputation: 52493
You have a mapping error here. Documentation: here, here and here
referencedColumname references to the ID field of your related entity.
name is the name of the extra column holding the foreign key to be created-
referencedColumnName is the foreign key of the related entity.
you define ManyToOne on the owning and OneToMany on the inverse side...
don't use OneToMany on both sides !!
the joinColumn definition has to be on your owning side ( aka the one which uses inversedBy ).
IF you had a many-to-one relation of users ... to one address ... which you dont.. The JoinColum would be on your user entity ( aka. owning side of bi-directional association )
* @ORM\JoinColumn(name="id_users", referencedColumnName="users_id")
*/
.. should be ...
* @ORM\JoinColumn(name="id_users", referencedColumnName="id_users_email_adresses")
*/
But ... In your case ...
One user shall have many adresses ... address will be the owning side of your bi-directional one-to-many relation.
User
oneToMany , mappedBy
Address
ManyToOne, inversedBy (+ JoinColumn)
Tip:
you can ommit the @JoinColumn completely as it will be auto-generated...
Entity names should usually be singular ...
Why these complicated id columns in your database ...isnt it enough to have a table user with a column id ?
Upvotes: 1