Symfony 2.7 - Doctrine migration with DateTimes stuck in permanent loop - symfony

I have scoured the internet, books and forums looking for an answer to this, and am hoping someone on here can help.
I have a standard Symfony 2 project setup, using entity annotations in doctrine. The problem I have is that any of my entities that contain a datetime type constantly want to migrate. So after initial migration to the db i can re run docrine:schema:update --dump-sql and I still see this:
ALTER TABLE log CHANGE date date DATETIME NOT NULL;
ALTER TABLE message CHANGE created_on created_on DATETIME NOT NULL, CHANGE updated_on updated_on DATETIME NOT NULL;
ALTER TABLE module CHANGE start_date start_date DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL, CHANGE end_date end_date DATETIME NOT NULL;
ALTER TABLE scheduled_commands CHANGE last_execution last_execution DATETIME NOT NULL;
I could sit here all day running migrations and checking this and it will stay the same. The entities have nothing special in them either:
/**
* #var \DateTime
*
* #ORM\Column(name="start_date", type="datetime")
*/
private $startDate;
/**
* #var \DateTime
*
* #ORM\Column(name="end_date", type="datetime")
*/
private $endDate;
Does anyone have any ideas on this? I am now completely stumped :( .
The mysql setup is current 5.6.25 too
the columns structure look like this once migrated
`start_date` | DATETIME | NOT NULL,
`end_date` | DATETIME | NOT NULL

You need to ensure that types.datetime is correctly set up in doctrine.yaml

Related

Doctrine is generating "DEFAULT NULL" columns, even nullable=false is specified

We migrated the configuration from yml to annotations. In the best case if we do doctrine:schema:update --dump-sql there are no modifications. But now doctrine generates SQL "DEFAULT NULL" on all existing not nullable columns.
We tried to set explicitly nullable=false. And also we tried to move this column property from embeddable to main entity. Nothing works properly.
Table already existing. If we have this annotation
/**
* #ORM\Column(name="first_name", type="string", length="255", nullable=false)
*/
protected $firstName;
doctrine generates:
ALTER TABLE user CHANGE first_name first_name VARCHAR(255) DEFAULT NULL;

Symfony entities are not updated in database after changes

My symfony app is using Doctrine to persist entities in mysql.
Today I updated my entities "Advertiser" and "Report" so there are relations between the two - as suggested in this post: When using EntityType to show a select, can't save entity from Symfony form
When I try creating a migration, it says that the database is already in sync.
php bin/console make:migration
Returns:
[WARNING] No database changes were detected.
The database schema and the application mapping information are already in sync.
However if I look at the table for the report, I see it still has the old schema:
+---------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| advertiser_id | int(11) | NO | MUL | NULL | |
| start_date | date | NO | | NULL | |
| end_date | date | NO | | NULL | |
| deleted | tinyint(1) | NO | | NULL | |
+---------------+------------+------+-----+---------+----------------+
Even though my entity looks like this now:
class Report
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="date")
*/
private $start_date;
/**
* #ORM\Column(type="date")
*/
private $end_date;
/**
* #ORM\Column(type="boolean")
*/
private $deleted;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Advertiser", inversedBy="reports")
* #ORM\JoinColumn(nullable=false)
*/
private $advertiser;
public function getId(): ?int
{
return $this->id;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->start_date;
}
public function setStartDate(\DateTimeInterface $start_date): self
{
$this->start_date = $start_date;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->end_date;
}
public function setEndDate(\DateTimeInterface $end_date): self
{
$this->end_date = $end_date;
return $this;
}
public function getDeleted(): ?bool
{
return $this->deleted;
}
public function setDeleted(bool $deleted): self
{
$this->deleted = $deleted;
return $this;
}
public function getAdvertiser(): ?Advertiser
{
return $this->advertiser;
}
public function setAdvertiser(?Advertiser $advertiser): self
{
$this->advertiser = $advertiser;
return $this;
}
}
I've been searching for solutions and have tried these, but no luck:
php bin/console doctrine:cache:clear-metadata
php bin/console doctrine:schema:update --force
Please let me know if you have any suggestion on how I can update my database with my updated schema.
I had the same issue, try clearing the cache with:
symfony console cache:clear
As a many to one relation, it's normal that you're database advertiser column only stores the key of the report as a "link" to it, so that's why Symfony doesn't see any changes in your DB.
Maybe you can also use :
php bin/console doctrine:schema:update --dump-sql
to see changes in your DB
php bin/console doctrine:schema:update --force
to apply changes without using migrations
Maybe it's useful for someone, but when using annotations make sure that the comment block follows the DocBlock format, the first line should have two asterisks: /** and not a single asterisk /*
DocBlock
/**
*
*/
PHP Multiline comment
/*
*
*
*/
Try using proper annotation setup may be missing required configuration, you can always try to validate your schema with bin/console doctrine:schema:validate:
/**
* #ManyToOne(targetEntity="App\Entity\Advertiser", inversedBy="reports")
* #JoinColumn(name="advertiser_id", referencedColumnName="id")
*/
private $advertiser;
And check Advertiser entity for issues as well, maybe it is missing primary key or something.
I believe that the problem was that I previously had a property called "advertiser_id" (int) on the report object. And possibly I was trying to change too many things at once for doctrine to manage. Previously, I had tried to remove the advertiser_id while adding the relation property for advertiser.
To get doctrine working again, I removed the advertiser property from the Report object - along with the getters and setters. I also removed the reverse lookup stuff from the Advertiser object. When I tried to run the migration, it seems like there were several migrations that it was trying to run - all doing the same thing: dropping a foreign key that doesn't exist. So I commented out all of those commands in the migration files and finally was able to get it to migrate. I also removed the advertiser_id property. The app is working again.
Then I tried adding the "advertiser" relation property back to the report. This time it worked as expected and I was able to migrate. So I think the issue is related to my object already having an advertiser_id property. Now that I've added the advertiser relation to the Report object, I see that doctrine added an advertiser_id column to the table. I suspect that it being present previously was the reason things broke down.
Thanks for the replies! Glad to have it working again.

