E
E
Evgeny Romashkan2018-06-27 19:05:00
symfony
Evgeny Romashkan, 2018-06-27 19:05:00

Set explicitly all default values ​​for fields via php each time an object is created?

Started learning Symfony and Doctrine. I was interested in how to set default values ​​for fields in the database.
Stumbled upon this discussion: https://stackoverflow.com/questions/3376881/defaul... .
The bottom line is that Doctrine has stopped supporting this and you need to do this:

<?php
/**
 * @Entity
 */
class myEntity {
    /**
     * @var string
     *
     * @Column(name="myColumn", type="string", length="50")
     */
    private $myColumn = 'myDefaultValue';
    ...
}

Did I understand correctly that now, every time a new object is created, in an INSERT query to the database, the default fields will be explicitly written every time? Is this normal practice?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
voronkovich, 2018-06-27
@EvgeniiR

I would use both ways at the same time:

  • It is necessary to specify a default value in the entity, because an entity must always be valid ;
  • Specifying a default value for the column is also necessary, because there are times when you have to work with "naked" SQL .
<?php

namespace App\Entity;

/**
 * @Entity
 */
class User
{
    const DEFAULT_ROLE = 'user';

    /**
     * @Column(type="string", options={"default": User::DEFAULT_ROLE})
     *
     * @var string
     */
    private $role = self::DEFAULT_ROLE;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question