如何在symfony2中创建一个包含2个其他表的主键的表?

时间:2014-11-14 09:25:26

标签: php mysql sql-server database symfony

我有这样一个场景,我有2个表,并且想要创建第3个表,其列只是2个表的2个主键。这两个表是: -

Events.php
<?php

namespace InstituteEvents\StudentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="institute_event_details")
 */

class Events
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $eid;

/**
 * @ORM\Column(name="eventname",type="string", length=50)
 */
protected $eventname;

public function getEId() {
    return $this->eid;
}

public function getEventname()
{
    return $this->eventname;
}

public function setEventname($eventname)
{
    $this->eventname = $eventname;

    return $this;
}

}   

AND

Student.php     

namespace InstituteEvents\StudentBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="institute_student_details")
 */
class Student extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $uid;

/**
 * @ORM\Column(name="firstname",type="string", length=50)
 * @Assert\NotBlank(),
 * @Assert\Length(
 *      min = 3,
 *      max = 100,
 *      minMessage = "Your name must be at least {{ limit }} characters long",
 *      maxMessage = "Your name cannot be longer than {{ limit }} characters long")
 * @Assert\Regex(
 *     pattern="/\d/",
 *     match=false,
 *     message="Your name cannot contain a number")
 */
protected $firstname;

/**
 * @ORM\Column(name="lastname",type="string", length=50)
 * @Assert\Length(
 *      min = 3,
 *      max = 100,
 *      minMessage = "Your name must be at least {{ limit }} characters long",
 *      maxMessage = "Your name cannot be longer than {{ limit }} characters long")
 * @Assert\Regex(
 *     pattern="/\d/",
 *     match=false,
 *     message="Your name cannot contain a number")
 */
protected $lastname;

/**
 * @Assert\NotBlank()
 * @Assert\Email()
 * 
 */
protected $email;

/**
 * Plain password. Used for model validation. Must not be persisted.
 * @Assert\NotBlank()
 * 
 */
protected $plainPassword;

public function getFirstname()
{
    return $this->firstname;
}

public function getLastname()
{
    return $this->lastname;
}

public function setFirstname($firstname)
{
    $this->firstname = $firstname;

    return $this;
}

public function setLastname($lastname)
{
    $this->lastname = $lastname;

    return $this;
}

function getEmail() {
    return $this->email;
}

function getPlainPassword() {
    return $this->plainPassword;
}

function setEmail($email) {
    $this->email = $email;
}

function setPlainPassword($plainPassword) {
    $this->plainPassword = $plainPassword;
}

public function __construct()
{
    parent::__construct();
    // your own logic
}
}

现在,我想在数据库中创建一个新表“Subscription”,其列只是“uid”和“eid”。现在很多“学生”都可以注册许多“活动”。这些表之间的关系是什么,我在哪里指定: -

Subscription.php

<?php

namespace InstituteEvents\StudentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="institute_subscription_details")
 */

class Subscription
{

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 */
protected $uid;

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 */
protected $eid;

 public function getUId() {
    return $this->uid;
}

 public function getEId() {
    return $this->eid;
}
}   

如何在数据库中创建这些表以及这些表之间的关系是什么条件: - 许多“学生”可以注册许多“事件”而许多“事件”可以有很多“学生”? ??

1 个答案:

答案 0 :(得分:2)

我不知道为什么你需要额外的实体。 DoctrineORM提供了多对多的双向关系。

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional