1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/manual/docs/en/basic-schema-mapping/introduction.txt
2007-10-17 21:16:49 +00:00

66 lines
3.1 KiB
Plaintext

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:
1. Define columns for your record classes
2. Define table options
3. Define indexes
4. Define basic constraints and validators for columns
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.
<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
)
);
}
}
</code>
Now lets create an export script for this class:
<code type="php">
require_once('Email.php');
require_once('path-to-Doctrine/Doctrine.php');
require_once('path-to-doctrine/lib/Doctrine.php');
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'));
</code>
The script would execute the following sql (we are using Mysql here as the database backend):
<code>
CREATE TABLE emails (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(200) NOT NULL)
</code>