18

I go with Symfony2 docs. It's said that adding

/**
 * @ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository")
 */

in my entity file and running php app/console doctrine:generate:entities Acme should create the ProductRepository file. It doesn't. I can't clarimy this more, it's just doesnt create that file, just recreates those entity files that were there before.

Tomek Buszewski
  • 7,659
  • 14
  • 67
  • 112
  • How are you checking that files do not exist? If it is inside an IDE, try refreshing. The command does not say anything about generating Repository files, but generates them anyway. – smottt Jan 02 '13 at 02:13
  • Both in IDE and thru Windows' explorer. – Tomek Buszewski Jan 02 '13 at 07:01

6 Answers6

26

I found the answer here: http://brentertainment.com/other/docs/book/doctrine/orm.html

If you have already generated your entity class before adding the repositoryClass mapping, you have to create the class on your own. Fortunately, it’s pretty easy. Simply create the class in the Repository directory of your bundle and be sure it extends Doctrine\ORM\EntityRepository. Once you’ve created the class, you can add any method to query your entities.

Simple, we have to do it by hand because we have already run this once

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Dave_____1
  • 348
  • 5
  • 13
  • 2
    I am using Symfony 2.6 and it seems that this is no longer the case. I added the repository to the annotation on my entity and Doctrine generated the repository even though I had generated the entity without a repository before. – Chris Jan 14 '15 at 10:41
  • Yes Chris, this is not the case: you are right. For me the generation still doesn't work. In the documentation is clearly followed the path explained in the question by Tomek: first you create the Entity class (Products in the docu examples); then, after the repositoryClass is specified in annotations, you rerun the command doctrine:generate:entities and Doctrine should create a new file ProductRepository.php (that currently doesn't exist, as only Product.php exists, and was generated the first time the generate command was launched. – Aerendir Feb 24 '15 at 12:09
  • 2
    In symfony 2.7 you use this command to generate both Entity and Repository classes: "php app/console doctrine:generate:entity --entity VisitorData --with-repository" – Dung Oct 13 '15 at 17:03
8

You can try to specify a particular bundle:

php app/console doctrine:generate:entities AcmeStoreBundle

Note that i have the full bundles name.

This would help even if you run doctrine:generate:entities before.

Andrey Kryukov
  • 765
  • 1
  • 10
  • 23
8

If you are using orm.yml files to generate your entities, you can define the repositoryClass, and then generate the entities again:

Acme\StoreBundle\Entity\Product:
type: entity
table: product
...
repositoryClass: Acme\StoreBundle\Entity\ProductRepository
...

And then run:

php app/console doctrine:generate:entities AcmeStoreBundle
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
glerendegui
  • 1,457
  • 13
  • 15
2

Super easy solution to this:

Generate an entity if you haven't already:

php app/console doctrine:generate:entity --entity="AppBundle:EntityName" --fields="id:string(255) content:text(100)"

Now modify these comment lines to your previously generated Entity:

* @ORM\Table(name="TABLENAME")
* @ORM\Entity(repositoryClass="AppBundle\Entity\EntityNameRepository")

Now, just run:

php app/console doctrine:generate:entities AppBundle:EntityNameRepository

Now you have an entity and repository. :)

Dovy
  • 1,278
  • 10
  • 19
1

To get rid of this problem and generate repo classes, you can temporary modify the end of the following file : symfony\vendor\doctrine\doctrine-bundle\Doctrine\Bundle\DoctrineBundle\Command\generateEntitiesDoctrineCommand.php

if ($m->customRepositoryClassName 
   && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
     $repoGenerator->writeEntityRepositoryClass(
        $m->customRepositoryClassName, $metadata->getPath());
}

with the following code :

if (true) { 
   $output->writeln(
     sprintf('  > AND Repository <comment>%s</comment>', $m->name . "Repository")
   );           
   $repoGenerator->writeEntityRepositoryClass(
     $m->name . "Repository", $metadata->getPath());
} 

Some explanations : in this code,

  • the if condition is simplified with 'if (true)' (and could finally be completely suppressed if you want)
  • $m->customRepositoryClassName is replaced by $m->name."Repository"
  • I added some output to be well informed (in the terminal window) when the repo files are generated.

If you don't use the 'if(true)' condition, and want to check things by yourself, you can also add a facultative else case with an output to get well informed in the future :

   else {
       $output->writeln(sprintf('  > NO repository generated for this class'));
    }

After the modifications, you can re-run the command as usual :

php app/console doctrine:generate:entities AcmeStoreBundle

This is a temporary code, because the problem is not very clear for me until now, the only things I see is that it seems to come from $m->customRepositoryClassName which returns an empty string. So, to find another and definitive solution, a way could be to check the method customRepositoryClassName of the metadata object...

Astucieux
  • 149
  • 1
  • 7
-3

based in Astucieux's answer:

if (true) { 
    $fullRepositoryClassName = $name . "\\Repository\\" . $basename . "Repository";
    $output->writeln(
        sprintf('  > AND Repository <comment>%s</comment>', $fullRepositoryClassName)
    );           
    $repoGenerator->writeEntityRepositoryClass(
        $fullRepositoryClassName, $metadata->getPath());
}