Skip to content

Entity: default values

vojtech-dobes edited this page Jan 10, 2012 · 2 revisions

There are two ways how to set default value of entity property:

  1. Annotation (only scalar values)
/**
 * @property $foo {default abc}
 */
  1. Method getDefault<Property>
protected function getDefaultFoo()
{
    return array();
}

Annotations override the method.

The default value can be anything accepted by setter. For example in case of DateTime, it can be string date:

/**
 * @property DateTime $registered {default now}
 */

Annotation can handle scalar values or constants:

/**
 * @property $foo {default abc} string
 * @property $foo {default "abc"} quoted string
 * @property $foo {default 123.52} number
 * @property $foo {default TRUE} constant
 * @property $foo {default self::FOO} class constant
 */

It's always possible to restore the default value:

$e->neco = Orm\IEntity::DEFAULT_VALUE;

When the default value is not set, it is equal to NULL.

Clone this wiki locally