Symfony Flex - Doctrine generate entity and entities runtime exception - php

I am testing the new Symfony 4 schema with Flex and I'm having a big trouble while creating a Doctrine Entity or creating the Entity Entities.
In previous versions of Symfony, you can create the entities with the command:
php bin/console doctrine:generate:entities
and
php bin/console doctrine:generate:entities App:MyBundle:MyEntity
In this version (Symfony 3.3-dev with the new Flex skeleton) the new folder structure "removes" the old Bundle structure, and when executing the above commands to create an Entity in ./src/Entity it returns and error like this:
[RuntimeException]
Can't find base path for "App\Entity\ExampleEntity" (path: "/mnt/c/.../src/Entity", destination: "/mnt/c/.../src/Entity").
The question is, that is any way to generate an Entity, or the Entity Entities focusing to a path and not with the PSR-4 autoload directive.
Thanks in advance!!!

Not a fix by any means but a workaround till they catch up. Very much a cowboy method.
Setup a new project with the standard Symfony file structure.
Copy your basic objects into the entity folder.
Generate the entitles there php bin/console doctrine:generate:entities AppBundle
Copy and paste them into your new Symfony Flex project entity folder.
Using an IDE do a find and replace in the folder for AppBundle with App
Sorry, it's far from ideal, but the easiest method I've found rather than having to generate all the getter/setters by hand.
UPDATE: 25/01/2017
As of 4.x the whole idea of doctrine:generate:entities has been dropped (In this issue after a long read you'll notice that Doctrine has dropped the commands completely, hence the new Symfony maker bundle).
Apparently, it's viewed as bad practice. Instead, they've released a new package called maker, which essentially creates the entity class for you but without the getters/setters, and the new suggested practice is to rely on your IDE to auto-generate the Getters and Setters for you.
This works except for the constructor for ArrayCollections for #OneToMany, but it doesn't take much to add these by hand.
What I've said above is also reflected in the Symfony documentation.
Generating Getters and Setters
Doctrine now knows how to persist a Product object to the database.
But the class itself isn't useful yet. All of the properties are
private, so there's no way to set data on them!
For that reason, you should create public getters and setters for all
the fields you need to modify from outside of the class. If you use an
IDE like PhpStorm, it can generate these for you. In PhpStorm, put
your cursor anywhere in the class, then go to the Code -> Generate
menu and select "Getters and Setters":

I had the same problem. Here an other solution.
Temporarily, change Doctrine\Bundle\DoctrineBundle\Mapping:getBasePathForClass
After
$namespace = str_replace('\\', '/', $namespace);
add:
$namespace = str_replace('App/', '', $namespace);
Entities will be created in src/App/Entity/. After the copy-paste, remove src/App.

Related

How to use an existing Database with Symfony 6 and Doctrine

I want to use an existing MariaDB with a new Symfony 6 project and Doctrine.
Unfortunately, I don't understand how to create the entities and repository from an existing database. The database is very complex and has a lot of relations. Is there an importer for this?
The following command seems to be deprecated. It works, but only creates the entities (but probably not completely correct).
symfony console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
Thanks for your help.
You can use this kind of command :
symfony console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
But be aware that this solution is deprecated.
What Symfony's team recommend is to create you entities yourself using the maker bundle.
Keep in mind that the maker only generate PHP classes, so you shall be able to reproduce your entities this way.
It's gonna take you some time, but it's the best way to do actually.
Just be careful with the naming of your relations (especially with the many to many pivot tables) wich may not be as generic as what the maker bundle generate

get_declared_classes in symfony 3.4

I have app in Symfony 3.4 and I need to get all classes registered in my AppBundle namespace.
I use get_declared_classes(). I've created custom namespace AppBundle\MyCustom and I keep there some classes. The problem is that symfony caches get_declared_classes() and it does not cache my custom namespace. When I use get_declared_classes() for the first time (after remove of cache files) I get all my custom classes, but I am getting a problem when I run script for next time. Any ideas?
here is repo with a problem:
https://github.com/webostin/getdeclaredclasses
The problem is that symfony caches get_declared_classes()
This is a PHP function, Symfony has no control over it.
The problem is might related to autoloading. On the 1st load, Symfony has to build the cache, so includes your class. With a warm cache this is not necessary, so your class is not autoloaded. get_declared_classes wont include your classes.
You can observe this behavior by putting $class = new MyTool(); in the 1st line of your execute method. This will trigger the autoloader, and your class will show up in declared classes.
Solution: It seems you want to get all classes implementing an interface. You can use DI for this.
Create a custom tag, and a manager class, and then use the manager class to access all tagged services in you command.
More here: http://symfony.com/doc/current/service_container/tags.html#creating-custom-tags

Generate Doctrine entity getters/setters for a reusable Symfony bundle

I'm creating a Symfony bundle in isolation, outside of any Symfony installation, to be used across several of my projects. I'm new to this type of workflow in Symfony and I'm a bit confused about how to best approach it.
I know I can write unit tests in order to test the functional side of the bundle, but I've also mapped out about 25 Doctrine entities that I would rather not have to manually define the getters/setters for.
I assumed I'd be able to install the Composer dependancies and then use vendor/bin/doctrine to generate them, but Doctrine throws an error, saying I should define a config-cli.php file, which is intended to instantiate an entity manager, which requires a connection.
That's fine, but given there is no actual database (in theory), I don't want to define a connection. I just want to generate the entities and test my services with PHPUnit, and then load the bundle into an actual Symfony installation later.
Am I going about this wrong? An article explaining the workflow would be very helpful, but I'm not finding anything through Google.
Generation of getters and setters is a common feature of IDEs. You can use the free NetBeans IDE, while some might prefer to dish out some money for PhpStorm. Both IDEs can generate the getters and setters for you.
So it appears that it's not possible to run the generate:* commands without an instance of the entity manager, not even just to generate the getters/setters. Unfortunately that means a connection is required, which is what I'm trying to avoid for this stage of the development.
I've been looking through some of the more popular Symfony bundles out there, trying to figure out how they handle it. From what I can gather the pattern is to define a base, non vendor-specific model in the Model/ namespace using standard DocBlocks for the properties (which any ol' generator can be used to parse and generate the getters/setters for,) then to extend the class in a Doctrine specific entity within the Entity/ namespace.
As Entity\Foo extends Model\Foo, the type hinting would still be valid, and you only need overwrite the property annotations and any methods requiring Doctrine specific code.
Although it's a little more work initially (ignoring the generation process,) I actually like this approach. Not only does it keep the repetitive getter/setter boilerplate code separate from the Doctrine mappings, but it actually follows best practises by de-coupling the models from Doctrine altogether.

