Commit 70caab15 authored by meus's avatar meus

Fixed some docs. Mainly substituting " with ' in code examples.

parent 7f68e096
<?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
)
);
$this->hasColumn("address2", // name of the column
"string", // column type
"200", // column length
// validators / constraints without arguments can be
// specified also as as string with | separator
"notblank|email"
);
// Doctrine even supports the following format for
// validators / constraints which have no arguments:
$this->hasColumn("address3", // name of the column
"string", // column type
"200", // column length
array("notblank", "email")
);
}
}
?>
<?php <?php
class User extends Doctrine_Record { class User extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("uid","integer",20,"primary|autoincrement"); $this->hasColumn('uid','integer',20,'primary|autoincrement');
} }
} }
?> ?>
<?php <?php
class Groupuser extends Doctrine_Record { class Groupuser extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("user_id", "integer", 20, "primary"); $this->hasColumn('user_id', 'integer', 20, 'primary');
$this->hasColumn("group_id", "integer", 20, "primary"); $this->hasColumn('group_id', 'integer', 20, 'primary');
} }
} }
?> ?>
<?php <?php
class User extends Doctrine_Record { class User extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",200,"primary"); $this->hasColumn('name','string',200,'primary');
} }
} }
?> ?>
<?php <?php
class User extends Doctrine_Record { class User extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->ownsMany("Phonenumber","Phonenumber.user_id"); $this->ownsMany('Phonenumber','Phonenumber.user_id');
} }
public function setTableDefition() { public function setTableDefition() {
$this->hasColumn("name","string",50); $this->hasColumn('name','string',50);
$this->hasColumn("loginname","string",20); $this->hasColumn('loginname','string',20);
$this->hasColumn("password","string",16); $this->hasColumn('password','string',16);
} }
} }
class Phonenumber extends Doctrine_Record { class Phonenumber extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("phonenumber","string",50); $this->hasColumn('phonenumber','string',50);
$this->hasColumn("user_id","integer"); $this->hasColumn('user_id','integer');
} }
} }
?> ?>
<?php <?php
class User extends Doctrine_Record { class User extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->hasOne("Address","Address.user_id"); $this->hasOne('Address','Address.user_id');
$this->ownsOne("Email","User.email_id"); $this->ownsOne('Email','User.email_id');
$this->ownsMany("Phonenumber","Phonenumber.user_id"); $this->ownsMany('Phonenumber','Phonenumber.user_id');
} }
public function setTableDefition() { public function setTableDefition() {
$this->hasColumn("name","string",50); $this->hasColumn('name','string',50);
$this->hasColumn("loginname","string",20); $this->hasColumn('loginname','string',20);
$this->hasColumn("password","string",16); $this->hasColumn('password','string',16);
// foreign key column for email ID // foreign key column for email ID
$this->hasColumn("email_id","integer"); $this->hasColumn('email_id','integer');
} }
} }
class Email extends Doctrine_Record { class Email extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("address","string",150); $this->hasColumn('address','string',150);
} }
} }
class Address extends Doctrine_Record { class Address extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("street","string",50); $this->hasColumn('street','string',50);
$this->hasColumn("user_id","integer"); $this->hasColumn('user_id','integer');
} }
} }
?> ?>
<?php <?php
class Task extends Doctrine_Record { class Task extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->hasOne("Task as Parent","Task.parent_id"); $this->hasOne('Task as Parent','Task.parent_id');
$this->hasMany("Task as Subtask","Subtask.parent_id"); $this->hasMany('Task as Subtask','Subtask.parent_id');
} }
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",100); $this->hasColumn('name','string',100);
$this->hasColumn("parent_id","integer"); $this->hasColumn('parent_id','integer');
} }
} }
?> ?>
<?php <?php
class Entity extends Doctrine_Record { class Entity extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",30); $this->hasColumn('name','string',30);
$this->hasColumn("username","string",20); $this->hasColumn('username','string',20);
$this->hasColumn("password","string",16); $this->hasColumn('password','string',16);
$this->hasColumn("created","integer",11); $this->hasColumn('created','integer',11);
} }
} }
......
<?php <?php
class Entity extends Doctrine_Record { class Entity extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",30); $this->hasColumn('name','string',30);
$this->hasColumn("username","string",20); $this->hasColumn('username','string',20);
$this->hasColumn("password","string",16); $this->hasColumn('password','string',16);
$this->hasColumn("created","integer",11); $this->hasColumn('created','integer',11);
} }
} }
......
<?php <?php
class User extends Doctrine_Record { class User extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->hasMany("Group","Groupuser.group_id"); $this->hasMany('Group','Groupuser.group_id');
} }
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",30); $this->hasColumn('name','string',30);
} }
} }
class Group extends Doctrine_Record { class Group extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->hasMany("User","Groupuser.user_id"); $this->hasMany('User','Groupuser.user_id');
} }
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",30); $this->hasColumn('name','string',30);
} }
} }
class Groupuser extends Doctrine_Record { class Groupuser extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("user_id","integer"); $this->hasColumn('user_id','integer');
$this->hasColumn("group_id","integer"); $this->hasColumn('group_id','integer');
} }
} }
...@@ -28,9 +28,9 @@ class Groupuser extends Doctrine_Record { ...@@ -28,9 +28,9 @@ class Groupuser extends Doctrine_Record {
$user = new User(); $user = new User();
// add two groups // add two groups
$user->Group[0]->name = "First Group"; $user->Group[0]->name = 'First Group';
$user->Group[1]->name = "Second Group"; $user->Group[1]->name = 'Second Group';
// save changes into database // save changes into database
$user->save(); $user->save();
...@@ -39,11 +39,11 @@ $user->save(); ...@@ -39,11 +39,11 @@ $user->save();
$user->Groupuser->delete(); $user->Groupuser->delete();
$groups = new Doctrine_Collection($conn->getTable("Group")); $groups = new Doctrine_Collection($conn->getTable('Group'));
$groups[0]->name = "Third Group"; $groups[0]->name = 'Third Group';
$groups[1]->name = "Fourth Group"; $groups[1]->name = 'Fourth Group';
$user->Group[2] = $groups[0]; $user->Group[2] = $groups[0];
// $user will now have 3 groups // $user will now have 3 groups
......
<?php <?php
class User extends Doctrine_Record { class User extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->hasMany("User as Friend","UserReference.user_id-user_id2"); $this->hasMany('User as Friend','UserReference.user_id-user_id2');
} }
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",30); $this->hasColumn('name','string',30);
} }
} }
class UserReference extends Doctrine_Record { class UserReference extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("user_id","integer"); $this->hasColumn('user_id','integer');
$this->hasColumn("user_id2","integer"); $this->hasColumn('user_id2','integer');
} }
} }
?> ?>
<?php <?php
class Forum_Board extends Doctrine_Record { class Forum_Board extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name", "string", 100); $this->hasColumn('name', 'string', 100);
$this->hasColumn("description", "string", 5000); $this->hasColumn('description', 'string', 5000);
} }
public function setUp() { public function setUp() {
// notice the 'as' keyword here // notice the 'as' keyword here
$this->ownsMany("Forum_Thread as Threads", "Forum_Thread.board_id"); $this->ownsMany('Forum_Thread as Threads', 'Forum_Thread.board_id');
} }
} }
class Forum_Thread extends Doctrine_Record { class Forum_Thread extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("board_id", "integer", 10); $this->hasColumn('board_id', 'integer', 10);
$this->hasColumn("updated", "integer", 10); $this->hasColumn('updated', 'integer', 10);
$this->hasColumn("closed", "integer", 1); $this->hasColumn('closed', 'integer', 1);
} }
public function setUp() { public function setUp() {
// notice the 'as' keyword here // notice the 'as' keyword here
$this->hasOne("Forum_Board as Board", "Forum_Thread.board_id"); $this->hasOne('Forum_Board as Board', 'Forum_Thread.board_id');
} }
} }
$board = new Board(); $board = new Board();
......
<?php <?php
$user = new User(); $user = new User();
$user->name = "Jack"; $user->name = 'Jack';
$group = $conn->create("Group"); $group = $conn->create('Group');
$group->name = "Drinking Club"; $group->name = 'Drinking Club';
// saves all the changed objects into database // saves all the changed objects into database
......
...@@ -3,9 +3,9 @@ $manager = Doctrine_Manager::getInstance(); ...@@ -3,9 +3,9 @@ $manager = Doctrine_Manager::getInstance();
// open new connection // open new connection
$conn = $manager->openConnection(new PDO("dsn","username","password")); $conn = $manager->openConnection(new PDO('dsn','username','password'));
// getting a table object // getting a table object
$table = $conn->getTable("User"); $table = $conn->getTable('User');
?> ?>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// select all users // select all users
$users = $conn->query("FROM User"); $users = $conn->query('FROM User');
// select all users where user email is jackdaniels@drinkmore.info // select all users where user email is jackdaniels@drinkmore.info
...@@ -10,5 +10,5 @@ $users = $conn->query("FROM User WHERE User.Email.address = 'jackdaniels@drinkmo ...@@ -10,5 +10,5 @@ $users = $conn->query("FROM User WHERE User.Email.address = 'jackdaniels@drinkmo
// using prepared statements // using prepared statements
$users = $conn->query("FROM User WHERE User.name = ?", array('Jack')); $users = $conn->query('FROM User WHERE User.name = ?', array('Jack'));
?> ?>
...@@ -5,15 +5,15 @@ $manager = Doctrine_Manager::getInstance(); ...@@ -5,15 +5,15 @@ $manager = Doctrine_Manager::getInstance();
// open first connection // open first connection
$conn = $manager->openConnection(new PDO("dsn","username","password"), "connection 1"); $conn = $manager->openConnection(new PDO('dsn','username','password'), 'connection 1');
// open second connection // open second connection
$conn2 = $manager->openConnection(new PDO("dsn2","username2","password2"), "connection 2"); $conn2 = $manager->openConnection(new PDO('dsn2','username2','password2'), 'connection 2');
$manager->getCurrentConnection(); // $conn2 $manager->getCurrentConnection(); // $conn2
$manager->setCurrentConnection("connection 1"); $manager->setCurrentConnection('connection 1');
$manager->getCurrentConnection(); // $conn $manager->getCurrentConnection(); // $conn
......
...@@ -6,13 +6,13 @@ $manager = Doctrine_Manager::getInstance(); ...@@ -6,13 +6,13 @@ $manager = Doctrine_Manager::getInstance();
// Doctrine_Connection // Doctrine_Connection
// a script may have multiple open connections // a script may have multiple open connections
// (= multiple database connections) // (= multiple database connections)
$dbh = new PDO("dsn","username","password"); $dbh = new PDO('dsn','username','password');
$conn = $manager->openConnection(); $conn = $manager->openConnection();
// or if you want to use Doctrine Doctrine_Db and its // or if you want to use Doctrine Doctrine_Db and its
// performance monitoring capabilities // performance monitoring capabilities
$dsn = "schema://username:password@dsn/dbname"; $dsn = 'schema://username:password@dsn/dbname';
$dbh = Doctrine_Db::getConnection($dsn); $dbh = Doctrine_Db::getConnection($dsn);
$conn = $manager->openConnection(); $conn = $manager->openConnection();
?> ?>
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
// NOTE: related record have always the first letter in uppercase // NOTE: related record have always the first letter in uppercase
$email = $user->Email; $email = $user->Email;
$email->address = "jackdaniels@drinkmore.info"; $email->address = 'jackdaniels@drinkmore.info';
$user->save(); $user->save();
// alternative: // alternative:
$user->Email->address = "jackdaniels@drinkmore.info"; $user->Email->address = 'jackdaniels@drinkmore.info';
$user->save(); $user->save();
?> ?>
<?php <?php
print $user->Email["address"]; print $user->Email['address'];
print $user->Phonenumber[0]->phonenumber; print $user->Phonenumber[0]->phonenumber;
print $user->Group[0]->get("name"); print $user->Group[0]->name;
?> ?>
<?php <?php
$user->Email["address"] = "koskenkorva@drinkmore.info"; $user->Email['address'] = 'koskenkorva@drinkmore.info';
$user->Phonenumber[0]->phonenumber = "123123"; $user->Phonenumber[0]->phonenumber = '123123';
$user->save(); $user->save();
......
Setting up a table definition in doctrine is done by using hasColumn method calls inside setTableDefinition method. Doctrine_Record::hasColumn() takes 4 arguments:
Doctrine_Record::hasColumn() takes 4 arguments:<br \>
1. [<b class="title">column name</b>] <br /> # **column name**
2. [<b class="title">column type</b>] <br /> # **column type**
3. [<b class="title">column length</b>] <br /> # **column length**
4. [<b class="title">column constraints and validators</b>] # **column constraints and validators**
<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
)
);
$this->hasColumn('address2', // name of the column
'string', // column type
'200', // column length
// validators / constraints without arguments can be
// specified also as as string with | separator
'notblank|email'
);
// Doctrine even supports the following format for
// validators / constraints which have no arguments:
$this->hasColumn('address3', // name of the column
'string', // column type
'200', // column length
array('notblank', 'email')
);
}
}
</code>
...@@ -4,83 +4,83 @@ The entity table has a column called 'type' which tells whether an entity is a g ...@@ -4,83 +4,83 @@ The entity table has a column called 'type' which tells whether an entity is a g
The only thing we have to do is to create 3 records (the same as before) and add call the Doctrine_Table::setInheritanceMap() method inside the setUp() method. The only thing we have to do is to create 3 records (the same as before) and add call the Doctrine_Table::setInheritanceMap() method inside the setUp() method.
<code type="php"> <code type='php'>
class Entity extends Doctrine_Record { class Entity extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",30); $this->hasColumn('name','string',30);
$this->hasColumn("username","string",20); $this->hasColumn('username','string',20);
$this->hasColumn("password","string",16); $this->hasColumn('password','string',16);
$this->hasColumn("created","integer",11); $this->hasColumn('created','integer',11);
// this column is used for column // this column is used for column
// aggregation inheritance // aggregation inheritance
$this->hasColumn("type", "integer", 11); $this->hasColumn('type', 'integer', 11);
} }
} }
class User extends Entity { class User extends Entity {
public function setUp() { public function setUp() {
$this->setInheritanceMap(array("type"=>1)); $this->setInheritanceMap(array('type'=>1));
} }
} }
class Group extends Entity { class Group extends Entity {
public function setUp() { public function setUp() {
$this->setInheritanceMap(array("type"=>2)); $this->setInheritanceMap(array('type'=>2));
} }
} }
</code> </code>
If we want to be able to fetch a record from the Entity table and automatically get a User record if the Entity we fetched is a user we have to do set the subclasses option in the parent class. The adjusted example: If we want to be able to fetch a record from the Entity table and automatically get a User record if the Entity we fetched is a user we have to do set the subclasses option in the parent class. The adjusted example:
<code type="php"> <code type='php'>
class Entity extends Doctrine_Record { class Entity extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",30); $this->hasColumn('name','string',30);
$this->hasColumn("username","string",20); $this->hasColumn('username','string',20);
$this->hasColumn("password","string",16); $this->hasColumn('password','string',16);
$this->hasColumn("created","integer",11); $this->hasColumn('created','integer',11);
// this column is used for column // this column is used for column
// aggregation inheritance // aggregation inheritance
$this->hasColumn("type", "integer", 11); $this->hasColumn('type', 'integer', 11);
$this->option("subclasses", array("User", "Group"); $this->option('subclasses', array('User', 'Group');
} }
} }
class User extends Entity { class User extends Entity {
public function setUp() { public function setUp() {
$this->setInheritanceMap(array("type"=>1)); $this->setInheritanceMap(array('type'=>1));
} }
} }
class Group extends Entity { class Group extends Entity {
public function setUp() { public function setUp() {
$this->setInheritanceMap(array("type"=>2)); $this->setInheritanceMap(array('type'=>2));
} }
} }
</code> </code>
We can then do the following given the previous table mapping. We can then do the following given the previous table mapping.
<code type="php"> <code type='php'>
$user = new User(); $user = new User();
$user->name="Bjarte S. Karlsen"; $user->name='Bjarte S. Karlsen';
$user->username="meus"; $user->username='meus';
$user->password="rat"; $user->password='rat';
$user->save(); $user->save();
$group = new Group(); $group = new Group();
$group->name="Users"; $group->name='Users';
$group->username="users"; $group->username='users';
$group->password="password"; $group->password='password';
$group->save(); $group->save();
$q = Doctrine_Query(); $q = Doctrine_Query();
$user = $q->from("Entity")->where("id=?")->execute(array($user->id))->getFirst(); $user = $q->from('Entity')->where('id=?')->execute(array($user->id))->getFirst();
$q = Doctrine_Query(); $q = Doctrine_Query();
$group = $q->from("Entity")->where("id=?")->execute(array($group->id))->getFirst(); $group = $q->from('Entity')->where('id=?')->execute(array($group->id))->getFirst();
</code> </code>
The user object is here an instance of User while the group object is an instance of Group. The user object is here an instance of User while the group object is an instance of Group.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment