简体   繁体   English

教义2简单关系

[英]Doctrine2 Simple Relation

I think I'm missing something obvious with doctrine relationship mapping. 我认为我在教义关系映射方面缺少明显的东西。

I have two models, Microsite and Page (one to many, page only belongs to one site). 我有两种模型,微型站点和页面(一对多,页面仅属于一个站点)。

/*
 ...

 * @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;

The schema looks good: 模式看起来不错:

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)

but I can't get the fixtures to load properly (I had them working, but even then I could not get the related pages per site. 但是我无法使灯具正确加载(我可以使灯具正常工作,但是即使那样,我也无法获得每个站点的相关页面。

My question is: Are these annotations correct, and how do I correctly add data, and retrieve pages from a given site? 我的问题是:这些批注是否正确?如何正确添加数据并从给定站点检索页面?

Fixtures are: 治具是:

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

You need to set the $site property on the Page . 您需要在Page上设置$site属性。 I advise you to delegate the creation of a Page to the 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;
    }
}

You would then use it this way: 然后,您将以这种方式使用它:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM