我最近学习了Symfony 2,所以我还是初学者。 为了好玩,我正在尝试建立一个简单的社交网站。
所以我有帖子和主题标签。我在这两个实体(posts / hashtags)之间创建了一个关系ManyToMany
我试图通过使用getRepository从我的数据库中获取一个#标签。 我使用了方法“findByHashtagName”。 当我得到对象时,我试图获取标签的id,但是当我调用方法getId()时,我有这个警告:
Call to a member function * on a non-object in * line *
当我得到对象时,我会检查它是否为空。 所以我真的不明白为什么它不起作用。
Hashtag实体:
<?php
namespace Moodress\Bundle\HashtagBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Hashtag
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Moodress\Bundle\HashtagBundle\Entity\HashtagRepository")
*/
class Hashtag
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="hashtag_name", type="string", length=255)
*/
private $hashtagName;
/**
* @ORM\ManyToMany(targetEntity="Moodress\Bundle\PosteBundle\Entity\Poste", cascade={"persist"})
*/
private $postes;
public function __construct()
{
$this->postes = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set hashtagName
*
* @param string $hashtagName
* @return Hashtag
*/
public function setHashtagName($hashtagName)
{
$this->hashtagName = $hashtagName;
return $this;
}
/**
* Get hashtagName
*
* @return string
*/
public function getHashtagName()
{
return $this->hashtagName;
}
/**
* Add postes
*
* @param \Moodress\Bundle\PosteBundle\Entity\Poste $postes
* @return Hashtag
*/
public function addPoste(\Moodress\Bundle\PosteBundle\Entity\Poste $postes)
{
$this->postes[] = $postes;
return $this;
}
/**
* Remove postes
*
* @param \Moodress\Bundle\PosteBundle\Entity\Poste $postes
*/
public function removePoste(\Moodress\Bundle\PosteBundle\Entity\Poste $postes)
{
$this->postes->removeElement($postes);
}
/**
* Get postes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPostes()
{
return $this->postes;
}
}
发布文件:
<?php
namespace Moodress\Bundle\PosteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Poste
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Moodress\Bundle\PosteBundle\Entity\PosteRepository")
*/
class Poste
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="id_user", type="integer")
*/
private $idUser;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var integer
*
* @ORM\Column(name="nb_comments", type="integer")
*/
private $nbComments;
/**
* @var integer
*
* @ORM\Column(name="nb_likes", type="integer")
*/
private $nbLikes;
/**
* @var \DateTime
*
* @ORM\Column(name="date_creation", type="datetime")
*/
private $dateCreation;
/**
* @var boolean
*
* @ORM\Column(name="is_there_fashtag", type="boolean")
*/
private $isThereFashtag;
/**
* @var array
*
* @ORM\Column(name="array_url_pictures", type="array")
*/
private $arrayUrlPicturesMaxSize;
/**
* Constructor
*/
public function __construct()
{
$this->dateCreation = new \Datetime();
$this->arrayUrlPicturesMaxSize = array();
$this->nbComments = 0;
$this->nbLikes = 0;
$this->isThereFashtag = false;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set idUser
*
* @param integer $idUser
* @return Poste
*/
public function setIdUser($idUser)
{
$this->idUser = $idUser;
return $this;
}
/**
* Get idUser
*
* @return integer
*/
public function getIdUser()
{
return $this->idUser;
}
/**
* Set description
*
* @param string $description
* @return Poste
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set nbComments
*
* @param integer $nbComments
* @return Poste
*/
public function setNbComments($nbComments)
{
$this->nbComments = $nbComments;
return $this;
}
/**
* Get nbComments
*
* @return integer
*/
public function getNbComments()
{
return $this->nbComments;
}
/**
* Set nbLikes
*
* @param integer $nbLikes
* @return Poste
*/
public function setNbLikes($nbLikes)
{
$this->nbLikes = $nbLikes;
return $this;
}
/**
* Get nbLikes
*
* @return integer
*/
public function getNbLikes()
{
return $this->nbLikes;
}
/**
* Set dateCreation
*
* @param \DateTime $dateCreation
* @return Poste
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* @return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set isThereFashtag
*
* @param boolean $isThereFashtag
* @return Post
*/
public function setIsThereFashtag($isThereFashtag)
{
$this->isThereFashtag = $isThereFashtag;
return $this;
}
/**
* Get isThereFashtag
*
* @return boolean
*/
public function getIsThereFashtag()
{
return $this->isThereFashtag;
}
public function addPicture($picture, $posteId)
{
$directory = "/Applications/MAMP/htdocs/moodress-website/Symfony/web/images/postes_images";
$picture->move($directory, "post_".$posteId."_pic_".count($this->arrayUrlPicturesMaxSize).".".$picture->getClientOriginalExtension());
$this->arrayUrlPicturesMaxSize[] = "images/postes_images/poste_".$posteId."_pic_".count($this->arrayUrlPicturesMaxSize).".".$picture->getClientOriginalExtension();
}
/**
* Set arrayUrlPicturesMaxSize
*
* @param array $arrayUrlPicturesMaxSize
* @return Poste
*/
public function setArrayUrlPicturesMaxSize($arrayUrlPicturesMaxSize)
{
$this->arrayUrlPicturesMaxSize = $arrayUrlPicturesMaxSize;
return $this;
}
/**
* Get arrayUrlPicturesMaxSize
*
* @return array
*/
public function getArrayUrlPicturesMaxSize()
{
return $this->arrayUrlPicturesMaxSize;
}
}
代码失败:
$em = $this->getDoctrine()->getManager();
$hashtag1 = $em->getRepository('MoodressHashtagBundle:Hashtag')->findByHashtagName("bcbg");
if (!$hashtag1) {
throw $this->createNotFoundException(
'None hashtag found');
}
else {
$hashtag1->getHashtagName();
// This call make the exception : Call to a member function * on a non-object in * line *
}
我做错了什么?
我检查它是否为空...它不是,因为我尝试调用getHashtagName ...如您所见,此方法在Hashtag实体中。
答案 0 :(得分:3)
使用此:
$hashtag1 = $em->getRepository('MoodressHashtagBundle:Hashtag')->findOneByHashtagName("bcbg");
echo $hashtag1->getHashtagName();
否则,如果您使用findByHashtagName,您将获得一系列结果:
$hashtags = $em->getRepository('MoodressHashtagBundle:Hashtag')->findByHashtagName("bcbg");
foreach($hashtags as $hashtag1) {
echo $hashtag1->getHashtagName();
//something else you want to do
}
答案 1 :(得分:2)
问题是您的存储库方法返回array
。在您的存储库中,您需要使用getResult()
而不是getSingleResult()
。