Symfony2 - 从Doctrine中检索关系数据

时间:2017-03-08 12:08:54

标签: php symfony doctrine-orm

当我尝试获取这样的数据时,我得到了丢失的数据:

$this->em->getRepository("PollBundle:Poll")->findAll();

以下是我得到的结果:http://i.imgur.com/f0M54og.png 系统正在获取民意调查的数据,但是您可以看到没有任何关于频率的信息。怎么了?

PollFrequency实体如下所示:     

namespace PollBundle\Entity;

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

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

/**
 * @ORM\OneToMany(targetEntity="Poll", mappedBy="poll_frequency")
 */
private $polls;
/**
 * @var string
 * @ORM\Column(name="frequency", type="string")
 */
private $frequency;

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

public function __construct()
{
    $this->polls = new ArrayCollection();
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set frequency
 *
 * @param string $frequency
 * @return PollFrequency
 */
public function setFrequency($frequency)
{
    $this->frequency = $frequency;

    return $this;
}

/**
 * Get frequency
 *
 * @return string 
 */
public function getFrequency()
{
    return $this->frequency;
}

/**
 * Set description
 *
 * @param string $description
 * @return PollFrequency
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

/**
 * Get description
 *
 * @return string 
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Add polls
 *
 * @param \PollBundle\Entity\Poll $polls
 * @return PollFrequency
 */
public function addPoll(\PollBundle\Entity\Poll $polls)
{
    $this->polls[] = $polls;

    return $this;
}

/**
 * Remove polls
 *
 * @param \PollBundle\Entity\Poll $polls
 */
public function removePoll(\PollBundle\Entity\Poll $polls)
{
    $this->polls->removeElement($polls);
}

/**
 * Get polls
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getPolls()
{
    return $this->polls;
}
}

民意调查实体如下:

 <?php

 namespace PollBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

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

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

/**
 * @var boolean
 * @ORM\Column(name="pageload", nullable=false, type="boolean")
 */
private $pageload;

/**
 * @var integer
 * @ORM\Column(name="loadafter", nullable=false, type="integer")
 */
private $loadafter;

/**
 * @var boolean
 * @ORM\Column(name="exitload", nullable=false, type="boolean")
 */
private $exitload;

/**
 * @var integer
 * @ORM\Column(name="viewcount", nullable=false, type="integer")
 */
private $viewcount;

/**
 * @var integer
 * @ORM\Column(name="status", nullable=false, type="integer")
 */
private $status;

/**
 * @var integer
 * @ORM\Column(name="user_id", nullable=false, type="integer")
 */
private $user_id;

/**
 * @ORM\OneToMany(targetEntity="PollFrequency", mappedBy="frequency")
 * @ORM\JoinColumn(name="frequency_id", referencedColumnName="id")
 */
private $frequency;

/**
 * @var \DateTime
 * @ORM\Column(name="createdate", nullable=false, type="datetime")
 */
private $createdate;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return Poll
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set pageload
 *
 * @param boolean $pageload
 * @return Poll
 */
public function setPageload($pageload)
{
    $this->pageload = $pageload;

    return $this;
}

/**
 * Get pageload
 *
 * @return boolean 
 */
public function getPageload()
{
    return $this->pageload;
}

/**
 * Set loadafter
 *
 * @param integer $loadafter
 * @return Poll
 */
public function setLoadafter($loadafter)
{
    $this->loadafter = $loadafter;

    return $this;
}

/**
 * Get loadafter
 *
 * @return integer 
 */
public function getLoadafter()
{
    return $this->loadafter;
}

/**
 * Set exitload
 *
 * @param boolean $exitload
 * @return Poll
 */
public function setExitload($exitload)
{
    $this->exitload = $exitload;

    return $this;
}

/**
 * Get exitload
 *
 * @return boolean 
 */
public function getExitload()
{
    return $this->exitload;
}

/**
 * Set viewcount
 *
 * @param integer $viewcount
 * @return Poll
 */
public function setViewcount($viewcount)
{
    $this->viewcount = $viewcount;

    return $this;
}

/**
 * Get viewcount
 *
 * @return integer 
 */
public function getViewcount()
{
    return $this->viewcount;
}

/**
 * Set status
 *
 * @param integer $status
 * @return Poll
 */
public function setStatus($status)
{
    $this->status = $status;

    return $this;
}

/**
 * Get status
 *
 * @return integer 
 */
public function getStatus()
{
    return $this->status;
}

/**
 * Set user_id
 *
 * @param integer $userId
 * @return Poll
 */
public function setUserId($userId)
{
    $this->user_id = $userId;

    return $this;
}

/**
 * Get user_id
 *
 * @return integer 
 */
public function getUserId()
{
    return $this->user_id;
}

/**
 * Set createdate
 *
 * @param \DateTime $createdate
 * @return Poll
 */
public function setCreatedate($createdate)
{
    $this->createdate = $createdate;

    return $this;
}

/**
 * Get createdate
 *
 * @return \DateTime 
 */
public function getCreatedate()
{
    return $this->createdate;
}

/**
 * Set frequency
 *
 * @param \PollBundle\Entity\PollFrequency $frequency
 * @return Poll
 */
public function setFrequency(\PollBundle\Entity\PollFrequency $frequency = null)
{
    $this->frequency = $frequency;

    return $this;
}

/**
 * Get frequency
 *
 * @return \PollBundle\Entity\PollFrequency 
 */
public function getFrequency()
{
    return $this->frequency;
}
}

快乐编码给所有人!

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。如果你需要解决方案。就是这样:

 /**
 * @ORM\ManyToOne(targetEntity="PollFrequency", fetch="EAGER")
 * @ORM\JoinColumn(name="frequency_id", referencedColumnName="id")
 */
private $frequency;

我还删除了部分PollFrequency:

/**
* @ORM\OneToMany(targetEntity="Poll", mappedBy="poll_frequency")
*/
private $polls;

以及与$ polls相关的所有内容。

快乐编码给所有人!