Doctrine2简单关系

时间:2012-07-24 07:59:30

标签: php doctrine doctrine-orm associations fixtures

我认为我遗漏了与学说关系映射明显的东西。

我有两个模型,Microsite和Page(一对多,页面只属于一个站点)。

/*
 ...

 * @ORM\Table()
 * @ORM\Entity(repositoryClass="EP\PreReg\DataBundle\Entity\MicrositeRepository")
 */
class Microsite
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    ...

    /**
     * @ORM\OneToMany(targetEntity="Page", mappedBy="site")
     */
    private $pages;


    public function __construct() {
        $this->pages = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getPages() {
        return $this->pages;
    }

    public function addPage($page) {
        $this->pages[] = $page;
    }
/*
 * @ORM\Table()
 * @ORM\Entity
 */
class Page
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $title
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @var string $slug
     *
     * @ORM\Column(name="slug", type="string", length=100)
     */
    private $slug;

    /**
     * @ORM\ManyToOne(targetEntity="Microsite", inversedBy="pages")
     */
    private $site;

架构看起来不错:

CREATE TABLE Page (id INT AUTO_INCREMENT NOT NULL, site_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, slug VARCHAR(100) NOT NULL, INDEX IDX_B438191EF6BD1646 (site_id), PRIMARY KEY(id)) ENGINE = InnoDB;

CREATE TABLE Microsite (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, shortCode VARCHAR(20) NOT NULL, subdomain VARCHAR(100) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;

ALTER TABLE Page ADD CONSTRAINT FK_B438191EF6BD1646 FOREIGN KEY (site_id) REFERENCES Microsite(id)

但是我无法正确加载灯具(我让它们正常工作,但即便如此,我也无法获得每个站点的相关页面。

我的问题是:这些注释是否正确,如何正确添加数据,以及从给定网站检索网页?

赛程是:

class LoadMicrositeData implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {

        $site = new Microsite();
        $site->setName('Microsite Name');
        $site->setShortCode('MIC');
        $site->setSubdomain('micro');

        $homepage = new Page();
        $homepage->setSlug('/');
        $homepage->setTitle('Welcome');

        $site->addPage($homepage);

        $manager->persist($site);
        $manager->persist($homepage);

        $manager->flush();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在$site上设置Page属性。我建议你将Page的创建委托给Microsite:

class Microsite
{
    // ...

    public function addPage() {
        $page = new Page($this);
        $this->pages->add($page);
        return $page;
    }
}

class Page
{
    // ...

    public function __construct(Microsite $site) {
        $this->site = $site;
    }
}

然后你会这样使用它:

$site = new Microsite();
$site->setName('Microsite Name');
$site->setShortCode('MIC');
$site->setSubdomain('micro');

$homepage = $site->addPage();
$homepage->setSlug('/');
$homepage->setTitle('Welcome');