Doctrine2 OneToMany - ManyToOne returns an empty collection with the database ok


I have many relationships of this type, but I don’t understand why this one doesn’t work.
I have representative offices and promotion units:
Delegation

Promotion

/** * Company\CBundle\Entity\Promotion * * @ORM\Entity * @DoctrineAssert\UniqueEntity("promotionCode") */ class Promotion { const REGISTER_DAYS = 30; const REGISTER_DISCOUNT = 100; /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(name="promotionCode", type="string", unique=true, nullable=true) */ protected $promotionCode; /** * @ORM\Column(name="name", type="string") */ protected $name; /** * @ORM\Column(name="description", type="string", nullable=true) */ protected $description; /** * @ORM\Column(name="days", type="integer") */ protected $days; /** * @ORM\Column(name="discount", type="float") */ protected $discount; /** * @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions") * @ORM\JoinColumn(name="delegation_id", referencedColumnName="id") */ private $delegation; /** * @ORM\ManyToOne(targetEntity="Product", inversedBy="promotions") */ private $product; /** * @var date $adquiredDate * * @ORM\Column(name="adquiredDate", type="date", nullable=true) */ private $adquiredDate; 

When I create an ad campaign in the controller, the Promotion table has a new object associated with the delegation

 private function createPromotion($delegation) { $em = $this->getDoctrine()->getEntityManager(); $promotion = Promotion::createPromotion($delegacion, Promotion::REGISTER_DAYS, Promotion::REGISTER_DISCOUNT); $em->persist($promotion); $em->persist($delegation); $em->flush(); } 

Database

 *************************** 15. row *************************** id: 32 delegation_id: 19 days: 20 discount: 50 adquiredDate: 2013-01-10 *************************** 16. row *************************** id: 33 delegation_id: 19 days: 25 discount: 50 adquiredDate: 2013-01-10 *************************** 17. row *************************** id: 34 delegation_id: 19 days: 30 discount: 50 adquiredDate: 2013-01-10 

But when I call $ delegation-> getPromotions () in another controller / action, there are no promotions, returns Doctrine \ ORM \ PersistentCollection without data.

Can anyone help please?


Edit with additional information. $ delega-> getPromotions () is empty, but looking for the promotion of this delegation and calling $ promotion-> getDelegation () returns the delegation correctly :?

+3
source share
1 answer

You tried to define the $ delegation property, for example

 /** * @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions") * @ORM\JoinColumn(name="delegation_id", referencedColumnName="id") */ private $delegation; 

See Doctrine2 Docs: Map Mapping -> Multi-to-One


There are also many typos in your code. for instance

 /** * @ORM\OneToMany(targetEntity="Promotion", mappedBy="delegacion", cascade={"all"}, orphanRemoval=true) */ protected $promotions; 

mappedBy="delegacion" must be mappedBy="delegation" .

or

 public function getDeleTacion() { return $this->deleTacion; } 

Must be

 public function getDelegation() { return $this->delegation; } 

Edit

Well, I created for you a minimalist version that worked for me. You can create it there or watch the differences with your code:

Promotion.php

 use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Promotion { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions", cascade={"persist"}) * @ORM\JoinColumn(name="delegation_id", referencedColumnName="id") */ public $delegation; /** * @ORM\ManyToOne(targetEntity="Product", inversedBy="promotions", cascade={"persist"}) * @ORM\JoinColumn(name="product_id", referencedColumnName="id") */ public $product; } 

Delegation.php

 use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Delegation { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\OneToMany(targetEntity="Promotion", mappedBy="delegation", cascade={"all"}, orphanRemoval=true) */ public $promotions; public function __construct() { $this->promotions = new \Doctrine\Common\Collections\ArrayCollection(); } } 

product.php

 use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Product { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\OneToMany(targetEntity="Promotion", mappedBy="product", cascade={"all"}, orphanRemoval=true) */ public $promotions; public function __construct() { $this->promotions = new \Doctrine\Common\Collections\ArrayCollection(); } } 

If you are now doing something like

 $delegation = new Delegation(); $product = new Product(); $promotion = new Promotion(); $promotion->delegation = $delegation; $promotion->product = $product; $em->persist($promotion); $em->flush(); $products = $em->createQuery('select p from BundleName\Entity\Product p')->execute(); $delegations = $em->createQuery('select d from BundleName\Entity\Delegation d')->execute(); var_dump(count($products[0]->promotions), count($delegations[0]->promotions)); 

In the end, you should

 int(1) int(1) 

Thus, refrence is actually saved and can be read. Phew Good luck with that!: -)

+2
source

All Articles