I
I
Igor Katkov2015-09-28 16:15:22
symfony
Igor Katkov, 2015-09-28 16:15:22

Doctrine OneToMany, is everything correct?

Hello!
There are entities (simplified):

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Brand
 *
 * @ORM\Table(name="brand")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\BrandRepository")
 */
class Brand
{
  /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * 1 brand:many products
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Product",
     *                mappedBy="brand")
     */
    private $products;


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

    /**
     * Get products
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getProducts()
    {
        return $this->products;
    }
}
?>



<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass=AppBundle\Repository\ProductRepository")
 */
class Product
{
      /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * many prodcuts : 1 brand
     * @var integer
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Brand", inversedBy="products")
     */
    private $brand;
}
?>

Is everything right? Why can't I receive products?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-09-28
@iKatkovJS

What is it?

class Brand
{
...
    public function __construct() {
        parent::__construct();

Read the doc for links. doctrine-orm.readthedocs.org/en/latest/reference/a...
And generate the entity at least once with this command
app/console doctrine:generate:entities AppBundle
(Essentially remove all methods, leave only properties) Then see what does doctrine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question