我正在尝试使用doctrine2
将以下PostgreSQL代码实现到php中create table tournaments (
id serial primary key,
tournament_name text not null,
start_time timestamp not null,
end_time timestamp not null
);
create table group_slots (
id serial,
tournament_id int references tournaments,
start_time timestamp not null,
end_time timestamp not null,
primary key(id, tournament_id)
);
您可以注意到锦标赛和group_slots表之间需要OneToMany
双向关系,其中tournament_id
是引用列。以下是我到目前为止的情况:
Tournaments
实体:
<?php
namespace SportsBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use SportsBundle\Entity\GroupSlots;
/**
* Tournaments
*
* @ORM\Table(name="tournaments")
* @ORM\Entity(repositoryClass="SportsBundle\Repository\TournamentsRepository")
*/
class Tournaments {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @var string
*
* @ORM\Column(name="tournament_name", type="string", nullable=false)
*/
protected $tournament_name;
/**
*
* @var datetime
*
* @ORM\Column(name="start_time", type="datetime", nullable=false)
*/
protected $start_time;
/**
*
* @var datetime
*
* @ORM\Column(name="end_time", type="datetime", nullable=false)
*/
protected $end_time;
/**
* @ORM\OneToMany(targetEntity="GroupSlots", mappedBy="tournament")
*/
protected $groupslots;
/**
* @return ArrayCollection
*/
public function __construct() {
$this->groupslots = new ArrayCollection();
}
}
GroupSlots
实体:
<?php
namespace SportsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SportsBundle\Entity\Tournaments;
/**
* GroupSlots
*
* @ORM\Table(name="group_slots")
* @ORM\Entity(repositoryClass="SportsBundle\Repository\GroupSlotsRepository")
*/
class GroupSlots {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @var datetime
*
* @ORM\Column(name="start_time", type="datetime", nullable=false)
*/
protected $start_time;
/**
*
* @var datetime
*
* @ORM\Column(name="end_time", type="datetime", nullable=false)
*/
protected $end_time;
/**
* @ORM\ManyToOne(targetEntity="Tournaments", inversedBy="groupslots")
* @ORM\JoinColumn(name="tournament_id", referencedColumnName="id")
*/
protected $tournament;
}
问题:
我无法以标准方式在refrenced列上使用@ORM\Id
。如何在GroupSlots实体中创建包含id
和引用列tournament_id
的复合主键?
修改
我尝试将@Id
添加到$tournament
属性,如文档http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html
以下是更新的GroupSlots
实体:
<?php
namespace SportsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SportsBundle\Entity\Tournaments;
/**
* GroupSlots
*
* @ORM\Table(name="group_slots")
* @ORM\Entity(repositoryClass="SportsBundle\Repository\GroupSlotsRepository")
*/
class GroupSlots {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @var datetime
*
* @ORM\Column(name="start_time", type="datetime", nullable=false)
*/
protected $start_time;
/**
*
* @var datetime
*
* @ORM\Column(name="end_time", type="datetime", nullable=false)
*/
protected $end_time;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Tournaments", inversedBy="groupslots")
* @ORM\JoinColumn(name="tournament_id", referencedColumnName="id")
*/
protected $tournament;
}
在我运行php bin/console doctrine:schema:update --force
之后,我收到错误
[Doctrine\ORM\Mapping\MappingException]
Single id is not allowed on composite primary key in entity SportsBundle\Entity\GroupSlots
有什么想法吗?