is regarding an association One-To-Many in the same table, but in MongoDB.
class Component
{
...
/**
* #MongoDB\ReferenceMany(
* discriminatorMap={
* "component"="Component"
* },
* inversedBy="components.id",
* cascade={"persist", "remove", "refresh", "merge"}
* )
*
*/
protected $components;
public function __construct()
{
$this->components = new ArrayCollection();
}
/**
* Add components
*
* #param $component
*/
public function addComponents(Component $component)
{
if(!$this->components->contains($component)){
$this->components->add($component);
}
}
...
}
This associates the components me no problem, I look at the collection and actually associates me, but when I try to regain the components, $ this->components is not an ArrayCollection, but a Object Component
any ideas?
It was resolved ...
/**
* #MongoDB\ReferenceOne(targetDocument="Component", inversedBy="children", cascade={"all"})
*/
public $parent;
/**
* #MongoDB\ReferenceMany(targetDocument="Component", mappedBy="parent", cascade={"all"})
*/
public $children;
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addChild(component $child)
{
if(!$this->children->contains($child)){
$child->parent = $this;
$this->children->add($child);
}
}
/**
* Get children
*
* #return Doctrine\Common\Collections\ArrayCollection $children
*/
public function getComponents()
{
return $this->children;
}
Related
so, I have multiple entities in my app, between them:
Product* - *Order
User* - 1UserRole
Order
class Order
{
...
/**
* #ManyToMany(targetEntity="Product", inversedBy="orders", cascade={"persist"})
* #JoinTable(
* name="orders_products",
* joinColumns={
* #JoinColumn(name="order_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #JoinColumn(name="product_id", referencedColumnName="id")
* }
* )
*/
private Collection $products;
...
public function __construct()
{
$this->products = new ArrayCollection();
}
...
public function addProduct(Product $product):self
{
$this->products[] = $product;
return $this;
}
}
Product
class Product
{
...
/**
* #ManyToMany(targetEntity="Order", mappedBy="products")
*/
protected Collection $orders;
...
public function __construct()
{
$this->orders = new ArrayCollection();
}
public function addOrder(Order $order): self
{
$this->orders[] = $order;
return $this;
}
}
User
class User
{
/**
* #ManyToOne(targetEntity="UserRole")
* #JoinColumn(name="role_id", referencedColumnName="id")
*/
protected $role;
}
And UserRole:
class UserRole
{
/** #Id #Column(type="integer") #GeneratedValue */
protected $id;
/** #Column(type="string", nullable=false, unique=true, length=20) */
protected $name;
/** #Column(type="integer", unique=true, nullable=false) */
protected $rolePriority;
/**
* Many Users have Many Stores.
* #ManyToMany(targetEntity="UserRoleAction", inversedBy="user_role_actions", fetch="EAGER", cascade={"persist"})
*/
protected Collection $userRoleActions;
/**
* UserRole constructor.
*/
public function __construct()
{
$this->userRoleActions = new ArrayCollection();
}
...
}
Ok so far, my issue is the following, when I try to save a new order that has many selected products:
class OrderService
{
public function createFromRequest(HttpRequest $request)
{
...
$products = $this->entityManager->getRepository(Product::class)->findWhatever(); //products are coming from this without issues.
foreach ($products as $product) {
$order->addProduct($product);
}
$this->entityManager->persist($order);
$this->entityManager->flush();
return $order;
}
}
I get:
Blockquote Expected value of type "UserRole" for association field "User#$role", got "__PHP_Incomplete_Class" instead.
I mean, how is this in any way related with the other entities??? I have no clue where to go to be fair. Any recommendations? Is doctrine doing some absolutely unrelated validation behind the scenes? wth?
I am new with Symfony. I am working with version 3. I simplified my example. I have 3 Tables, for this tables I created a class for each one.
Table "Person": id | name
Table "Group": id | groupname
Table "PersonGroup": id | person | group
Every Person could be in several groups and every groups can have several persons. In the table PersonGroup I connect the persons with the groups. In my Controller I want to request in which group the selected person is. But in the Dump I only have the data from the PersonGroup table and not the details from the Group (in this example the name):
PersonGroup {#485 ▼
-id: 5
-person: Person {#456 ▼
-id: 4
-name: "Adam"
-personGroups: PersistentCollection {#445 ▶}
}
-group: Group {#486 ▼
+__isInitialized__: false
-id: 5
-name: null
-personGroups: null
…2
}
}
My Controller:
/**
* #Route("/person/{personId}", name="personController")
*/
public function showAction($personId)
{
$em = $this->getDoctrine()->getManager();
$item = $em->getRepository('AppBundle:Person')
->findOneBy(['id' => $personId]);
foreach ($person->getPersonGroup() as $personGroup) {
dump($personGroup);
}
return $this->render('person/detail.html.twig', [
'person' => $person
]);
}
Person class / entity:
class Person
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string")
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\PersonGroup", mappedBy="person")
*/
private $personGroups;
/**
* Item constructor.
*/
public function __construct()
{
$this->personGroups= new ArrayCollection();
}
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getName()
{
return $this->name;
}
/**
* #param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* #return ArrayCollection*
*/
public function getPersonGroups()
{
return $this->personGroups;
}
}
Group class/Entity:
class Group
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string")
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\PersonGroup", mappedBy="group")
*/
private $personGroups;
/**
* Item constructor.
*/
public function __construct()
{
$this->personGroups= new ArrayCollection();
}
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getName()
{
return $this->name;
}
/**
* #param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* #return ArrayCollection*
*/
public function getPersonGroups()
{
return $this->personGroups;
}
}
PersonGroup class / entity:
class PersonGroup
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Person", inversedBy="group")
* #ORM\JoinColumn(nullable=false)
*/
private $person;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Group", inversedBy="person")
* #ORM\JoinColumn(nullable=false)
*/
private $group;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return Person
*/
public function getPerson()
{
return $this->person;
}
/**
* #param Person $person
*/
public function setPerson(Person $person)
{
$this->person = $person;
}
/**
* #return Group
*/
public function getGroup()
{
return $this->group;
}
/**
* #param Group $group
*/
public function setGroup(Group $group)
{
$this->group = $group;
}
}
You can access to your Group entity from PersonGroup
/**
* #Route("/person/{personId}", name="personController")
*/
public function showAction($personId)
{
$em = $this->getDoctrine()->getManager();
$item = $em->getRepository('AppBundle:Person')
->findOneBy(['id' => $personId]);
foreach ($person->getPersonGroup() as $personGroup) {
dump($personGroup->getGroup());
}
return $this->render('person/detail.html.twig', [
'person' => $person
]);
}
But you should create a custom function in Group repository and Person repository to retrieve the correct entity.
EDIT
If you want te retrieve all the Group entity you should add a parameter fetch="EAGER" to your relations, it will automatically do aan innerJoin on your relation.
class PersonGroup
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Person", inversedBy="group", fetch="EAGER")
* #ORM\JoinColumn(nullable=false)
*/
private $person;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Group", inversedBy="person", fetch="EAGER")
* #ORM\JoinColumn(nullable=false)
*/
private $group;
For example if you want to get All Groups from a Person you can do a custom repository function.
Ex:
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\Person;
/**
* GroupRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class GroupRepository extends \Doctrine\ORM\EntityRepository
{
public function findByPerson(Person $person){
return $this->createQueryBuilder('g')
->leftJoin('g.personGroups', 'pg')
->leftJoin('pg.person', 'p')
->where('p.id = :personId')
->setParameter('personId', $person->getId())
->getQuery()->getResult();
}
}
I have an application with Zend Framework2 and Doctrine2 as ORM.
I have this Entity called User:
namespace Adm\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User{
/**
* #ORM\Id
* #ORM\Column(type="integer");
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="string")
*/
protected $email;
/**
* #ORM\Column(type="string")
*/
protected $password;
/**
* #ORM\ManyToMany(targetEntity="Module")
* #ORM\JoinTable(
* name="user_module",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="module_id", referencedColumnName="id")}
* )
*/
protected $modules;
public function __construct() {
$this->modules = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #return the $id
*/
public function getId() {
return $this->id;
}
/**
* #return the $name
*/
public function getName() {
return $this->name;
}
/**
* #return the $email
*/
public function getEmail() {
return $this->email;
}
/**
* #return the $password
*/
public function getPassword() {
return $this->password;
}
/**
* #param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* #param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
/**
* #param field_type $email
*/
public function setEmail($email) {
$this->email = $email;
}
/**
* #param field_type $password
*/
public function setPassword($password) {
$this->password = $password;
}
/**
* Add module
*
* #param dm\Entity\Module
* #return User
*/
public function addModules(Module $modules = null){
$this->modules[] = $modules;
}
/**
* Get modules
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getModules(){
return $this->modules;
}
}
See the modules property is a relation Many to Many with a table called user_modules.
And i have the Entity Module as well:
namespace Adm\Entity;
use Doctrine\ORM\Mapping as ORM;
class Module{
/**
* #ORM\Id
* #ORM\Column(type="integer");
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string")
*/
private $name;
/**
* #ORM\Column(type="integer")
*/
private $status;
/**
* #return the $id
*/
public function getId() {
return $this->id;
}
/**
* #return the $name
*/
public function getName() {
return $this->name;
}
/**
* #return the $status
*/
public function getStatus() {
return $this->status;
}
/**
* #param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* #param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
/**
* #param field_type $status
*/
public function setStatus($status) {
$this->status = $status;
}
}
I receive a array variable containing the Post from a form to insert in a table. Each post element have it's property in Entity, as expected. Together, i have a $module variable which is an array containing id's of the modules. My question is: How do i insert this data in the user_module table?
My add function is this:
public function addUser($newUser){
$user = new User();
$user->setName($newUser['name']);
...
$this->getEm()->persist($user);
$this->getEm()->flush();
}
Firstly you need to have cascade={"persist"} as mentioned by #skrilled.
Then you need to retrieve the module entities from the database. You mentioned you have the id's in the $module variable.
You need a DQL statement something like this
$builder = $this->getEntityManager()->createQueryBuilder();
$builder->select('m')
->from('Adm\Entity\Module', 'm')
->where('m.id IN (:modules)')
->setParameter('modules', $modules);
$moduleEntities= $builder->getQuery()->getResult(Query::HYDRATE_OBJECT);
and in your User entity you will need
public function addModules(Array $moduleEntities)
{
foreach ($moduleEntities as $module) {
if ($module instanceof Module) {
$this->modules[] = $module;
}
}
return $this;
}
finally in your addUser method you will need to add the array of modules from the above DQL
public function addUser($newUser, $moduleEntities)
{
$user = new User();
$user->setName($newUser['name']);
....
$user->addModules($moduleEntities);
$this->getEm()->persist($user);
$this->getEm()->flush();
}
I hope this helps
You should read about using cascade. This will allow you to save/modify/remove the associated relationships and how you expect this to work.
In this case, you would want the relationship to persist since you want the associated entities to be saved when user itself is saved.
#ORM\ManyToMany(targetEntity="Module", cascade={"persist"})
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html
By default, no operations are cascaded.
The following cascade options exist:
persist : Cascades persist operations to the associated entities.
remove : Cascades remove operations to the associated entities.
merge : Cascades merge operations to the associated entities.
detach : Cascades detach operations to the associated entities.
refresh : Cascades refresh operations to the associated entities.
all : Cascades persist, remove, merge, refresh and detach operations to associated entities.
I made a web application with Symfony2, in which a User has an array correlation ManytoMany with the entity Mission. The User can upload the entity $product through a form, and one of the data passed by the form is the mission associated to the user.
There are more than only one mission for every user; so, when he uploads a $product object, he should also be able to select the mission he prefers.
To upload the file I use a form in a controller in the following way:
$form = $this->createFormBuilder($product)
->add('mission', 'entity', array('required' => true, 'multiple' => false, 'class' => 'AcmeManagementBundle:Mission', 'query_builder' => function($repository) { return $repository->createQueryBuilder('c')->orderBy('c.id', 'ASC'); },))
//...
->add('save', 'submit')
->getForm();
It works, but not fine: indeed in this field I can select all the mission stored, and not only the ones associated with the user.
I tried then with:
$form = $this->createFormBuilder($product)
->add('mission', 'collection', array('required' => true) )
//...
->add('save', 'submit')
->getForm();
It works but shows only one mission, and doesn't allow the user to select the preferred mission.
I tried also with:
->add('mission', 'collection', array('required' => true) )
but it tells me:
Neither the property "missions" nor one of the methods "getMissions()",
"isMissions()", "hasMissions()", "__get()" exist and have public access
in class "Acme\GroundStationBundle\Entity\Product".
How I should change my controller??
My product entity is:
class Product
{
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\OneToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", mappedBy="product")
*/
protected $mission;
//...
/**
* Set mission
*
* #param string $mission
* #return Product
*/
public function setMission($mission)
{
$this->mission = $mission;
return $this;
}
/**
* Get mission
*
* #return string
*/
public function getMission()
{
return $this->mission;
}
//...
UPDATE ---
I will post also my product and mission entity, as asked in the comments
This is my User Entity is:
abstract class User extends BaseUser
{
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", inversedBy="users", orphanRemoval=true)
* #ORM\JoinTable(name="user_mission")
*/
private $missions;
/**
* Add missions
*
* #param \Acme\ManagementBundle\Entity\Mission $missions
* #return User
*/
public function addMission(\Acme\ManagementBundle\Entity\Mission $missions)
{
$this->missions[] = $missions;
return $this;
}
//...
And my Mission Entity:
<?php
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* #ORM\Entity
*/
class Mission {
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
* #var integer
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\ManyToOne(targetEntity="Acme\GroundStationBundle\Entity\Product", inversedBy="mission")
* #ORM\JoinColumn(name="productId", referencedColumnName= "id")
*/
private $product;
/**
* #ORM\Column(type="string", length=600)
* #var String
*/
protected $description;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\User", mappedBy="missions", cascade={"all"}, orphanRemoval=true)
*/
private $users;
public function __construct(){
$this -> users = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Mission
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return Mission
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Add users
*
* #param \Acme\ManagementBundle\Entity\User $users
* #return Mission
*/
public function addUser(\Acme\ManagementBundle\Entity\User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* #param \Acme\ManagementBundle\Entity\User $users
*/
public function removeUser(\Acme\ManagementBundle\Entity\User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
public function __toString()
{
return $this->name;
}
/**
* Set product
*
* #param \Acme\GroundStationBundle\Entity\Product $product
* #return Mission
*/
public function setProduct(\Acme\GroundStationBundle\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* #return \Acme\GroundStationBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
}
Please take a look at my changes to your code.
When you define One(product)ToMany(missions) relation you have situation like this:
1. Product has many missions and must have an ArrayCollection of missions which you can add, remove or get all.
class Product
{
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\OneToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", mappedBy="product")
*/
// RENAME this attribute to plural. Product HAS MANY missions
// Than naming convention is "human readable" addMission and removeMission from collection but getMissions
protected $missions;
//...
//
// remove those functions:
public function setMission($mission)
//...
public function getMission()
//...
//
// add those functions:
public function __construct(){
$this->missions = new ArrayCollection();
}
public function addMission( Acme\ManagementBundle\Entity\Mission $mission )
{
$this->missions[] = $mission;
return $this;
}
public function removeMission( Acme\ManagementBundle\Entity\Mission $mission )
{
$this->missions->removeElement( $mission );
return $this;
}
public function getMissions()
{
return $this->missions;
}
//...
2. Many MissionS are owned by one product. Change only annotation
class Mission {
//...
// RENAME inversedBy to missions
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\ManyToOne(targetEntity="Acme\GroundStationBundle\Entity\Product", inversedBy="missions")
* #ORM\JoinColumn(name="productId", referencedColumnName= "id")
*/
private $product;
EDIT START
3. Opposite - MANY products Belongs to one Mission
If there is situation like you mentioned in comment, then your Annotations are wrong. Look at this fix:
class Product
{
// ...
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\ManyToOne(targetEntity="Acme\GroundStationBundle\Entity\Mission", inversedBy="products")
* #ORM\JoinColumn(name="missionId", referencedColumnName= "id")
*/
protected $mission;
// ...
// roll back this functions:
public function setMission($mission)
public function getMission()
// remove those functions
public function __construct(){
public function addMission( Acme\ManagementBundle\Entity\Mission $mission )
public function removeMission( Acme\ManagementBundle\Entity\Mission $mission )
public function getMissions()
//...
class Mission {
// ...
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\OneToMany(targetEntity="Acme\ManagementBundle\Entity\Product", mappedBy="mission")
*/
private $products;
// ...
//
// remove those functions:
public function setProduct($product)
public function getProduct()
//...
//
// add those functions:
public function __construct(){
$this->products = new ArrayCollection();
}
public function addProduct( Acme\ManagementBundle\Entity\Product $product )
{
$this->products[] = $product;
return $this;
}
public function removeProduct( Acme\ManagementBundle\Entity\Product $product )
{
$this->products->removeElement( $product );
return $this;
}
public function geProducts()
{
return $this->products;
}
//...
EDIT END
3. After that remember to:
$ php app/console doctrine:generate:entities AcmeGroundStationBundle:Product
$ php app/console doctrine:generate:entities AcmeGroundStationBundle:Mission
$ php app/console doctrine:schema:update --force
Good Luck!
By saying this:
Neither the property "missions" nor one of the methods
"getMissions()", "isMissions()", "hasMissions()", "__get()" exist and
have public access in class
"Acme\GroundStationBundle\Entity\Product".
Symfony2 is telling you that you have to set a relationship between Mission and Product entities.
Try to create a oneToMany/manyToOne relationship between those entities by setting up annotations and properties in your objects. I'm not proficient enough in annotations, but I can tell what it would look like in Yaml:
# in Product:
oneToMany:
missions:
targetEntity: Mission
mappedBy: product
# in Mission:
manyToOne:
product:
targetEntity: Product
inversedBy: missions
joinColumn:
name: productId
referencedColumnName: id
Before you test, don't forget to update your objects to your annotations:
$ php app/console doctrine:generate:entities YourBundle:Product
$ php app/console doctrine:generate:entities YourBundle:Mission
Then, tell us what happens in the commentaries. You're gonna have to do some testing before you get it working, in my opinion, but you're on the way ;)
I have an User entity and a mission entity, that are associated
In the profiler of symfony i get two errors.
For the class Acme\ManagementBundle\Entity\User I get:
The mappings Acme\ManagementBundle\Entity\User#missions and Acme\ManagementBundle\Entity\Mission#users are incosistent with each other.
For the class Acme\ManagementBundle\Entity\Mission I get:
The association Acme\ManagementBundle\Entity\Mission#users refers to the inverse side field Acme\ManagementBundle\Entity\User#users which does not exist.
I tried to solve by myself reading here but I could't.
My mission entity is:
class Mission {
/**
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\User", inversedBy="users")
*/
protected $users;
public function __construct(){
$this -> users = new ArrayCollection();
}
/**
* Add users
*
* #param \Acme\ManagementBundle\Entity\User $users
* #return Mission
*/
public function addUser(\Acme\ManagementBundle\Entity\User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* #param \Acme\ManagementBundle\Entity\User $users
*/
public function removeUser(\Acme\ManagementBundle\Entity\User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
//...
}
And my user entity:
abstract class User extends BaseUser
{
/**
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", mappedBy="users")
*/
protected $missions;
public function __construct(){
parent::__construct();
$this -> missions = new ArrayCollection();
}
/**
* Add missions
*
* #param \Acme\ManagementBundle\Entity\Mission $missions
* #return User
*/
public function addMission(\Acme\ManagementBundle\Entity\Mission $missions)
{
$this->missions[] = $missions;
return $this;
}
/**
* Remove missions
*
* #param \Acme\ManagementBundle\Entity\Mission $missions
*/
public function removeMission(\Acme\ManagementBundle\Entity\Mission $missions)
{
$this->missions->removeElement($missions);
}
/**
* Get missions
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMission()
{
return $this->missions;
}
}
You have wrong annotation in the users property in Mission class. It should be:
/**
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\User", inversedBy="missions")
*/
protected $users;