0

Im using the FOSUserBundle in symfony2, it's works very well but I have to create for example another table like Entry, with fields:

 user_id, description,...

I need to make a relationship between the user table and Entry table. What is the best way to make the relationship One to Many? Which class I should extend?

I get this error:

Entry has no association named ApplicationSonataUserBundle:User

My code is:

   /**
 *
 * @ORM\ManyToOne(targetEntity="\Application\Sonata\UserBundle\Entity\User",  inversedBy="entry")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/

protected $users;

I use this instruccion:

   $em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery('SELECT b
                               FROM LoggerFrontendBundle:Entry a JOIN a.ApplicationSonataUserBundle:User b
                       ') ;

Thanks

Faery
  • 4,552
  • 10
  • 50
  • 92
JERC
  • 1,584
  • 3
  • 17
  • 38

1 Answers1

0

User Entity:

/**
* @ORM\OneToOne(targetEntity="path to new Entity", mappedBy="user", cascade={"persist"})
*/
protected $name;

New Entity:

/**
* @ORM\OneToOne(targetEntity="path to User Entity", inversedBy="name")
*/
protected $user;

From the controller

$em = $this->getDoctrine()->getEntityManager();
// when you query users, you get the information from the new Entity.
$users = $em->getRepository('ApplicationSonataUserBundle:User')->findAll();

return array('users' => $user); // return the array of user information

Twig:

{% for user in users %} // loop through users<br/>

{{ user.name.getName }} // you can call user.getname or user.getEmail , you can add the name from the Entity on and get the fields from the new Entity like user.name.getName etc...

{% endfor %} //end loop
Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
Dave Mascia
  • 1,364
  • 10
  • 14