This commit is contained in:
parent
6fd82d82b8
commit
6fd1080729
@ -71,42 +71,41 @@ $board->save();
|
||||
++ Foreign key associations
|
||||
+++ One-To-One
|
||||
|
||||
Binding One-To-One foreign key associations is done with {{Doctrine_Record::ownsOne()}} and {{Doctrine_Record::hasOne()}} methods. In the following example user owns one email and has one address. So the relationship between user and email is one-to-one composite. The relationship between user and address is one-to-one aggregate.
|
||||
One-to-one relations are propably the most basic relations. In the following example we have two classes, User and Email with their relation being one-to-one.
|
||||
|
||||
The {{Email}} component here is mapped to {{User}} component's column {{email_id}} hence their relation is called LOCALKEY relation. On the other hand the {{Address}} component is mapped to {{User}} by it's {{user_id}} column hence the relation between {{User}} and {{Address}} is called FOREIGNKEY relation.
|
||||
First lets take a look at the Email class. Since we are binding a one-to-one relationship we are using the hasOne() method. Notice how we define the foreign key column (user_id) in the Email class. This is due to a fact that Email is owned by the User class and not the other way around. In fact you should always follow this convention - always place the foreign key in the owned class.
|
||||
|
||||
The recommended naming convention for foreign key columns is: [tableName]_[primaryKey]. As here the foreign table is 'user' and its primary key is 'id' we have named the foreign key column as 'user_id'.
|
||||
|
||||
<code type="php">
|
||||
class User extends Doctrine_Record
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->hasOne('Address', array('local' => 'id', 'foreign' => 'user_id'));
|
||||
$this->hasOne('Email', array('local' => 'email_id', 'foreign' => 'id'));
|
||||
$this->hasMany('Phonenumber', array('local' => 'id', 'foreign' => 'user_id'));
|
||||
}
|
||||
public function setTableDefinition()
|
||||
{
|
||||
$this->hasColumn('name', 'string',50);
|
||||
$this->hasColumn('loginname', 'string',20);
|
||||
$this->hasColumn('password', 'string',16);
|
||||
|
||||
// foreign key column for email id
|
||||
$this->hasColumn('email_id', 'integer');
|
||||
}
|
||||
}
|
||||
class Email extends Doctrine_Record
|
||||
{
|
||||
public function setTableDefinition()
|
||||
{
|
||||
$this->hasColumn('user_id', 'integer');
|
||||
$this->hasColumn('address', 'string', 150);
|
||||
}
|
||||
}
|
||||
class Address extends Doctrine_Record
|
||||
{
|
||||
public function setTableDefinition()
|
||||
public function setUp()
|
||||
{
|
||||
$this->hasColumn('street', 'string', 50);
|
||||
$this->hasColumn('user_id', 'integer');
|
||||
$this->hasOne('User', array('local' => 'user_id', 'foreign' => 'id'));
|
||||
}
|
||||
}
|
||||
</code>
|
||||
|
||||
The User class is very similar to the Email class. Notice how the local and foreign columns are switched in the hasOne() definition compared to the definition of the Email class.
|
||||
|
||||
<code type="php">
|
||||
class User extends Doctrine_Record
|
||||
{
|
||||
public function setTableDefinition()
|
||||
{
|
||||
$this->hasColumn('name', 'string',50);
|
||||
$this->hasColumn('loginname', 'string',20);
|
||||
$this->hasColumn('password', 'string',16);
|
||||
}
|
||||
public function setUp()
|
||||
{
|
||||
$this->hasOne('Email', array('local' => 'id', 'foreign' => 'user_id'));
|
||||
}
|
||||
}
|
||||
</code>
|
||||
@ -114,6 +113,10 @@ class Address extends Doctrine_Record
|
||||
|
||||
+++ One-to-Many, Many-to-One
|
||||
|
||||
One-to-Many and Many-to-One relations are very similar to One-to-One relations. The recommended conventions you came in terms with in the previous chapter also apply to one-to-many and many-to-one relations.
|
||||
|
||||
In the following example we have two classes: User and Phonenumber. We define their relation as one-to-many (a user can have many phonenumbers). Here once again the Phonenumber is clearly owned by the User so we place the foreign key in the Phonenumber class.
|
||||
|
||||
<code type="php">
|
||||
class User extends Doctrine_Record
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user