1
0
mirror of synced 2024-12-15 23:56:02 +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");
}
public function setTableDefinition() {
$this->hasColumn("name","string",30);
}
}
class Group extends Doctrine_Record {
public function setUp() {
$this->hasMany("User","Groupuser.user_id");
}
public function setTableDefinition() {
$this->hasColumn("name","string",30);
}
}
class Groupuser extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("user_id","integer");
$this->hasColumn("group_id","integer");
}
}
$user = new User();
// add two groups
$user->Group[0]->name = "First Group";
$user->Group[1]->name = "Second Group";
// 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";
$groups[1]->name = "Fourth Group";
$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
?>