This commit is contained in:
parent
eaf2fdcdce
commit
02dab53c96
@ -79,13 +79,7 @@ Doctrine check constraints act as database level constraints as well as applicat
|
||||
|
||||
Doctrine provides the following simple check operators:
|
||||
|
||||
||~ Operator ||~ Description ||
|
||||
|| {{gt}} || greater than constraint ( > ) ||
|
||||
|| {{lt}} || less than constraint ( < ) ||
|
||||
|| {{gte}} || greater than or equal to constraint ( >= ) ||
|
||||
|| {{lte}} || less than or equal to constraint ( <= ) ||
|
||||
|
||||
Consider the following example:
|
||||
Consider the following example which uses 'min' validator:
|
||||
|
||||
<code type="php">
|
||||
class Product extends Doctrine_Record
|
||||
@ -93,7 +87,7 @@ class Product extends Doctrine_Record
|
||||
public function setTableDefinition()
|
||||
{
|
||||
$this->hasColumn('id', 'integer', 4, 'primary');
|
||||
$this->hasColumn('price', 'decimal', 18, array('gt' => 0);
|
||||
$this->hasColumn('price', 'decimal', 18, array('min' => 0);
|
||||
}
|
||||
}
|
||||
</code>
|
||||
@ -103,12 +97,66 @@ When exported the given class definition would execute the following statement (
|
||||
<code type="sql">
|
||||
CREATE TABLE product (
|
||||
id INTEGER,
|
||||
price NUMERIC CHECK (price > 0)
|
||||
PRIMARY KEY(id))
|
||||
price NUMERIC,
|
||||
PRIMARY KEY(id),
|
||||
CHECK (price > 0))
|
||||
</code>
|
||||
|
||||
So Doctrine optionally ensures even at the database level that the price of any product cannot be below zero.
|
||||
|
||||
You can also set the maximum value of a column by using the 'max' validator. This also creates the equivalent CHECK constraint.
|
||||
|
||||
<code type="php">
|
||||
class Product extends Doctrine_Record
|
||||
{
|
||||
public function setTableDefinition()
|
||||
{
|
||||
$this->hasColumn('id', 'integer', 4, 'primary');
|
||||
$this->hasColumn('price', 'decimal', 18, array('min' => 0, 'max' => 1000000);
|
||||
}
|
||||
}
|
||||
</code>
|
||||
|
||||
Generates (in pgsql):
|
||||
|
||||
<code type="sql">
|
||||
CREATE TABLE product (
|
||||
id INTEGER,
|
||||
price NUMERIC,
|
||||
PRIMARY KEY(id),
|
||||
CHECK (price > 0),
|
||||
CHECK (price < 1000000))
|
||||
</code>
|
||||
|
||||
Lastly you can create any kind of CHECK constraints by using the check() method of the Doctrine_Record. In the last example we add constraint to ensure that price is always higher than the discounted price.
|
||||
|
||||
<code type="php">
|
||||
class Product extends Doctrine_Record
|
||||
{
|
||||
public function setTableDefinition()
|
||||
{
|
||||
$this->hasColumn('id', 'integer', 4, 'primary');
|
||||
$this->hasColumn('price', 'decimal', 18, array('min' => 0, 'max' => 1000000);
|
||||
$this->hasColumn('discounted_price', 'decimal', 18, array('min' => 0, 'max' => 1000000);
|
||||
|
||||
$this->check('price > discounted_price');
|
||||
}
|
||||
}
|
||||
</code>
|
||||
|
||||
Generates (in pgsql):
|
||||
|
||||
<code type="sql">
|
||||
CREATE TABLE product (
|
||||
id INTEGER,
|
||||
price NUMERIC,
|
||||
PRIMARY KEY(id),
|
||||
CHECK (price > 0),
|
||||
CHECK (price < 1000000),
|
||||
CHECK (price > discounted_price))
|
||||
</code>
|
||||
|
||||
|
||||
> 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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user