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

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;

Related

Doctrine 2 - ManyToOne Nullable relation not work

I have a strange error, on my Symfony4 project.
One of my relation is a ManyToOne with nullable true, like this:
class UserComic
{
...
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Series")
* #ORM\JoinColumn(name="id_series", referencedColumnName="id", nullable=true)
*/
private $series;
...
}
then I try to create a new UserComic with the $series attribute set to NULL, but i receive this error:
"Entity of type App\Entity\UserComic is missing an assigned ID for
field 'series'. The identifier generation strategy for this entity
requires the ID field to be populated before EntityManager#persist()
is called. If you want automatically generated identifiers instead you
need to adjust the metadata mapping accordingly."
As the field is not setted to nullable. Any advice?
ID means UNIQ and NOT NULLABLE.
You are on a ManyToOne relatsionship (on the many side) that break the UNIQ property of ID
You set the joinColumn as nullable, that breaks the NOT NULLABLE PROPERTY

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.

Doctrine2 what is the best way to update entities? ( Database => Doctrine )

Let's say I have all my entities set and I have my repositories linked in annotations like this:
/**
* User
* #ORM\Entity(repositoryClass="App\Repository\UserRepository")
* #ORM\Table(name="user")
*/
class User
{....}
now I go to my sql database and add a new column or any other change. I need to update doctrine model and at the moment I do it like this :
1 ) php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity
2) php bin/console make:entity --regenerate App
these commands are purging my entities annotations and I get:
/**
* User
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User
{....}
3) I have to relink all my repositories manually in each entities (I have 12)
QUESTION:
Is there a way to update from database to entity while keeping all repository links at the top of my entities? How do you do it ?
I could not find anything in the doctrine manual; I was hoping for a doctrine:mapping:import setting ?
Instead of going from database to entity, go by another way: from entity to database
add needed property to the entity. like
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
protected $name;
run php bin/console doctrine:migrations:diff you will get migration with changes like $this->addSql('ALTER TABLE user ADD name VARCHAR(255) DEFAULT NULL');
run migration
EDIT
If you still want to go by your way, at least you could narrow down changes only to one entity by adding --filter="user" to both your commands

Doctrine: set foreign relation

This is regarding a problem with Doctrine when I try to insert a record into a associative entity. Below is a simplified description of the problem.
I have two tables, let's call them One and Two. Table One has a foreign key to table Two, called twoId with a column two_id. Field two_id happens to be part of the primary key.
* #ORM\Id
* #ORM\Column(name="user_id", type="string", length=40)
*/
private $twoId;
/**
* #ManyToOne(targetEntity="[...]", inversedBy="[...]", fetch="EAGER")
* #JoinColumn(name="two_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $two;
I am trying to insert a new record into table A. This works:
$two = [.. read from DB ..];
$one = new One();
$one->setTwo($two);
$one->setTwoId($two->getId());
$em->persist($one);
$em->flush();
I don't like to call both setTwo and setTwoId. Furthermore, I don't like reading the $two record before referencing it.
If I skip setTwoId call, I get the error: Entity of type [..] is missing an assigned ID for field 'twoId'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called.
If I skip setTwo call, I get the error: Integrity constraint violation: 1048 Column 'two_id' cannot be null
My problems are:
How can I avoid calling both setTwo() and setTwoId()?
What if I want to reference a entity from Two without reading it? Should I use $em->getReference()? (PhpStorm doesn't even recognize it)
In case someone makes the same mistake:
As pointed out by #lordrhodos, declaring the field $twoId was wrong because Doctrine will create it automatically without having a definition.
Definition:
/**
* #ManyToOne(targetEntity="[...]", inversedBy="[...]", fetch="EAGER")
* #JoinColumn(name="two_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $two;
Usage:
$two = [.. read from DB ..];
$one = new One();
$one->setTwo($two);
$em->persist($one);
$em->flush();

Doctrine ORM Custom Column Collation

I'm trying to set custom column collation as in Doctrine documentation:
http://doctrine-dbal.readthedocs.org/en/latest/reference/schema-representation.html and
http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html
using
#ORM\Column(name="body", type="string", length=140, options={"customSchemaOptions"={"collate"="utf8mb4_unicode_ci"}})
but when I update the schema it always goes back to utf8_unicode_ci (when I set it manually for example). Any ideas?
This has been added by now if you (or someone else) still need, see column annotation docs
Example: In annotations:
/**
* #var string
*
* #ORM\Column(type="string", length=64, nullable=false, options={"collation":"utf8_bin"})
*/
private $code;
In Yaml:
Your\Nice\Entity:
fields:
code:
type: string
length: 64
options:
collation: utf8_bin # Although the recommendation is the utf8mb4* set now.
This is supported by all common database drivers now.

Resources