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

66 lines
3.1 KiB
Plaintext
Raw Normal View History

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