Symfony2存储库标准文件夹或名称空间

时间:2022-08-31 06:45:21

I am creating a Repository to use it with an Entity in a Symfony2 project, but I do not know where to store the class. I have been researching in Internet but I do not have any info about the default namespace o default folder to store the Repositories.

我正在创建一个存储库,以便与Symfony2项目中的一个实体一起使用它,但是我不知道在哪里存储类。我一直在互联网上研究,但我没有任何关于默认名称空间o默认文件夹存储库的信息。

I could "think" in two approaches:

我可以用两种方法“思考”:

  1. use Entity folder: (entity and entityRepository in the same folder)

    使用实体文件夹:(同一文件夹中的实体和实体库)

    /project/bundle/entity;

    /项目/包/实体;

  2. use Repository folder: (entity in entity folder and repository in Repository one)

    使用存储库文件夹:(实体文件夹中的实体,存储库1中的存储库)

    /project/bundle/entity; /project/entity/repository;

    /项目/包/实体;/ /项目/实体库;

There are any standard about that?

有什么标准吗?

4 个解决方案

#1


3  

You can store them anywhere you want, but the official Symfony book uses

您可以将它们存储在任何您想要的地方,但官方的Symfony图书使用

Acme/DemoBundle/Repository/

Acme / DemoBundle /仓库/

So I think that would be the more standard way

所以我认为这是更标准的方法。

#2


1  

If somehow Symfony won't find your repository, you can use annotation at entity class to define specific repository namespace\class (in example: "Acme\DemoBundle\Entity\Repository\MyEntityRepository") like this:

如果Symfony找不到您的存储库,您可以使用entity类的注释来定义特定的存储库名称空间命名空间类(例如:“Acme\DemoBundle\ entity repository \MyEntityRepository”),如下所示:

use Doctrine\ORM\Mapping as ORM;

/**
*@ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\Repository\MyEntityRepository")
*/
class MyEntity { ... }

Maybe it can be defined through YML, XML or PHP but I use annotations at entities.

也许可以通过YML、XML或PHP来定义,但我在实体中使用注释。

#3


0  

I'm now reading a tutorial about How to create a blog with Symfony2 and there they have a Repository directory where store all the repository classes like this: src/Blogger/BlogBundle/Repository/BlogRepository.php I don't know if this is the best solution, but hope it helps. If you want to have a look at the tutorial - it's here - http://tutorial.symblog.co.uk/docs/extending-the-model-blog-comments.html

我现在正在阅读一个关于如何使用Symfony2创建博客的教程,在那里他们有一个存储库目录,其中存储所有的存储库类:src/Blogger/BlogBundle/Repository/BlogRepository。我不知道这是否是最好的解决方案,但希望它能有所帮助。如果你想看一下教程——它在这里——http://tutorial.symblog.co.uk/docs/extenting -the-model-blog-comments.html

#4


0  

Without wishing to through a spanner in the monkey, the Doctrine entity generator creates the repository classes in the Entity folder:

不希望通过monkey中的扳手,Doctrine实体生成器在实体文件夹中创建存储库类:

php app/console doctrine:generate:entity

php应用程序/控制台学说:生成:实体

Entity:

实体:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Test
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\TestRepository")
 */
class Test
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

Repository:

存储库:

namespace AppBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * TestRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TestRepository extends EntityRepository
{
}

#1


3  

You can store them anywhere you want, but the official Symfony book uses

您可以将它们存储在任何您想要的地方,但官方的Symfony图书使用

Acme/DemoBundle/Repository/

Acme / DemoBundle /仓库/

So I think that would be the more standard way

所以我认为这是更标准的方法。

#2


1  

If somehow Symfony won't find your repository, you can use annotation at entity class to define specific repository namespace\class (in example: "Acme\DemoBundle\Entity\Repository\MyEntityRepository") like this:

如果Symfony找不到您的存储库,您可以使用entity类的注释来定义特定的存储库名称空间命名空间类(例如:“Acme\DemoBundle\ entity repository \MyEntityRepository”),如下所示:

use Doctrine\ORM\Mapping as ORM;

/**
*@ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\Repository\MyEntityRepository")
*/
class MyEntity { ... }

Maybe it can be defined through YML, XML or PHP but I use annotations at entities.

也许可以通过YML、XML或PHP来定义,但我在实体中使用注释。

#3


0  

I'm now reading a tutorial about How to create a blog with Symfony2 and there they have a Repository directory where store all the repository classes like this: src/Blogger/BlogBundle/Repository/BlogRepository.php I don't know if this is the best solution, but hope it helps. If you want to have a look at the tutorial - it's here - http://tutorial.symblog.co.uk/docs/extending-the-model-blog-comments.html

我现在正在阅读一个关于如何使用Symfony2创建博客的教程,在那里他们有一个存储库目录,其中存储所有的存储库类:src/Blogger/BlogBundle/Repository/BlogRepository。我不知道这是否是最好的解决方案,但希望它能有所帮助。如果你想看一下教程——它在这里——http://tutorial.symblog.co.uk/docs/extenting -the-model-blog-comments.html

#4


0  

Without wishing to through a spanner in the monkey, the Doctrine entity generator creates the repository classes in the Entity folder:

不希望通过monkey中的扳手,Doctrine实体生成器在实体文件夹中创建存储库类:

php app/console doctrine:generate:entity

php应用程序/控制台学说:生成:实体

Entity:

实体:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Test
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\TestRepository")
 */
class Test
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

Repository:

存储库:

namespace AppBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * TestRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TestRepository extends EntityRepository
{
}