Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 原则1和多个主键,可能吗?_Php_Doctrine - Fatal编程技术网

Php 原则1和多个主键,可能吗?

Php 原则1和多个主键,可能吗?,php,doctrine,Php,Doctrine,我可以有多个原则1的主键吗?如果没有,有什么解决办法吗?表中始终有一个主键(其数据库的限制) 您可以改为使用唯一密钥(我不知道doctrine中的唯一密钥支持如何。请访问手册)doctrine手册实际上没有提到这方面的任何内容,但Symfony(1.2)在手册中简要介绍了(Symfony使用doctrine作为默认ORM) 我想不出为什么条令不支持复合主键,因为您可以为连接表声明模型,它实际上由复合主键组成。是的,您可以。但是它没有很多文档。例如,如果我们希望实体用户具有多个位置,并且这些位置具

我可以有多个原则1的主键吗?如果没有,有什么解决办法吗?

表中始终有一个主键(其数据库的限制)

您可以改为使用唯一密钥(我不知道doctrine中的唯一密钥支持如何。请访问手册)

doctrine手册实际上没有提到这方面的任何内容,但Symfony(1.2)在手册中简要介绍了(Symfony使用doctrine作为默认ORM)


我想不出为什么条令不支持复合主键,因为您可以为连接表声明模型,它实际上由复合主键组成。

是的,您可以。但是它没有很多文档。例如,如果我们希望实体用户具有多个位置,并且这些位置具有多个用户,那么您需要以下内容:

$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();
在用户模型的设置方法中,您将:

$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
));
$this->hasMany('User as Users', array(
    'refClass' => 'UserLocation',
    'local' => 'location_id',
    'foreign' => 'user_id'
));
$this->hasMany('User', array(
     'local' => 'user_id',
     'foreign' => 'id'));

$this->hasMany('Location', array(
     'local' => 'location_id',
     'foreign' => 'id'));
在位置模型的设置方法中,您放置以下内容:

$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
));
$this->hasMany('User as Users', array(
    'refClass' => 'UserLocation',
    'local' => 'location_id',
    'foreign' => 'user_id'
));
$this->hasMany('User', array(
     'local' => 'user_id',
     'foreign' => 'id'));

$this->hasMany('Location', array(
     'local' => 'location_id',
     'foreign' => 'id'));
在多对多关系模型(UserLocation)的设置方法中,您可以设置:

$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
));
$this->hasMany('User as Users', array(
    'refClass' => 'UserLocation',
    'local' => 'location_id',
    'foreign' => 'user_id'
));
$this->hasMany('User', array(
     'local' => 'user_id',
     'foreign' => 'id'));

$this->hasMany('Location', array(
     'local' => 'location_id',
     'foreign' => 'id'));
现在,如果您要执行一个Doctrine_查询,并从location id:12获取所有用户,它将类似于:

$q = Doctrine_Query::create()
     ->select("u.*")
     ->from('User u')
     ->leftJoin("u.UserLocation ul") 
     ->where('ul.location_id = ?',12);
如果要插入用户,请记住也要创建UserLocation对象,如下所示:

$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();