0

I have two entities:

ENTITY 1 - BRAND:

<?php
namespace ...;
//Here the use definitions

class Brand
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    //More properties, getters and setters
}
?>

ENTITY 2 - PRODUCT:

<?php
namespace ...;
//Here the use definitions

class Product
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Brand")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
     * })
     */
    private $brand;

    //More properties, getters and setters
}
?>

I know that I can get the products of a brand consulting PRODUCT directly, thanks to ORM. But is there a way to get all the products consulting over BRAND entity?

I mean, is there a way that adding a property into BRAND entity, something like this:

/**
 * @ORM\ManyToOne(targetEntity="Producto")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id", referencedColumnName="brand_id")
 * })
 */
private $listOfProducts;

Can I get a list of products?

1

1 Answer 1

1

Yes you can. On your Brand entity, add OneToMany column like this :

<?php
namespace ...;
//Here the use definitions

class Brand
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    //More properties, getters and setters
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

It was what I want,Works perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.