外键为复合主键

时间:2017-02-11 14:19:19

标签: php postgresql doctrine-orm symfony

我正在尝试使用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 

有什么想法吗?

0 个答案:

没有答案