Symfony2 + Doctrine中的ManyToOne

时间:2014-06-12 16:01:09

标签: symfony doctrine

我想创建4个表之间的关系。

  • 页面(页面,字段:名称,uri,状态表)

  • 块(块表,这里我可以有特定页面的多个记录,字段:id,parrent_id,模板)

  • block_content(每个块的内容表,这里可以有特定块的多个记录,字段:id,block_id,lang,content)

  • block_relation(保持页表和块表之间的关系,字段:id,page_id,block_id)

我如何在这些表之间建立关系。

直到现在我已经创造了这个:

block_content:

    <?php

namespace Casewise\Bundle\DispatcherBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * BlockRelation
 *
 * @ORM\Table(name="block_relation")
 * @ORM\Entity
 */
class BlockRelation
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Casewise\Bundle\PageBundle\Entity\Page", inversedBy="block_relation")
     * @ORM\JoinColumn(name="page_id", referencedColumnName="page_id", nullable=FALSE)
     */
    private $pageid;

    /**
     * @ORM\ManyToOne(targetEntity="Casewise\Bundle\CmsBundle\Entity\Block", inversedBy="block_relation")
     * @ORM\JoinColumn(name="block_id", referencedColumnName="block_id", nullable=FALSE)
     */
    private $blockid;



    /** SETTERS AND GETTERS */



    /**
     * Gets the value of id.
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Sets the value of id.
     *
     * @param integer $id the id 
     *
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Gets the value of pageid.
     *
     * @return integer
     */
    public function getPageid()
    {
        return $this->pageid;
    }

    /**
     * Sets the value of pageid.
     *
     * @param integer $pageid the pageid 
     *
     * @return self
     */
    public function setPageid($pageid)
    {
        $this->pageid = $pageid;

        return $this;
    }

    /**
     * Gets the value of blockid.
     *
     * @return integer
     */
    public function getBlockid()
    {
        return $this->blockid;
    }

    /**
     * Sets the value of blockid.
     *
     * @param integer $blockid the blockid 
     *
     * @return self
     */
    public function setBlockid($blockid)
    {
        $this->blockid = $blockid;

        return $this;
    }
}

阻止:     

namespace Casewise\Bundle\CmsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
//use Doctrine\Common\Collections\ArrayCollection;

/**
 * Block
 *
 * @ORM\Table(name="block")
 * @ORM\Entity
 */
class Block
{
    /**
     * @var integer
     *
     * @ORM\Column(name="block_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $blockid;

    /**
     * @var integer
     *
     * @ORM\Column(name="parrent_id", type="integer")
     */
    private $parrentid;

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

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="boolean", options={"default" = 1})
     */
    private $status;



    /** SETTERS AND GETTERS */



    /**
     * Gets the value of blockid.
     *
     * @return integer
     */
    public function getBlockid()
    {
        return $this->blockid;
    }

    /**
     * Sets the value of blockid.
     *
     * @param integer $blockid the blockid 
     *
     * @return self
     */
    public function setBlockid($blockid)
    {
        $this->blockid = $blockid;

        return $this;
    }

    /**
     * Gets the value of parrentid.
     *
     * @return integer
     */
    public function getParrentid()
    {
        return $this->parrentid;
    }

    /**
     * Sets the value of parrentid.
     *
     * @param integer $parrentid the parrentid 
     *
     * @return self
     */
    public function setParrentid($parrentid)
    {
        $this->parrentid = $parrentid;

        return $this;
    }

    /**
     * Gets the value of template.
     *
     * @return string
     */
    public function getTemplate()
    {
        return $this->template;
    }

    /**
     * Sets the value of template.
     *
     * @param string $template the template 
     *
     * @return self
     */
    public function setTemplate($template)
    {
        $this->template = $template;

        return $this;
    }

    /**
     * Gets the value of status.
     *
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Sets the value of status.
     *
     * @param string $status the status 
     *
     * @return self
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }
}

BlockContent:

    <?php

   namespace Casewise\Bundle\CmsBundle\Entity;

  use Doctrine\ORM\Mapping as ORM;

/**
 * Block
 *
 * @ORM\Table(name="block_content")
 * @ORM\Entity
 */
class BlockContent
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Casewise\Bundle\CmsBundle\Entity\Block", inversedBy="block_id")
     * @ORM\JoinColumn(name="block_id", referencedColumnName="block_id", nullable=FALSE)
     */
    private $blockid;


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


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


    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text")
     */
    private $content;



    /** SETTERS AND GETTERS */



    /**
     * Gets the value of id.
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Sets the value of id.
     *
     * @param integer $id the id 
     *
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Gets the value of blockid.
     *
     * @return integer
     */
    public function getBlockid()
    {
        return $this->blockid;
    }

    /**
     * Sets the value of blockid.
     *
     * @param integer $blockid the blockid 
     *
     * @return self
     */
    public function setBlockid($blockid)
    {
        $this->blockid = $blockid;

        return $this;
    }

    /**
     * Gets the value of name.
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Sets the value of name.
     *
     * @param string $name the name 
     *
     * @return self
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Gets the value of language.
     *
     * @return string
     */
    public function getLanguage()
    {
        return $this->language;
    }

    /**
     * Sets the value of language.
     *
     * @param string $language the language 
     *
     * @return self
     */
    public function setLanguage($language)
    {
        $this->language = $language;

        return $this;
    }

    /**
     * Gets the value of content.
     *
     * @return string
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Sets the value of content.
     *
     * @param string $content the content 
     *
     * @return self
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }
}

网页

<?php

namespace Casewise\Bundle\PageBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
//use Doctrine\Common\Collections\ArrayCollection;

/**
 * Page
 *
 * @ORM\Table(name="page")
 * @ORM\Entity
 */
class Page
{
    /**
     * @var integer
     *
     * @ORM\Column(name="page_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $pageid;


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

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

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="boolean", options={"default" = 1})
     */
    private $status;



    /** SETTERS AND GETTERS */



    /**
     * Gets the value of id.
     *
     * @return integer
     */
    public function getPageId()
    {
        return $this->pageid;
    }

    /**
     * Sets the value of id.
     *
     * @param integer $id the id 
     *
     * @return self
     */
    public function setPageId($pageid)
    {
        $this->pageid = $pageid;

        return $this;
    }

    /**
     * Gets the value of name.
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Sets the value of name.
     *
     * @param string $name the name 
     *
     * @return self
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Gets the value of uri.
     *
     * @return string
     */
    public function getUri()
    {
        return $this->uri;
    }

    /**
     * Sets the value of uri.
     *
     * @param string $uri the uri 
     *
     * @return self
     */
    public function setUri($uri)
    {
        $this->uri = $uri;

        return $this;
    }

    /**
     * Gets the value of status.
     *
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Sets the value of status.
     *
     * @param string $status the status 
     *
     * @return self
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }
}

更新

我用以下内容更新了shema:doctrine:schema:update --force 当我请求关系实体并给他pageid时,我需要将所有相关块提供给特定页面,但它返回一个巨大的对象,其中包含symfony2特定对象(超过3000行的对象) ,如请求,响应和与我的请求无关的其他对象。

这就是我请求实体的方式:

$blocks = $this->getDoctrine()
    ->getRepository('CasewiseDispatcherBundle:BlockRelation')
    ->findByPageid(1);
        echo '<pre>';
            print_r($blocks);
        echo '</pre>';

0 个答案:

没有答案