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!

    点赞 打赏 评论

相关推荐 更多相似问题