1
0
mirror of synced 2025-01-17 22:11:41 +03:00

Initial entry.

This commit is contained in:
Jonathan.Wage 2007-09-20 16:01:33 +00:00
parent 370f649573
commit 17636810d5
5 changed files with 252 additions and 0 deletions

View File

@ -12,6 +12,9 @@
+ Event listeners
+ Class templates
+ Plugins
+ File Parser
+ Migration
+ Data Fixtures
+ Searching
+ Database abstraction
+ Improving Performance

View File

@ -0,0 +1,69 @@
Doctrine Data uses the Doctrine Parser for the dumping and loading of fixtures data so it is possible to use any of the formats available in the Parser. Currently yml is the only fully supported format but xml and others are next.
++ Exporting
You can export data to fixtures file in many different formats
<code type="php">
// A few ways exist for specifying where you export the data
// Dump to one large fixture file
Doctrine_Data::exportData('data.yml', 'yml');
// Dump to individual files. One file per model. 3rd argument true specifies to dump to individual files
Doctrine_Data::exportData('path/to/directory', 'yml', true);
</code>
++ Importing
You can import data from fixtures files in many different formats
<code type="php">
// Path can be in a few different formats
$path = 'path/to/data.yml'; // Path directly to one yml file
$path = array('data.yml', 'data2.yml', 'more.yml'); // Array of yml file paths
$path = array('directory1', 'directory2', 'directory3'); // Array of directories which contain yml files. It will find all files with an extension of .yml
// Specify the format of the data you are importing
$format = 'yml';
$format = 'xml';
$format = 'csv';
$models = array('User', 'Phonenumber'); // you can optionally specify an array of the models you wish to import the data for, by default it loads data for all the available loaded models and the data that exists
Doctrine_Data::importData($path, $format, $models);
</code>
++ Writing
You can write your fixtures files manually and load them in to your applications. Below is a sample data.yml fixtures file
<code type="yml">
---
Email:
Email_1:
address: jwage@mac.com
Group:
Group_1:
name: Drama Actors
User:
User_1:
Phonenumber: Phonenumber_1
name: Jonathan H. Wage
loginname: jwage
Email: Email_1
Groupuser:
Groupuser_1:
Group: Group_1
User: User_1
Phonenumber:
Phonenumber_1:
phonenumber: 555-555-5555
Group: Group_1
</code>
Here is how you would write code to load the data from that data.yml file
<code type="php">
Doctrine_Data::importData('data.yml', 'yml');
</code>

View File

@ -0,0 +1,68 @@
The parser is built to allow dumping and loading from many different formats. Currently xml and yml are the only drivers but later other file formats such as csv may be added. You can specify the data to load/dump in with the $type argument on dump() and load()
++ Dumping
Dumping array to yml variable
<code type="php">
$array = array('test' => array('key' => 'value'), 'test2' => 'test');
// Dump the array to yml and return, set to $yml(does not write to file). Replace null with a path to a yml file if you wish to write to disk
$yml = Doctrine_Parser::dump($array, null, 'yml');
</code>
$yml would contain the following
<code type="yaml">
---
test:
key: value
test2: test
</code>
Dumping array to yml file
<code type="php">
$array = array('test' => array('key' => 'value'), 'test2' => 'test');
// Dump the above array to test.yml using yml parser
Doctrine_Parser::dump($array, 'test.yml', 'yml');
</code>
A file named test.yml would be created and would contain the following
<code type="yaml">
---
test:
key: value
test2: test
</code>
++ Loading
Loading and parsing data from a yml file to a php array
<code type="php">
$array = array('test' => array('key' => 'value'), 'test2' => 'test');
// We dump the above array to test.yml using the yml parser dumper
Doctrine_Parser::dump($array, 'test.yml', 'yml');
// Now we reload that dumped yaml file back to the original array format using the yml parser loder
$array = Doctrine_Parser::load('test.yml', 'yml');
print_r($array);
</code>
The print_r() would output the following
<code>
Array
(
[test] => Array
(
[key] => value
)
[test2] => test
)
</code>

View File

@ -0,0 +1,112 @@
The Doctrine Migration tools allow you to migrate databases and it issues alter table statements directly to your databases when you need to deploy database changes.
++ Writing Migration Classes
Migration classes consist of a simple class that extends from Doctrine_Migration. You can define a public up() and down() method that is meant for doing and undoing changes to a database for that migration step.
<code type="php">
class MigrationTestTable extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('field1', 'string');
}
}
class Migration2 extends Doctrine_Migration
{
public function up()
{
$this->createTable('migration_test_table', array('field1' => array('type' => 'string')));
}
public function down()
{
$this->dropTable('migration_test_table');
}
}
class Migration3 extends Doctrine_Migration
{
public function up()
{
$this->addColumn('migration_test_table', 'field1', 'string');
}
public function down()
{
$this->renameColumn('migration_test_table', 'field1', 'field2');
}
}
class Migration4 extends Doctrine_Migration
{
public function up()
{
$this->changeColumn('migration_test_table', 'field1', 'integer');
}
public function down()
{
$this->changeColumn('migration_test_table', 'field1', 'string');
}
}
</code>
+++ Methods
Here is a list of the available methods you can use to alter your database in your migration classes
<code type="php">
public function createTable($tableName, array $fields = array(), array $options = array())
public function dropTable($tableName)
public function renameTable($oldTableName, $newTableName)
public function addColumn($tableName, $columnName, $type, array $options = array())
public function renameColumn($tableName, $oldColumnName, $newColumnName)
public function changeColumn($tableName, $columnName, $type, array $options = array())
public function removeColumn($tableName, $columnName)
public function addIndex($tableName, $indexName, array $options = array())
public function removeIndex($tableName, $indexName)
</code>
+++ Altering Data
You can alter table directly in your up() and down() methods like you normally would by creating new model instances and calling save() or creating queries and deleting data.
<code type="php">
class Migration1 extends Doctrine_Migration
{
public function up()
{
// Add new user
$user = new User();
$user->loginname = 'jwage';
$user->save();
}
public function down()
{
// Delete the user we added in the up
$query = new Doctrine_Query();
$query->delete('User')->from('User u')->where('u.loginname = ?', 'jwage')->execute();
}
}
</code>
++ Performing Migrations
<code type="php">
// Upgrade one at a time
Doctrine_Migration::migration(1, 2);
Doctrine_Migration::migration(2, 3);
Doctrine_Migration::migration(3, 4);
// Then revert back to version 1
Doctrine_Migration::migration(4, 1);
// One big upgrade
Doctrine_Migration::migration(1, 4);
echo Doctrine_Migration::getCurrentVersion(); // should echo 4
</code>

View File