Reputation: 2691
I recently learnt Symfony 2, so i'm still a beginner. For fun, i'm trying to make a simple social network website.
So I have posts, and hashtags. I create a relation ManyToMany between this two entities (posts/hashtags)
I tried to get a hashtag from my database, by using getRepository. I used the method "findByHashtagName". When i got the object, i tried to get the id of the hashtag, but when I called the method getId(), I have this warning :
Call to a member function * on a non-object in * line *
When I get the object, I check if it's not null. So I really don't understand why it doesn't work.
Hashtag entity :
<?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;
}
}
Post file :
<?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;
}
}
The code failing :
$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 *
}
What do I do wrong ?
I check if it's null... It's not, because I try to call getHashtagName... As you see, this methode is in Hashtag entity.
Upvotes: 1
Views: 12575
Reputation: 2643
The problem is that your repository method is returning an array
. In your repository, instead of getResult()
you need to use getSingleResult()
.
Upvotes: 2
Reputation: 4293
Use this:
$hashtag1 = $em->getRepository('MoodressHashtagBundle:Hashtag')->findOneByHashtagName("bcbg");
echo $hashtag1->getHashtagName();
Else if you use findByHashtagName you will get an array of results:
$hashtags = $em->getRepository('MoodressHashtagBundle:Hashtag')->findByHashtagName("bcbg");
foreach($hashtags as $hashtag1) {
echo $hashtag1->getHashtagName();
//something else you want to do
}
Upvotes: 3