user252849
user252849

Reputation: 67

Doctrine 2 Mapping One-to-Many / Many-to-One

I'm converting my application from doctrine1 to doctrine2.4 i did automate mapping from database and I'm missing some relations:

Table : Products productid,title ,price

Table : LocationProducts id , productid , loctionid , qty

so each LocationProducts row have 1 product, each product can be on few locations

on mapping under the locationproducts class i got :

 /**
 * @var \Products
 *
 * @ORM\ManyToOne(targetEntity="Products")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="productid", referencedColumnName="productid")
 * })
 */
private $productid;

and under product Model i don't have the relationship , i have tried to add it but its fails : its says productid mapping is already exists

this is my target query :

$qb = $em->createQueryBuilder()
    ->select('p.productid,lp.qty AS totalqty')
    ->from('Products','p')
    ->innerJoin('p.LocationProducts','lp')
    ->setFirstResult( $offset )
    ->setMaxResults( $limit )
    //getDQL
    ->getQuery();

and its returns the error:

Class Products has no association named LocationProducts

What am i missing ? thanks.

Upvotes: 0

Views: 95

Answers (2)

Anjana Silva
Anjana Silva

Reputation: 9181

You are missing the mapped by attribute annotation.

<?php
/**
 * @OneToMany(targetEntity="Phonenumber", mappedBy="user", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
 */
public $phonenumbers;

Refer to mappedBy in this link :- http://docs.doctrine-project.org/en/2.0.x/reference/annotations-reference.html#annref-onetomany

Hope this helps.

Cheers!

Upvotes: 1

epicdev
epicdev

Reputation: 406

The error message says that there is no association, so you need to add it. Currently your relation is unidirectional and you cannot join on p.LocationProducts since it does not exist.

Upvotes: 0

Related Questions