Symfony2 - 错误:调用* --- findByOne数组中的非对象上的成员函数

时间:2014-03-21 22:51:32

标签: arrays symfony doctrine-orm

我被困住了,无法弄清楚为什么这不起作用。这很好用,我使用findOneById(2),但是当我使用findOneBy(array(...))时,我得到以下错误" FatalErrorException:错误:调用非成员函数getFirstName()对象在..."。

这是我正在遵循的教程:http://www.youtube.com/watch?v=seztFo5j7H4,代码完全相同。

请帮助!!!

实体

namespace General\LoginBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Users
 */
class Users
{
/**
 * @var string
 */
private $userName;

/**
 * @var string
 */
private $firstName;

/**
 * @var string
 */
private $password;

/**
 * @var integer
 */
private $userid;


/**
 * Set userName
 *
 * @param string $userName
 * @return Users
 */
public function setUserName($userName)
{
    $this->userName = $userName;

    return $this;
}

/**
 * Get userName
 *
 * @return string 
 */
public function getUserName()
{
    return $this->userName;
}

/**
 * Set firstName
 *
 * @param string $firstName
 * @return Users
 */
public function setFirstName($firstName)
{
    $this->firstName = $firstName;

    return $this;
}

/**
 * Get firstName
 *
 * @return string 
 */
public function getFirstName()
{
    return $this->firstName;
}

/**
 * Set password
 *
 * @param string $password
 * @return Users
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string 
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Get userid
 *
 * @return integer 
 */
public function getUserid()
{
    return $this->userid;
}
}

控制器

 public function indexAction()
{
    $username = 'username';
    $password = 'password';

    $em = $this->getDoctrine()->getManager();

    $repository = $em->getRepository('GeneralLoginBundle:Users');

    $user = $repository->findOneBy(array(
        'userName' => $username,
        'password' => $password,
    ));

    //        exit(\Doctrine\Common\Util\Debug::dump($user));

    return $this->render('GeneralLoginBundle:Default:index.html.twig', array(
        'name' => $user->getFirstName()
    ));
}

1 个答案:

答案 0 :(得分:2)

仔细检查$ username变量,确保它是您在数据库中使用的内容。