1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/manual/docs/Object relational mapping - Constraints and validators - Check.php

42 lines
1.5 KiB
PHP
Raw Normal View History

2007-04-14 01:49:11 +04:00
Doctrine check constraints act as database level constraints as well as application level validators. When a record with check validators is exported additional CHECK constraints are being added to CREATE TABLE statement.
Doctrine provides the following simple check operators:
* '''gt'''
> greater than constraint ( > )
* '''lt'''
> less than constraint ( < )
* '''gte'''
> greater than or equal to constraint ( >= )
2007-05-11 23:23:18 +04:00
* '''lte'''
2007-04-14 01:49:11 +04:00
> less than or equal to constraint ( <= )
Consider the following example:
<code type='php'>
class Product extends Doctrine_Record
{
public function setTableDefinition()
{
2007-05-11 23:23:18 +04:00
$this->hasColumn('id', 'integer', 4, 'primary');
$this->hasColumn('price', 'decimal', 18, array('gt' => 0);
2007-04-14 01:49:11 +04:00
}
}
</code>
When exported the given class definition would execute the following statement (in pgsql):
CREATE TABLE product (
id INTEGER,
price NUMERIC CHECK (price > 0)
PRIMARY KEY(id))
So Doctrine optionally ensures even at the database level that the price of any product cannot be below zero.
> NOTE: some databases don't support CHECK constraints. When this is the case Doctrine simple skips the creation of check constraints.
If the Doctrine validators are turned on the given definition would also ensure that when a record is being saved its price is always greater than zero.
If some of the prices of the saved products within a transaction is below zero, Doctrine throws Doctrine_Validator_Exception and automatically rolls back the transaction.