How to remove an entity? - php

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.

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

Create a model from existing table (Symfony3)

I have an existing table with 5 columns, and I want to generate an Entity without that generates a new migration with new table!
Thanks!
So, as I mentioned in comment - you just need to follow steps in Symfony documentation.
This way, for Symfony 3 you need follow this:
php bin/console doctrine:mapping:import --force AppBundle xml
This command line tool asks Doctrine to introspect the database and generate the XML metadata files under the src/AppBundle/Resources/config/doctrine folder of your bundle.
Once the metadata files are generated, you can ask Doctrine to build related entity classes by executing the following command.
php bin/console doctrine:mapping:convert annotation ./src
It will generate Entity classes, and after that you must to remove the XML files, generated by first command.

Symfony Flex - Doctrine generate entity and entities runtime exception

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.

Doctrine/Symfony entity generator and generating entity from one table

I have already a few entities, but now a new table appeared in a database, and I'd like to generate an entity on only this one table.
I already saw this, but I have further questions.
I already have a User entity (and a db table). Now, the new table is called "Report" (no entity for it right now, I want to create it) and it has a foreign key to User. There are also a few more foreign keys.
If I do what is suggested in the above answer, that is:
$ php app/console doctrine:mapping:import --force AppBundle xml --filter="Report"
$ php app/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Report"
$ php app/console doctrine:generate:entities AppBundle:Report --no-backup
Will Doctrine generator try to modify my User entity? Or will just create a Report entity?
Btw. I understand(?), this it will not, because this is ManyToOne relation, but let's assume for a moment that this is ManyToMany for a moment.
I know I could simply try it, but the last time I executed the doctrine:mapping:import --force command I ended up with a crashing app and I spent many hours to solve this problem, until someone on Stackoverflow told me to remove the src\AppBundle/Resources/config/doctrine/ directory, which helped.
I didn't think about making a backup before running this command.
So, I'm a bit afraid of it... Now I created a backup, but I'm not sure if this will help in case of troubles.
It depends on the /Resources/config/doctrine directory. If you empty it out and do the commands you described, then only the Report entity will be modified. If you have all of the previous mappings in that directory it should modify the User entity as well.
The commands will automatically create backups of your entities for you, so you will have a new User.php and a backup in User.php~, which you should be careful of if you are using version control and like to git add src/ without thinking too hard about it. You don't want to add those files to source control.
Either way, backups are created, and you should be using version control on top of that, so you should be fine.

MySQL Table structure not updated in Doctrine ORM Entity

Its symfony 2 - doctrine issue.
I was trying to add some more fields to one of my tables in MySQL. After modification, i had run the command doctrine mapping import and doctrine generate entities commands, but the Entity_name.php file under Bundle/Entity/ folder not getting updated. Can anyone help me ?
Thanks and regards,
Tismon Varghese
Your message is brief, but my interpretation is that you are trying to ADD more fields to an existing table, by creating a NEW entity. This doesn't sound like the correct workflow. The entity ought to already exist, and to add fields to the table, will require adding new properties, correctly annotated, to the existing Entity.
What commands are you running, and what command line output are you seeing? The doctrine based SF2 commands offer reasonable information upon failure.
Try to run this command:
$ php app/console doctrine:schema:update --force

Categories