introduction.txt 3.1 KB
Newer Older
zYne's avatar
zYne committed
1
This chapter and its subchapters tell you how to do basic schema mappings with Doctrine. After you've come in terms with the concepts of this chapter you'll know how to:
zYne's avatar
zYne committed
2

zYne's avatar
zYne committed
3
1. Define columns for your record classes
zYne's avatar
zYne committed
4 5 6
2. Define table options
3. Define indexes
4. Define basic constraints and validators for columns
zYne's avatar
zYne committed
7 8 9 10 11 12 13 14 15 16 17 18 19

All column mappings within Doctrine are being done via the hasColumn() method of the Doctrine_Record. The hasColumn takes 4 arguments:

# **column name**   String that specifies the column name and optional alias. This is needed for all columns. If you want to specify an alias for the column name you'll need to use the format '[columnName] as [columnAlias]'
# **column type**   String that specifies the column type. See the column types section.
# **column length**  Integer that specifies the column length. Some column types depend not only the given portable type but also on the given length. For example type string with length 1000 will be translated into native type TEXT on mysql.
# **column constraints and validators** An array that specifies the list of constraints and validators applied to given column.

Note that validators / column constraints and the column length fields are optional.  The length may be omitted by using **null** for the length argument, allowing doctrine to use a default length and permitting a fourth argument for validation or column constraints.

Lets take our first example. The following definition defines a class called Email which refers to a table called 'emails'. The Email class has two columns id (an auto-incremented primary key column) and a string column called address.

Notice how we add two validators / constraints for the address column (notblank and email). The notblank validator assures that the address column isn't blank (so it must not contain space-characters only) whereas the email validator ensures that the address is a valid email address.
zYne's avatar
zYne committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33

<code type="php">
class Email extends Doctrine_Record {
    public function setTableDefinition() {
        // setting custom table name:
        $this->setTableName('emails');

        $this->hasColumn('address',         // name of the column
                         'string',          // column type
                         '200',             // column length
                         array('notblank' => true,
                               'email'    => true  // validators / constraints
                               )
                         );
zYne's avatar
zYne committed
34 35 36
    }
}
</code>
zYne's avatar
zYne committed
37

zYne's avatar
zYne committed
38
Now lets create an export script for this class:
zYne's avatar
zYne committed
39

zYne's avatar
zYne committed
40
<code type="php">
zYne's avatar
zYne committed
41 42
require_once('Email.php');
require_once('path-to-Doctrine/Doctrine.php');
zYne's avatar
zYne committed
43

zYne's avatar
zYne committed
44
require_once('path-to-doctrine/lib/Doctrine.php');
zYne's avatar
zYne committed
45

zYne's avatar
zYne committed
46 47 48 49 50 51 52
spl_autoload_register(array('Doctrine', 'autoload'));

// in order to export we need a database connection
$manager = Doctrine_Manager::getInstance();
$conn    = $manager->openConnection('mysql://user:pass@localhost/test');

$conn->export->exportClasses(array('Email'));
zYne's avatar
zYne committed
53 54
</code>

zYne's avatar
zYne committed
55 56 57
The script would execute the following sql (we are using Mysql here as the database backend):

<code>
zYne's avatar
zYne committed
58
CREATE TABLE emails (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(200) NOT NULL)
zYne's avatar
zYne committed
59 60 61 62 63 64 65
</code>