How to remove an entity?

This is my first time using symfony framework. I am trying to learn Doctrine. I have created and entity class. I have created an entity called Product. But since there was something wrong I deleted the Entity folder and now I am trying to create one again. But the framework is not allowing me to do so. It saying that an entity class already exists. How do I remove my previous entity class?
You must to clear doctrine cache:
php app/console doctrine:cache:clear-metadata
php app/console doctrine:cache:clear-query
php app/console doctrine:cache:clear-result
Chen creating your Product entity, what type of mapping do you pick ? XML, YAML, Annotation ?
If you pick XML or YAML, then you got an external file that define your mapping, maybe it's this file that is blocking you.
Take a look to src/Path/To/Your/Bundle/Resources/config/doctrine.

Standard idiom for adding new models to an app built on Symfony + Doctrine

What is the a standard way of adding new model to my app built on Symfony + Doctrine while maintaining all previous models and their meta-data (like relationships).
What am I really looking for: A command / procedure that will be equivalent of ./script/generate model FooModel in Ruby on Rails (which does not have any sort of reset db / reset models while generating)
If these two are different things, and I am chasing the wrong ghost (I would like to think I am not), please let me know.
EDIT: Updated the question.
You shouldn't be overriding the base classes, as these will be mostly be auto-generated whenever you do build:all or doctrine:build-model etc. Use the classes generated in the lib/model directory eg YourModel.class.php if you want to add new methods etc. Then your new models will be generated alongside your existing ones.
Standard process is to add the new model and any relationships it requires to schema.yml
Then do ./symfony doctrine:build-all (or :build --all for symfony 1.3/1.4)
As richsage says, you shouldn't be editing the base classes, so this operation is totally non destructive.
Doctrine also has functionality for migrations so that you an update the database schema easily as you deploy the new code into production:
http://www.doctrine-project.org/documentation/cookbook/1_0/en/symfony-and-doctrine-migrations
Newer versions of doctrine (1.1 +, symfony 1.3+) include the generate-migrations-diff task, which can create migrations for you. This is covered very well here:
Extra changeColumns in Doctrine generate-migrations-diff
[edit: the author of the question above has copy/pasted it below as well now]
The generate-migrations-diff doesn't diff two different yaml files. It actually compares your models and your yaml file and then generates a migration based on the differences. If you start from a db that is in sync with your yaml and classes, your workflow to make schema changes should be:
Change yaml file
Run generate-migrations-diff to diff your current (changed) yaml with your (unchanged) models. This will generate a migrations file in your doctrine/migrations directory (or whatever migrations_path is set to in your doctrine config).
Run migrate to run the migration created in step 2 and modify your database
Run generate-models-yaml to generate new classes based on your yaml file. These go where you've specified your generated models go (models_path in your doctrine config).
Run generate-sql to generate a SQL file. This will go where your doctrine sql_path config is set to.

Categories