开发者

Doctrine 1 and multiple primary keys, is it possible?

开发者 https://www.devze.com 2023-03-26 18:53 出处:网络
Can i have multiple primary keys with Doctrine 1? I开发者_开发百科f not, any workarounds?There are always 1 primary key in table (Its Databases\' restriction)

Can i have multiple primary keys with Doctrine 1? I开发者_开发百科f not, any workarounds?


There are always 1 primary key in table (Its Databases' restriction)
You can use Unique keys instead( I don't know how about unique key support in doctrine. Visit manual on Doctrine site )


The doctrine manuals doesn't really mention anything about this, but Symfony (1.2) briefly covers this in their manual (symfony uses doctrine as their default ORM).

$userGroup = Doctrine::getTable('UserGroup')->find(array(1, 2));

I can't think of why doctrine wouldn't support composite primary keys, since you can declare models for junction table, which effectively consists of composite primary keys.


Yes you can. But It does not have a lot of documentation. For example, if we want the entity User to have many Locations, and the Locations to have many Users, then your need something like this:

In the setup method of your User model you put this:

$this->hasMany('Location as Locations', array(
    'refClass' => 'UserLocation', //Refering to the relation table
    'local' => 'user_id', //the user id in the realtion table
    'foreign' => 'location_id' //the location id in the relation table
));

In the setup method of the location model you put this:

$this->hasMany('User as Users', array(
    'refClass' => 'UserLocation',
    'local' => 'location_id',
    'foreign' => 'user_id'
));

In the setup method of the many to many relation model (UserLocation) you can put this:

$this->hasMany('User', array(
     'local' => 'user_id',
     'foreign' => 'id'));

$this->hasMany('Location', array(
     'local' => 'location_id',
     'foreign' => 'id'));

Now, if you want to do a Doctrine_Query, and get all the users from the location id: 12, it would be something like:

$q = Doctrine_Query::create()
     ->select("u.*")
     ->from('User u')
     ->leftJoin("u.UserLocation ul") 
     ->where('ul.location_id = ?',12);

If you are inserting a user remember to create the UserLocation objetct as well, something like this:

$userLocation = new UserLocation();
$userLocation->location_id = $locationId;
$userLocation->last_login = date('Y-m-d H:i:s');
$userLocation->user_id = $user->id; //from the user you created before
$userLocation->save();
0

精彩评论

暂无评论...
验证码 换一张
取 消