dpgua04022 2017-03-08 12:08
浏览 40
已采纳

Symfony2 - 从Doctrine中检索关系数据

When I try to get data like this, I got missing data:

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

Here are the results that I got: http://i.imgur.com/f0M54og.png The system is getting poll's data however nothing to show about frequency as you can see. What's wrong?

PollFrequency Entity is like that as following:

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

Poll Entity is also here as following:

 <?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;
}
}

Happy Coding to All !

  • 写回答

1条回答 默认 最新

  • doucongqian6644 2017-03-09 18:50
    关注

    I solved like that. If you need solution. that's it as following:

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

    And also I removed that part of PollFrequency as following:

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

    and everything that related to $polls.

    Happy Coding to All!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?