18
votes

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.

6
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
Both in IDE and thru Windows' explorer.Tomek Buszewski

6 Answers

26
votes

I am having the same issue

But I've 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

8
votes

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
8
votes

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.

2
votes

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. :)

1
votes

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...

-3
votes

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());
}