Doctrine is trying to set default values even when none is defined

I am currently migrating from a legacy project to a Symfony4.
So I still need to keep the schema of the database.
I imported the database, and after fixing tons of issues I'm still unable to solve this one :
I have a join column where Doctrine is trying to set default value
$this->addSql('ALTER TABLE temperature_recording_system_sensor ALTER temperature_recording_system_site_uuid SET DEFAULT \'uuid_generate_v4()\'');
My column definition is actually like this :
/**
* #var TemperatureRecordingSystemSite
*
* #ORM\ManyToOne(targetEntity="TemperatureRecordingSystemSite")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="temperature_recording_system_site_uuid", referencedColumnName="temperature_recording_system_site_uuid", nullable=true, columnDefinition="DEFAULT NULL")
* })
*/
private $temperatureRecordingSystemSiteUuid;
How should I tell Doctrine to not set default values, as I can't use the option field on join column ?
I'm on Postgres 9.6.10 also.
Ia not trying to set a default value, plus it's on a JoinColumn, not a Column.

symfony2 doctrine decimal precision scale annotations are ignored

i've set up an attribute in my entity like this :
/**
* #var decimal
*
* #ORM\Column(name="latitude", type="decimal", precision=10, scale=7, nullable=true)
*/
private $latitude;
but when i generate the database schema with : doctrine:database:create; doctrine:schema:create
my field is set up to decimal (10,0) in the database (when i look up with phpmyadmin)
and so, when in insert data like 42.123456 with a form, this data is truncated to 42.
how can i resolve this?
Thanks.
Ok, finally get to resolve this.
Simply manually remove the cache (app/cache/..)
removing it by symfony command cache:clear wont revolve the problem

doctrine2 date/datetime as ID

is it possible to set an attribute as an id when the column is date or datetime?`
Here my Attribute in the entity:
/**
* #var date $statisticdate
*
* #ORM\Column(name="statisticdate", type="date", nullable=false, unique=true)
* #Id
* #Assert\DateTime()
*/
private $statisticdate;
When i'm persisting a new object, i have an error that the object Datetime cannot be converted to string.
Now i created a normal id attribut and everything works.
Thank you very much.
Asfar as I know, it is only allowed in a composite key, and even that is has some known issues. I think the easy solution can be to make it a string and let the setStaticdate($datetime) check wheather it is a valid date or not.

Resources