is there a simple way to persist an entity with all values = 0? My entity has an extreme amount of rows ... typing ->setBlabla(0) for that amount of rows..puh..
Is there an "easier" way?
Regards
is there a simple way to persist an entity with all values = 0? My entity has an extreme amount of rows ... typing ->setBlabla(0) for that amount of rows..puh..
Is there an "easier" way?
Regards
The doctrine docs suggest to define default values via entity properties:
class User
{
const STATUS_DISABLED = 0;
const STATUS_ENABLED = 1;
private $status = self:STATUS_DISABLED;
}
I would prefer to set all properties default values in entity class constructor:
class User
{
const STATUS_DISABLED = 0;
const STATUS_ENABLED = 1;
private $status;
public function __construct()
{
$this->status = self::STATUS_DISABLED;
}
}
You can set a default value for each field and use nullable=false
to force it insert the default value; like this
@ORM\Column(name="column_name", type="integer", nullable=false, options={"default" = 0})
Or you can create a service prePresist
to set the "0" value to empty fields before persist be fired. Take at look at
Doctirn2 Lifecycle Events
Symfony2 Event Listener