简体   繁体   中英

get attribute with Relation OneToMany doctrine

I work with symfony platform and I have 2 entities "Product and Producter" with relation OneToMany, and for the display of the list of "Product" I like to display the Logo of each "Producter"

Product

class Product
 {

/**
 * 
 * @ORM\ManyToOne(targetEntity="UserBundle\Entity\Producter",      inversedBy="products")
 * @ORM\JoinColumn(name="pr_id", referencedColumnName="id")
 */

protected $owner;

Producter

class Producter extends User
{
 /**
  * @ORM\OneToMany(targetEntity="Gestion\CentralBundle\Entity\Product", mappedBy="owner")
  */
  protected $products;

 /**
  * @ORM\Column(type="string", length=255, nullable=true) 
  */
  protected $path;

I have problem when i tried to ger the pictchers of the prodecters (varaible path does not exist )

I think you forgot to add arrayCollection into the constructor. You have to explain doctrine, that products is a collection of object!

    use Doctrine\Common\Collections\ArrayCollection;

    class Producter extends User
    {
        // ... 

       /**
        * @ORM\OneToMany(targetEntity="Gestion\CentralBundle\Entity\Product", mappedBy="owner")
        */
        protected $products;

        public function __construct() {
            $this->products = new ArrayCollection();
        }

And as it have been written before - rename pruducts to products.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM