I\'ve been playing around with Symfony on my web server and I\'ve been creating entity with doctrine for my database. I wanted to add a column to one of these entity... I wa
here you have to follow two steps to make changes in your entity Step1: Open Your Entity File..For EX: "Acme\MyBundle\Entity\Book"
Current Entity is having few fields like:id,name,title etc. Now if you want to add new field of "image" and to make change constraint of "title" field then add field of "image" with getter and setter
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=500)
*/
private $image;
And add getter and setter
/**
* Set image
*
* @param string $image
*
* @return Book
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getimage()
{
return $this->image;
}
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=500)
*/
private $title;
Ok...You have made changes according to your need,And You are one step away.
Step 2: Fire this command in project directory
php bin/console doctrine:schema:update --force
Now,check your table in database ,It's Done!!
There is a pitfall on step php app/console doctrine:migrations:diff
described above when using doctrine migrations.
You made changes in entity, all seems valid, but the command on creating migrations php app/console doctrine:migrations:diff
says "No changes detected in your mapping information."
And you can't find where's the old database structure placed, search in files found nothing.
For me, the key was to clear Redis cache.
php app/console redis:flushdb
It happens because of config.yml
snc_redis:
clients:
default:
type: predis
alias: default
dsn: redis://localhost
doctrine:
query_cache:
client: default
entity_manager: default
namespace: "%kernel.root_dir%"
metadata_cache:
client: default
entity_manager: default
document_manager: default
namespace: "%kernel.root_dir%"
result_cache:
client: default
namespace: "%kernel.root_dir%"
entity_manager: [default, read] # you may specify multiple entity_managers
After that, migrations:diff reread all of entities (instead of taking outdated ones metadata from cache) and created the right migration.
So, the full chain of steps for modifying entities is:
php app/console redis:flushdb
)php app\console doctrine:migrations:diff
)php app\console doctrine:migrations:migrate
)Of course, your doctrine metadata cache may be not Redis but something else, like files in app/cache or anything other. Don't forget to think about cache clearing.
May be it helps for someone.
f you need to add a new field property to an existin entity you can use make:entity
$ php bin/console make:entity
Class name of the entity to create or update
"existingentity"
New property name (press to stop adding fields):
description
Field type (enter ? to see all types) [string]:
text
Can this field be null in the database (nullable) (yes/no) [no]:
no
New property name (press to stop adding fields):
(press enter again to finish)
information extracted from https://symfony.com/doc/current/doctrine.html
I think, you need to update your relation table manually to map the relations. I found this:discussion
Again, We can generate entities from existing database, as a whole, but separately? :(
I found a way in which I added the new column inside YourBundle/Resources/Config/doctrine/Yourentity.orm.yml.
Then added getter and setter methods inside Entity class.
Then did php app/console doctrine:schema:update --force
from console. It worked for me.
Actually, using Doctrine does not make sense at all to do something like you suggested.
Doctrine is a ORM (Object-Relational Mapping) tool. It means that you want to abstract the database from your PHP code, you delegate database stuff to Doctrine. Doctrine does a wonderful job on that area.
As you want to keep your customers/peers updated with the latest version of the model, you should use the Doctrine Migrations ( http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html ). That's the way to manage database updates. Moreover, it gives you complete control on what to do when upgrading/downgrading the database. You could, e.g., set default values before you add the FK.
The steps for adding a new property on the class should be:
For Symfony 2:
modify the class by:
Acme\MyBundle\Entity\Customer and add the property you want;
Acme\MyBundle\Entity\Customer
or modify the doctrine file:
Acme/MyBundle/Resources/config/doctrine/customer.yml
run the console command (it will add the proper set/get in the class)
php app/console doctrine:generate:entities AcmeMyBundle:Customer
run the console command
php app/console doctrine:migrations:diff
run the console command (it will place a new file on app/DoctrineMigrations)
php app/console doctrine:migrations:migrate
When you're deploying the new version of the code, all you got to do is update the source code and run the command above.
For Symfony 3:
modify the class by:
Acme\MyBundle\Entity\Customer and add the property you want;
Acme\MyBundle\Entity\Customer
or modify the doctrine file:
Acme/MyBundle/Resources/config/doctrine/customer.yml
run the console command (it will add the proper set/get in the class)
php bin/console doctrine:generate:entities AcmeMyBundle:Customer
run the console command (update database)
php bin/console doctrine:schema:update --force