2013-02-11 3 views
0

Я хотел бы использовать метод get из класса User inherit из FOSUserBundle. но у меня есть ошибка:Неустранимая ошибка: вызов функции-члена на пользователе

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; 
    } 
} 

из моего контроллера:

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(), 
      )); 
} 

ответ

5

findBy() возвращает array, так что вы не можете вызвать его методы.

+0

Как я могу получить свои данные? я не понимаю структуру a возвращаемого массива: массив (1) { [0] => объект (Olr \ LoanBundle \ Entity \ User) # 552 (25) { ["id": protected] = > Int (6) [ "нОМ": защита] => строка (7) "Romeyer" [ "Prénom": защита] => строка (7) "Оливье" ... } } – Olivier

+0

Ну, это массив и ... ну, вы обращаетесь к нему, как и каждый массив http://php.net/language.types.array Итак, в вашем случае: '$ entity [0] -> getNom()' например , Или 'current ($ entity) -> getNom()'. Или 'list ($ entity) = $ entity; $ Entity-> getNom(); '. Всегда следите за пустым массивом. – KingCrunch

+1

Возвращаемое значение представляет собой массив объектов «Olr \ LoanBundle \ Entity \ User». Если вы знаете, что каждая запись, связанная с солью, будет уникальной, вы можете использовать 'findOneBy()' вместо этого, чтобы возвращать один объект, а не массив. – theunraveler

Смежные вопросы