Home > OS >  How to rename Entity and attribute in symfony
How to rename Entity and attribute in symfony

Time:08-12

I just want to rename "EntityName" to something else the most easily possible, with the database entry. Also how can I rename an Attribute?

So, I have an Entity called "EntityName" (for exemple) in /appname/src/Entity/EntityName.php like this:

<?php
namespace App\Entity;
use App\Repository\EntityNameRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=EntityNameRepository::class)
*/
class EntityName
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
private $id;
[...]

When I try to rename the name of the class and try php bin/console doctrine:schema:update --dump-sql or --force I get this error:

In EntityName.php line 11: Compile Error: Cannot declare class App\Entity\NewName, because the name is already in use

(I think I cannot rename beaucause it check itself if it already exist.)

CodePudding user response:

I'd advise caution, specially if it's a production environment.

doctrine:schema:update --force (or doctrine:migrations:diff or make:migration, for that matter) doesn't detect changes so it will drop your existing table (along with all your data) and create a new one.

The safest way to preserve data would be to hand-create a migration with the ALTER TABLE ... RENAME. You can also use the @Table(name="old_table_name") annotation to preserve the original name, although this might be a bit confusing when looking at the database alone.

Depending on your IDE you might need more or less elbow grease. Chances are that the following will be handled correctly:

  • Both the entity and repository class name and file name, making them match (even in the caseName).
  • The entity class name in the repository constructor.
  • The repository class name in the repositoryClass annotation of the entity.
  • targetEntity annotations in relations.

There will be the property names themselves in the related classes (and JoinTableand mappedBy / inversedBy if applicable) left to change, if you wish to keep things consistent. If so, you would have to add the needed statements to the migration as well, and you might need to disable the constraints in the process. --dump-sql might be helpful on hinting what tables need renaming, but you won't be able to use the sql straight up.

The easiest way, if you don't care about your data, would be doctrine:schema:update --force indeed.

CodePudding user response:

If you want to rename the entity means you have to change both Entity and Repository Class name and file name too. Then have to change Both classes import statements properly. After that just run this comment bin/console c:c.

CodePudding user response:

If you want to rename an entity, you only need to change its class name AND file name to make it match. Also make sure the namespace match file path.

If all these things are right, then make sure you don't have another file which would contain the same namespace and class name you are trying to use for your "NewName" class

If you still get the error message, then you might want to clear you cache by running php bin/console c:c

Lastly, if it still fails, you might want to dump the autoload by running composer dump-autoload (or if you are using Phpstorm: tools > composer > dump autoloader)

  • Related