I have a Product class that has many fields for it for ManyToMany, such as ingredients, sizes, views, etc. A total of about 14 different fields. Not all fields relate to each product.
I have a display configured this way
Class product { private $species;
This works great for many / many.
The problem is in my table product_ingredients I need to add an additional field, that is, I need to switch from ManyToMany to OneToMany / ManyToOne So,
/** * @var ProductIngredient[] * * @ORM\OneToMany(targetEntity="ProductIngredient", mappedBy="product") * @ORM\JoinColumn(name="productId", referencedColumnName="id") */ private $ingredients;
Now my ProductIngredient object looks like this
/** * @var IngredientType * @ORM\ManyToOne(targetEntity="IngredientType", fetch="EAGER") * @ORM\JoinColumn(name="ingredientTypeId", referencedColumnName="id") */ private $ingredientType; /** * @var Ingredient * * @ORM\ManyToOne(targetEntity="Ingredient", inversedBy="products", fetch="EAGER") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="ingredientId", referencedColumnName="id") * }) */ private $ingredient; /** * @var Product * * @ORM\ManyToOne(targetEntity="Product", inversedBy="ingredients") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="productId", referencedColumnName="id") * }) */ private $product;
So, in my product class for views, I use @ORM \ OrderBy, so the view is already ordered. Is there a way, somehow I can do this for my field of ingredients?
Or am I wrong in my logic and shouldn't they be fields in the product class and should just look for the repository instead?
I wanted this to be easy, so I could scroll through objects like $product->getIngredients() instead of doing
$ingredients = $this->getDoctrine()->getRepository('ProductIngredient')->findByProduct($product->getId());