Commit fad6255a authored by zYne's avatar zYne

Docs for CHECK constraints

parent a336d424
......@@ -8,4 +8,7 @@ $manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_TABLES ^
Doctrine::EXPORT_CONSTRAINTS);
// turn off exporting
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_NONE);
</code>
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 ( >= )
* '''lte'''
> less than or equal to constraint ( <= )
Consider the following example:
<code type='php'>
class Product extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('id', 'integer', 4, 'primary');
$this->hasColumn('price', 'numeric', 200, array('gt' => 0);
}
}
</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.
Data types are a way to limit the kind of data that can be stored in a table. For many applications, however, the constraint they provide is too coarse. For example, a column containing a product price should probably only accept positive values. But there is no standard data type that accepts only positive numbers. Another issue is that you might want to constrain column data with respect to other columns or rows. For example, in a table containing product information, there should be only one row for each product number.
[source: http://www.postgresql.org/docs/8.2/static/ddl-constraints.html#AEN2032]
Doctrine allows you to define *portable* constraints on columns and tables. Constraints give you as much control over the data in your tables as you wish. If a user attempts to store data in a column that would violate a constraint, an error is raised. This applies even if the value came from the default value definition.
Doctrine constraints act as database level constraints as well as application level validators. This means double security: the database doesn't allow wrong kind of values and neither does the application.
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