Reputation: 4118
I'm trying to automatically order the results of a report by the ManyToMany annotation @OrderBy
:
/**
* @ORM\ManyToMany(targetEntity="Artist", inversedBy="soundtrack", cascade={"persist", "remove"})
* @ORM\JoinTable(name="soundtrack_artist")
* @OrderBy({"name" = "ASC", "surname" = "ASC"})
**/
private $artists;
but it gives me this error:
[Semantical Error] The annotation "@OrderBy" in property
Acme\UserBundle\Entity\Soundtrack::$artists was never imported.
Did you maybe forget to add a "use" statement for this annotation?
I tried to add also:
use Doctrine\ORM\Mapping\OrderBy;
But the error remains! I'm doing something wrong?
Upvotes: 12
Views: 22076
Reputation: 181
If you include this namespace:
use Doctrine\ORM\Mapping as ORM;
This annotation will work:
@ORM\OrderBy({"date" = "ASC"})
The first key should be a mapped attribute of your class. The order should be ASC or DESC.
Please refer to Doctrine official documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/ordered-associations.html
Upvotes: 14
Reputation: 61
@ORM\OrderBy
resolve all problem, in symfony annotation its important inserting the scope of a method.
Upvotes: 3
Reputation: 1820
Not sure if you found the answer, but this worked for me:
@ORM\OrderBy
Upvotes: 24