2

If you want to know how to set default value in symfony2, look here.

I can set it through both ways. For example, I can set it through the variable directly like this.

/**
 * @var string $variable
 *
 * @ORM\Column(name="variable", type="string", nullable=true)
 */
private $variable = "default_value";

or i can use the options attribute

/**
 * @var string $variable
 *
 * @ORM\Column(name="variable", type="string", nullable=true,options={"default" = "default_value"})
 */
private $variable = "default_value";

I want to know what is the difference b/w each of these two. More importantly what are the cases when the first way won't suffice , and options attribute has to be used.

From what i've come to know so far, setting the variable directly sets default value on a symfony2 level , and the options attribute sets it for doctrine. What difference does it make to set default value on ORM level, and when does it clash with symfony2 defaults? What happens if i only use one of the two.

Community
  • 1
  • 1
Shrey
  • 85
  • 1
  • 9
  • 4
    This question has already been answered here http://stackoverflow.com/questions/3376881/default-value-in-doctrine – Daniel Stefaniuk Apr 07 '16 at 08:44
  • I want to know the difference b/w these two approaches. I don't think there is an answer there that explains the difference and the case where one of the two wont suffice. If there is , please be kind enough to point out. – Shrey Apr 13 '16 at 08:18

1 Answers1

0

You're right, the difference is that the value is being set on different levels. To elaborate, if you create a default value using:

/**
 * @var string $variable
 *
 * @ORM\Column(name="variable", type="string", nullable=true)
 */
private $variable = "default_value";

When do you create a new object with the default value.

$a = new MyClass();
$a->getVariable(); // -> 'default_value'

So the actual default value is accessible immediately. If you use the second approach:

/**
 * @var string $variable
 *
 * @ORM\Column(name="variable", type="string", nullable=true,options={"default" = "default_value"})
 */
private $variable;

This will cause that the schema in DB will contain default value for the column so the value will be accessible after you save the entity

$a = new MyClass();
$a->getVariable(); // -> null
$em->perist($a);
$em->flush();
$a->getVariable(); // -> 'default_value'

I would say this is the basic difference. It really depends of where you want to control your default values and when you want to have them.

In my opinion, in most of the cases the assignment in the entity itself is better, since if you want to change the value later in the system, all you need to do is update the value on the entity.

If you will be using the default in the database, then updating to a different value will need to update the annotation and then create a migration to alter the default value in the DB field

UPDATE (Migration question):

In case you use default value specified in Doctrine annotation and you're using Doctrine migrations bundle for the Symfony project and you will write your migration for this class manually (not by using php app/console doctrine:migrations:diff), you need to specify manually the default as an option. The annotation itself just tells how the column definition should look like.

If the default value is set only in PHP, i.e. private $variable = 'default_value'; migration bundle will not be affected by it at all. That means that if you run php app/console doctrine:migrations:diff the DEFAULT value for the field won't be filled in. Of course if you write the migration yourself, the default value in the database will be there only if you fill it automatically in.

To sum the migration budle functionality - the automatically generated migrations are affected only by default value in @ORM\Column annotation.

Shrey
  • 85
  • 1
  • 9
Ondrej Führer
  • 449
  • 5
  • 9
  • Thanks. just what i needed. Just one more thing. If i use **ONLY symfony level defaults**, will it affect the entity creation migrations? For example, if i don't specify default value in migration, will it be able to pick up symfony level defaults, or do i need to set options attribute for that? – Shrey Apr 13 '16 at 10:50
  • I updated the answer. I assume by **ONLY symfony level defaults** you mean default for Doctrine @Column annotation.. As explained in the update, if you wirte your migrations manually, then you need of course specify the column by yourself. If you use the `php app/console doctrine:migrations:diff` command to automaticaly generate migrations, it will take care of it. But anyway I really suggest to use PHP-level defaults as I mentioned above. – Ondrej Führer Apr 13 '16 at 14:45
  • No, by symfony level defaults, i mean private $variable="false"; – Shrey Apr 14 '16 at 06:25
  • Ah sorry, in that case it won't affect the DB schema at all and there is not need to do it since you handle the defaults on PHP side – Ondrej Führer Apr 14 '16 at 11:16
  • Updated, short answer to your questions - 1) no 2) no . PHP-level defined default will not affect the migraions bundle at all => no default value in DB column. – Ondrej Führer Apr 15 '16 at 07:48