Symfony doctrine:schema:update not working

2020-08-14 10:27发布

问题:

I have a strange problem : I have an application symfony 2.3 (with sonata user) I created a bundle with one entity - the entity was created without a problem then I had to modify the entity and now it seems to be impossible to modify the schema :

To see what happens I increased all the string lengths with +1

The entity code (with annotations) :

namespace Too\ConfigAppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * ConfigApp
 *
 * @ORM\Table(name="ConfigApp")
 * @ORM\Entity(repositoryClass="Too\ConfigAppBundle\Entity\ActiviteRepository")
 */
class ConfigApp
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $nom
     *
     * @ORM\Column(name="nom", type="string", length=101, unique=true)
     */
    private $nom;

    /**
     * @var string $nomSlug
     *
     * @Gedmo\Slug(fields={"nom"}, updatable=true, separator="_")
     * @ORM\Column(name="nomSlug", type="string", length=101, nullable=true)
     */
    private $nomSlug;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=151)
     */
    private $email;

    /**
     * @var string $telephone
     *
     * @ORM\Column(name="telephone", type="string", length=16)
     */
    private $telephone;

    /**
     * @var datetime $cree_le
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="cree_le", type="datetime")
     */
    private $cree_le;

    /**
     * @var datetime $modifie_le
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modifie_le", type="datetime")
     */
    private $modifie_le;

    ...

Now see the result of :

php app/console doctrine:schema:update --dump-sql

CREATE TABLE ConfigApp (id INT AUTO_INCREMENT NOT NULL, nom VARCHAR(100) NOT NULL, nomSlug VARCHAR(100) NOT NULL, email VARCHAR(150) NOT NULL, telephone VARCHAR(15) NOT NULL, cree_le DATETIME NOT NULL, modifie_le DATETIME NOT NULL, PRIMARYKEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB

None of the new length is taken in account : for example the field nom should have length=101, but the dump-sql gives nom VARCHAR(100) !

Could anyone try to figure out whats going wrong ? Thanks !

EDIT : I tried to clear the cache before with : * php app/console doctrine:cache:clear-metadata * php app/console cache:clear * by deleting all content in cache folders

I also tried --dump-sql and --force.

This changes nothing at all. Please any hint would be welcome !

回答1:

I just ended up on the exact same issue: schema doesn't update.

Note that --force returns exactly the same thing as --dump-sql, the only difference is that --force runs the SQL against the database.

Although, in my case, the issue wasn't because of the .orm.xml file. It was because I've set this in the config_dev.xml:

doctrine:
orm:
    metadata_cache_driver:
        type: memcached
        host: localhost
        port: 11211
        instance_class: Memcached
    query_cache_driver:
        type: memcached
        host: localhost
        port: 11211
        instance_class: Memcached
    result_cache_driver:
        type: memcached
        host: localhost
        port: 11211
        instance_class: Memcached

Even when I issue an often salvatory:

php app/console cache:clear

the memcached data isn't flushed. So I had to restart memcached, then everything was up and running again!

So thanks for your question, it led me to the right spot in my case.

UPDATE: as Phil suggested above, running this command does the trick too:

php app/console doctrine:cache:clear-metadata


回答2:

It is possible that you forget to enable Doctrine auto mapping;

orm:
   #auto_mapping: true

If auto mapping is disabled (or commented like above) , you should register Entities of each bundle manually.

orm:
   entity_managers:
      default:
         mappings:
            AcmeHelloBundle: ~


回答3:

Try to use YAML instead of the default annotation when you run

php app/console doctrine:generate:entity

Or instead of running

app/console doctrine:schema:update --force

You can just manually create your MySql table which is a very tedious task



回答4:

I found the solution : I did not see before but there was a doctrine folder in src\Too\ConfigAppBundle\Resources\config containing a file called ConfigApp.orm.yml :

Too\ConfigAppBundle\Entity\ConfigApp:
    type: entity
    table: null
    repositoryClass: Too\ConfigAppBundle\Entity\ConfigAppRepository
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        nom:
            type: string
            length: '100'
        nomSlug:
            type: string
            length: '100'
        email:
            type: string
            length: '150'
        telephone:
            type: string
            length: '15'
        cree_le:
            type: datetime
            length: null
        modifie_le:
            type: datetime
            length: null
    lifecycleCallbacks: {  }

I deleted this folder and now updating the schema works again.

Surely I did something to generate this doctrine folder but I don't know what it was - if someone could tell me how this stuff is generated - and why ?



回答5:

i think its beacause of the doctrine:mapping:import command. This command stores the schema of an existing database into .orm.xml files. Probebly you execute this command.

I had the same issue, coast me a lot of time to find out.



回答6:

Because I was using .orm.yml-mapping I had the issue that I created the doctrine-folder, in which the yml mappings were present, under the wrong path, so I fixed it by moving the doctrine-folder to the the config folder: ...\BundleName\Resources\config\doctrine\MyEntity.orm.yml



回答7:

Though some of the answers given by @rai and others are correct, one more suggestion for Symfony version is equal to or above 3.0 please use bin/console instead app/console, as shown below,

bin/console doctrine:schema:update --force


回答8:

Type php app/console help doctrine:schema:update in CLI

 --dump-sql            Dumps the generated SQL statements to the screen (does no
t execute them).

...

 --force               Causes the generated SQL statements to be physically exec
uted against your database.

So try the --force instead of --dump-sql.

And here is the command for cache clearing :

php app/console cache:clear

Don't forget to use the help keyword before a command namespace in order to get the help message for that command.

Hope it helps



回答9:

Try

php app/console doctrine:schema:update --force

this is update your database schema with entity