custom repository class in symfony2

后端 未结 6 2008
刺人心
刺人心 2020-12-05 17:40

I\'m new in symfony2.I created a repository class when I created an entity class through command line.But I couldn\'t access my custom functions in that repository class. ho

相关标签:
6条回答
  • 2020-12-05 18:00

    The manual has a nice step by step guide ... http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

    • First define the repository class in the annotations / yaml configuration
    • Create the class
    • Create the function
    • Then call the newly created function ....
    0 讨论(0)
  • 2020-12-05 18:03

    I think you just forgot to register this repository in your entity. You just have to add in your entity configuration file the repository class.

    In src/Mypro/symBundle/Resources/config/doctrine/Register.orm.yml:

    Mypro\symBundle\Entity\Register:
        type: entity
        repositoryClass: Mypro\symBundle\Entity\RegisterRepository
    

    Don't forget to clear your cache after this change, just in case.

    And if you're using Annotations (instead of yml config) then instead of the above, add something like:

    /**
     * @ORM\Entity(repositoryClass="Mypro\symBundle\Entity\RegisterRepository")
    */
    

    to your Entity class to register the repository

    0 讨论(0)
  • 2020-12-05 18:07

    I simply follow to native documentation doc and look for the annotation that I selected. For example I selected yaml, added configurations to Product.orm.yml and Category.orm.yml files, also add new php attributes to Entity Methods :

    protected $products;
    
    public function __construct()
    {
        $this->products = new ArrayCollection();
    }
    

    for Category.php and

    protected $category;
    

    for Product.php

    then run php app/console doctrine:generate:entities Acme ,that successfully add new getters and setters

    0 讨论(0)
  • 2020-12-05 18:09

    xml for mapping: Update xml mapping file in folder Resource/config/doctrine, add repository-class attribute:

    <entity name="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContent" table="uv_update_page_content" repository-class="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContentRepository">
    

    http://doctrine-mongodb-odm.readthedocs.org/en/latest/cookbook/mapping-classes-to-orm-and-odm.html Then update cache:

    php app/console doctrine:cache:clear-metadata
    php app/console cache:clear
    
    0 讨论(0)
  • 2020-12-05 18:17

    First of all You don't need custom repo to do that.. You can set the order by clause in the EM getRepository findBy method:

    //$em - entity manager
    //from Doctrine documentation: findBy(criteria(array), order(array), limit, offset)
    $result = $em->getRepository('symBundle:Register')->findBy(array(), array('name' => 'ASC'))
    
    0 讨论(0)
  • 2020-12-05 18:22

    I too lost a lot of time when I got into this mess of configuration. I was configuring the repository class in my Entity as annotation mapping. The mapping was there but still the repository was not associated with the entity. When I moved the annotation mapping i.e. @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository"), to the last line, it worked.

     /*
     * User
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository")
     */
    class User
    {
    ...
    }`   
    
    0 讨论(0)
提交回复
热议问题