Symfony2s doctrine:generate:entities doesn't generate repo classes - symfony

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.

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

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

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.

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

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

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

Related

Symfony doctrine:fixtures:load give ContextErrorException, Notice: Undefined variable

I can't figure out what's happening here.
I'm trying to add fixtures on a symfony project, nothing strange, fixtures seems to be well formed, and the console command :
php bin/console doctrine:fixtures:load --env=test
find my fixtures but don't want to load it since it say :
# php bin/console doctrine:fixtures:load --env=test
Careful, database will be purged. Do you want to continue y/N ?y
> purging database
> loading Covoituragesimple\AnnonceBundle\DataFixtures\ORM\AppFixtures
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Undefined variable: EjehHi1ubBYApRfaNZnEo
I have no more idea where to looks ?
What to search ? Where it comes from ?
How to solve it ?
What I tried :
I try to delete the oneToOne and oneToMany relations -->deosn't seem to change anything)
ps: I use the version of doctrine-fixtures-bundle "doctrine/doctrine-fixtures-bundle": "2.*" (i was not abble to use the last one since i use symfony-framework-bundle 3.2.13 and the last version of doctrine-fixtures ask for 3.3+).
[RESOLVED] Aoutch ! Ok my bad, my bad.
The rubber duck debbuging helped me to fix it.
$client = new Client();
$client->setName("clienttest");
$client->setEmail("clienttest#provider.com");
$client->setUsername("clienttest");
$client->setDisplayname("clienttest");
$client->setSignaturemail("blablabla.<br />");
$client->setPassword("$2y$10$EjehHi1ubBYApRfaNZnEo.aQ/aZIiTJdY2sPetVWj7P7/yQNfhe7a");
$manager->persist($client);
$manager->flush();
I used double quote instead of simple quote, so php was trying to interpret the bcrypt hash of the password and told me that the var $EjehH.... doesn't exist (which is true).
Replacing double quote with single quote solved the problem.
$client->setPassword('$2y$10$EjehHi1ubBYApRfaNZnEo.aQ/aZIiTJdY2sPetVWj7P7/yQNfhe7a');
Thanks for help.

Unable to find template Bundle:Default:index.html.twig

I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur
I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper
If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle
Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}
This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)
Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.
In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");

Drupal 8: Mismatched entity and/or field definitions

While trying to understand why my view is not displaying, I noticed the following error in the log:
I do not think it is possible to delete the URL alias from Taxonomy terms. At least I cannot find how to do this.
I have, however gone through ALL of my taxonomy terms and removed the value for this field.
I have also done the following with Pathauto:
Also, I have checked the report located at admin/reports/fields and can confirm that there are no entities that use a field called URL alias.
I have gone through each content item and ensured that they have the following setting (anyone know how to do this in bulk?). But still the error remains.
Anyone know then how I can fix this strange error?
Im not entirely sure what this command does, but it fixed the error:
drush updb --entity-updates
Since https://www.drupal.org/node/2554097, the magic in Drupal core that took care of updating entity definitions is gone. drush updb --entiy-updates is an alternative to this but it is not a silver bullet. Instead, it is safer to write database updates.
Taking the screenshot at the top as an example, here is a database update that would delete those two field definitions:
/**
* Fix taxonomy and node field definitions.
*
*/
function mymodule_update_8101() {
$manager = \Drupal::entityDefinitionUpdateManager();
if ($field = $manager->getFieldStorageDefinition('alias', 'node')) {
$manager->uninstallFieldStorageDefinition($field);
}
if ($field = $manager->getFieldStorageDefinition('alias', 'term')) {
$manager->uninstallFieldStorageDefinition($field);
}
}
Have a look at the rest of the available methods at https://www.drupal.org/node/2554097 in order to write database updates for each scenario.
use the entity_update module or the devel_entity_updates module

Doctrine: "1/1ReflectionException: Property Power\SelfBundle\Entity\Check::$part1 does not exists"

I am working with symfony. I have an entity Check. It has a relation field part.
The definition is something like:
/** #ORM\OneToMany(targetEntity="Power\SelfBundle\Entity\Friends", mappedBy="check")*/
protected $part1;
In the inverse relation (at Friendsentity) the declaration is like:
/** #ORM\ManyToOne(targetEntity="Power\SelfBundle\Entity\Check", inversedBy="part1") */
public $check;
Now for removing ambiguity I have deleted the field and replaced it by part. I have updated the schema and cleared the cache and logs.
Later I am inserting into the friends entity (table infact) and it gives an error while event calling the entity:
1/1ReflectionException: Property Power\SelfBundle\Entity\Check::$part1 does not exists
But $part1 is deleted long ago. The cache is cleared and the log is cleared too. I have searched the whole project but nowhere even the text part1 is found.
I am out of my wit!!
Can anyone please help me!!
Thanks.
The problem is fixed after lots of researching.
The problem was in doctrine metadata cache.
In my config.yml file metadata_cache_driver was set to use APC. So clearing the doctrine app/cache was not helpful.
After lots of searching - I have done
php -r "apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode');"
and BINGO it worked!!
So in my config_dev.yml I have overridden the setting to set the metadata_cache_driver to array.

how to use ACL in Symfony2

I am trying to use ACL for my project, I have not done it before. I know only the concepts, what it is and why to use it.
I run this command :
$ php app/console init:acl
and I got five tables in my database .
My question is how to use these tables, means how data will be inserted in these tables .
I also have followed steps from here
and still not getting the hang of it , please help me out .
You should'nt use table directly (but you already know), but ACL Entities instead (but it's tricky).
Some people worked on bunbles to simplify those actions. Here is an example on how to use it :
https://github.com/Problematic/ProblematicAclManagerBundle
$comment = new Comment(); // create some entity
$aclManager = $this->get('problematic.acl_manager');
// Adds a permission no matter what other permissions existed before
$aclManager->addObjectPermission($comment, MaskBuilder::MASK_OWNER, $userEntity);
// Replaces all current permissions with this new one
$aclManager->setObjectPermission($comment, MaskBuilder::MASK_OWNER, $userEntity);
$aclManager->revokePermission($comment, MaskBUILDER::MASK_DELETE, $userEntity);
$aclManager->revokeAllObjectPermissions($comment, $userEntity);
You can apply permission on objects or directly on classes (upper level)

Resources