1
0
mirror of synced 2025-01-09 02:27:10 +03:00
doctrine2/manual/codes/Object relational mapping - Relations - Join table associations - Many-to-Many.php

55 lines
1.2 KiB
PHP
Raw Normal View History

2006-07-24 01:08:06 +04:00
<?php
class User extends Doctrine_Record {
public function setUp() {
$this->hasMany('Group','Groupuser.group_id');
2006-07-24 01:08:06 +04:00
}
public function setTableDefinition() {
$this->hasColumn('name','string',30);
2006-07-24 01:08:06 +04:00
}
}
class Group extends Doctrine_Record {
public function setUp() {
$this->hasMany('User','Groupuser.user_id');
2006-07-24 01:08:06 +04:00
}
public function setTableDefinition() {
$this->hasColumn('name','string',30);
2006-07-24 01:08:06 +04:00
}
}
class Groupuser extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn('user_id','integer');
$this->hasColumn('group_id','integer');
2006-07-24 01:08:06 +04:00
}
}
$user = new User();
// add two groups
$user->Group[0]->name = 'First Group';
2006-07-24 01:08:06 +04:00
$user->Group[1]->name = 'Second Group';
2006-07-24 01:08:06 +04:00
// save changes into database
$user->save();
2006-08-21 14:16:36 +04:00
// deleting the associations between user and groups it belongs to
$user->Groupuser->delete();
$groups = new Doctrine_Collection($conn->getTable('Group'));
2006-07-24 01:08:06 +04:00
$groups[0]->name = 'Third Group';
2006-07-24 01:08:06 +04:00
$groups[1]->name = 'Fourth Group';
2006-07-24 01:08:06 +04:00
$user->Group[2] = $groups[0];
// $user will now have 3 groups
$user->Group = $groups;
// $user will now have two groups 'Third Group' and 'Fourth Group'
2006-08-21 14:16:36 +04:00
2006-07-24 01:08:06 +04:00
?>