Cały czas nie moge sie połapać w tym Doctrine. Mam 3 tabele w bazie danych:
- Users (id, name, password)
- Groups(id, name) nazwy grup
- UserGroups(id, uid, gid) grupy przydzielone użytkownikom
No wiec napisałem w Doctrine:
<?php class Users extends Doctrine_Record { public function setUp() { $this->hasMany('Groups', 'UserGroups.gid'); } public function setTableDefinition() { $this->hasColumn('name', 'string', 20, 'notnull|unique'); $this->hasColumn('password', 'string', 40, 'notnull'); } } class Groups extends Doctrine_Record { public function setUp() { $this->hasMany('Users', 'UserGroups.uid'); } public function setTableDefinition() { $this->hasColumn('name', 'string', 30, 'notnull|unique'); } } class UserGroups extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn('uid', 'integer', 10, 'notnull'); $this->hasColumn('gid', 'integer', 10, 'notnull'); } } ?>
Aby dodać użytkownika robie tak:
<?php $user = new Users; $user->name = 'Apo'; $user->Groups[0]->name = 'admin'; // $user->save(); ?>
Po takim czymś dodaje mi sie użytkownik o nazwie Apo, dodaje sie grupa admin i automatycznie przypisuje sie ta grupa do usera w tabeli UserGroups. No i w tym problem żeby ta grupa za każdym razem sie nowa nie tworzyła tylko dała domyślną grupe admin (bez jej tworzenia). I chcialem sie zapytać jak powinno to optymalnie i poprawnie wyglądać. Zrobiłem coś takiego:
<?php $user = new Users; $user->name = 'admin'; $user->save(); $group = new UserGroups; $group->uid = 5; // tu trzeba dać id aktualnie tworzonego usera ale how to make this ? $group->gid = 1; // czyli admin $g->save(); ?>
Pozdrawiam