简体   繁体   中英

Get Multiple Entities from Symfony route annotation with the same column name

I have two Doctrine Entities. Project and Story, both of which have a unique column name Alias. I want to fetch both project and story entities (In this case i actually want only the Story object but the project alias will be dynamic). I tried with the @Entity property:

    /**
     * @Route("/project/{Alias}/{StoryAlias}", name="front-project-story-page" )
     * @Entity("Story", expr="repository.findBy(['Alias'=>StoryAlias])")
     */
    public function FrontProjectStoryPage(Project $project,Story $story)
    {
     ....
    }

But it prompts this error

An exception has been thrown during the rendering of a template 
("[Semantical Error] Annotation @Entity is not allowed to be declared on method. 
You may only use this annotation on these code elements: CLASS in /home/../config/routes/../../src/Controller/ 
(which is being imported from "/home/../config/routes/annotations.yaml"). 
Make sure annotations are installed and enabled.").

How can i fetch entities that may have same column name?

According to the documentation you can do like this

mapping: Configures the properties and values to use with the findOneBy() method: the key is the route placeholder name and the value is the Doctrine property name:

/**
 * @Route("/blog/{date}/{slug}/comments/{comment_slug}")
 * @ParamConverter("post", options={"mapping": {"date": "date", "slug": "slug"}})
 * @ParamConverter("comment", options={"mapping": {"comment_slug": "slug"}})
*/
public function showComment(Post $post, Comment $comment)
{
}

I believe you are using Doctrine\ORM\Annotation\Entity instead of Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity .

however, as someone else have mentioned, its also possible to achieve the same result using Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter :

    /**
     * @Route("/project/{alias}/{story-alias}", name="front-project-story-page" )
     *
     * @ParamConverter("project", options={"mapping": {"alias": "alias"}})
     * @ParamConverter("story", options={"mapping": {"story-alias": "alias"}})
     */
    public function FrontProjectStoryPage(Project $project,Story $story)
    {
     ....
    }

read more: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrineconverter-options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM