1

我最近学习了 Symfony 2,所以我还是个初学者。为了好玩,我正在尝试制作一个简单的社交网络网站。

所以我有帖子和主题标签。我在这两个实体(帖子/主题标签)之间创建了一个多对多关系

我尝试使用 getRepository 从我的数据库中获取主题标签。我使用了“findByHashtagName”方法。当我得到对象时,我试图获取主题标签的 id,但是当我调用方法 getId() 时,我有这个警告:

Call to a member function * on a non-object in * line *

当我得到对象时,我检查它是否不为空。所以我真的不明白为什么它不起作用。

标签实体:

<?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 实体中。

4

2 回答 2

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
}
于 2013-11-14T06:21:16.840 回答
2

问题是您的存储库方法返回一个array. 在您的存储库中, getResult()您需要使用getSingleResult().

于 2013-11-14T06:25:35.527 回答