0
我想從繼承自FOSUserBundle的User類使用get方法。但我有一個錯誤:致命錯誤:調用用戶的成員函數
Fatal error: Call to a member function getNom() on a non-object.
我的用戶類別:
<?php
namespace Olr\LoanBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*
*/
protected $nom;
/**
* @ORM\Column(type="string", length=255)
*
*/
protected $prenom;
/**
* @ORM\Column(type="string", length=50)
*
*/
protected $adresse;
/**
* @ORM\Column(type="string", length=50)
*
*/
protected $adresse2;
/**
* @ORM\Column(type="string", length=5)
*
*/
protected $cp;
/**
* @ORM\Column(type="string", length=50)
*
*/
protected $ville;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*/
public function setNom($nom)
{
$this->nom = $nom;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set prenom
*
* @param string $prenom
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
}
/**
* Get prenom
*
* @return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* Set adresse
*
* @param string $adresse
*/
public function setAdresse($adresse)
{
$this->adresse = $adresse;
}
/**
* Get adresse
*
* @return string
*/
public function getAdresse()
{
return $this->adresse;
}
/**
* Set adresse2
*
* @param string $adresse2
*/
public function setAdresse2($adresse2)
{
$this->adresse2 = $adresse2;
}
/**
* Get adresse2
*
* @return string
*/
public function getAdresse2()
{
return $this->adresse2;
}
/**
* Set cp
*
* @param string $cp
*/
public function setCp($cp)
{
$this->cp = $cp;
}
/**
* Get cp
*
* @return string
*/
public function getCp()
{
return $this->cp;
}
/**
* Set ville
*
* @param string $ville
*/
public function setVille($ville)
{
$this->ville = $ville;
}
/**
* Get ville
*
* @return string
*/
public function getVille()
{
return $this->ville;
}
}
從我CONTROLER:
public function invitationAction($token)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('OlrLoanBundle:User')->findBy(array('salt'=>$token));
return $this->render('OlrLoanBundle:Tribu:invitation.html.twig',
array('nom'=>$entity->getNom(),
'prenom'=>$entity->getPrenom(),
));
}
我怎樣才能得到我的數據?我不理解返回數組的結構: array(1){ [0] => object(Olr \ LoanBundle \ Entity \ User)#552(25){ [「id」:protected] = > INT(6) [ 「NOM」:保護] => 串(7) 「候梅葉」 [ 「prenom」:保護] => 串(7) 「奧力」 ... } } – Olivier 2013-02-11 22:13:51
好吧,它是一個數組,並且......你可以像訪問每個數組一樣訪問它http://php.net/language.types.array所以在你的情況下:'$ entity [0] - > getNom()'例如。或'current($ entity) - > getNom()'。或者'list($ entity)= $ entity; $實體 - > getNom();'。始終注意空陣列。 – KingCrunch 2013-02-11 22:27:26
返回值是一個「Olr \ LoanBundle \ Entity \ User」對象的數組。如果你知道與salt相關的每條記錄都是唯一的,你可以使用'findOneBy()'來返回一個單獨的對象而不是一個數組。 – theunraveler 2013-02-11 22:29:57