Advanced components - Validators - Validating transactions.php 1.38 KB
Newer Older
doctrine's avatar
doctrine committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
class User extends Doctrine_Record {
    public function setUp() {
        $this->ownsOne("Email","User.email_id");
    }
    public function setTableDefinition() {
        // no special validators used only types 
        // and lengths will be validated
        $this->hasColumn("name","string",15);
        $this->hasColumn("email_id","integer");
        $this->hasColumn("created","integer",11);
    }
}
class Email extends Doctrine_Record {
    public function setTableDefinition() {
        // specialized validators 'email' and 'unique' used
        $this->hasColumn("address","string",150,"email|unique");
    }
}
20
$conn = Doctrine_Manager::getInstance()->openConnection(new PDO("dsn","username","password"));
doctrine's avatar
doctrine committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
$user = new User();
$user->name = "this is an example of too long name";

$user->save(); // throws a Doctrine_Validator_Exception

$user->name  = "valid name";
$user->created = "not valid"; // not valid type
$user->save(); // throws a Doctrine_Validator_Exception


$user->created = time();
$user->Email->address = "drink@.."; // not valid email address
$user->save(); // throws a Doctrine_Validator_Exception

$user->Email->address = "drink@drinkmore.info";
$user->save(); // saved


39
$user   = $conn->create("User");
doctrine's avatar
doctrine committed
40 41 42
$user->Email->address = "drink@drinkmore.info"; // not unique!
$user->save(); // throws a Doctrine_Validator_Exception     
?>