Commit 02dab53c authored by zYne's avatar zYne

--no commit message

--no commit message
parent eaf2fdcd
...@@ -79,13 +79,7 @@ Doctrine check constraints act as database level constraints as well as applicat ...@@ -79,13 +79,7 @@ Doctrine check constraints act as database level constraints as well as applicat
Doctrine provides the following simple check operators: Doctrine provides the following simple check operators:
||~ Operator ||~ Description || Consider the following example which uses 'min' validator:
|| {{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:
<code type="php"> <code type="php">
class Product extends Doctrine_Record class Product extends Doctrine_Record
...@@ -93,7 +87,7 @@ class Product extends Doctrine_Record ...@@ -93,7 +87,7 @@ class Product extends Doctrine_Record
public function setTableDefinition() public function setTableDefinition()
{ {
$this->hasColumn('id', 'integer', 4, 'primary'); $this->hasColumn('id', 'integer', 4, 'primary');
$this->hasColumn('price', 'decimal', 18, array('gt' => 0); $this->hasColumn('price', 'decimal', 18, array('min' => 0);
} }
} }
</code> </code>
...@@ -103,12 +97,66 @@ When exported the given class definition would execute the following statement ( ...@@ -103,12 +97,66 @@ When exported the given class definition would execute the following statement (
<code type="sql"> <code type="sql">
CREATE TABLE product ( CREATE TABLE product (
id INTEGER, id INTEGER,
price NUMERIC CHECK (price > 0) price NUMERIC,
PRIMARY KEY(id)) PRIMARY KEY(id),
CHECK (price > 0))
</code> </code>
So Doctrine optionally ensures even at the database level that the price of any product cannot be below zero. 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. > 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 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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment