How to place an order for OneToMany / ManyToOne

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 { /** * @var Species[] * @ORM\ManyToMany(targetEntity="Species") * @ORM\JoinTable(name="product_species", * joinColumns={@ORM\JoinColumn(name="productId", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="speciesId", referencedColumnName="id")} * ) * @ORM\OrderBy({"name" = "asc"}) */ 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()); 
+19
symfony doctrine2
Sep 18
source share
4 answers

in the Product object, just the orderBy adad for the relation of ingredients

 /** * ... * @ORM\OrderBy({"some_attribute" = "ASC", "another_attribute" = "DESC"}) */ private $ingredients; 
+61
Sep 19 '12 at 13:45
source share

Well, I came up with a hacker way. Since I really only care about output sorting, I did a basic branch extension

 use Doctrine\Common\Collections\Collection; public function sort(Collection $objects, $name, $property = null) { $values = $objects->getValues(); usort($values, function ($a, $b) use ($name, $property) { $name = 'get' . $name; if ($property) { $property = 'get' . $property; return strcasecmp($a->$name()->$property(), $b->$name()->$property()); } else { return strcasecmp($a->$name(), $b->$name()); } }); return $values; } 

I would like to avoid this hack, although I still would like to know the real solution

+6
Sep 20 '12 at 2:27
source share

You should use the 'query_builder' option in your form: http://symfony.com/doc/master/reference/forms/types/entity.html#query-builder The parameter value may look something like this:

 function(EntityRepository $er) { return $er->createQueryBuilder('i')->orderBy('i.name'); } 

Remember to add the "use" statement for EntityRepository

+3
Apr 20 '13 at 9:21
source share

As I was given a task on a similar issue, you should write a connection request for the ORM order to work, see here for reference. http://www.krueckeberg.org/notes/d2.html

However, I solved the problem with the I-shaped client-side sort plugin, jquery datatable and tinysort are both excellent :-)

0
Jan 15 '13 at 15:53
source share



All Articles