类 '...\Entity\User' 没有方法 'getGenderList'
Posted
技术标签:
【中文标题】类 \'...\\Entity\\User\' 没有方法 \'getGenderList\'【英文标题】:Class '...\Entity\User' does not have a method 'getGenderList'类 '...\Entity\User' 没有方法 'getGenderList' 【发布时间】:2015-11-22 20:46:30 【问题描述】:我正在使用Sonata Admin bundle 作为我网站的后端。
我的数据库中的用户表如下所示:
id (PK)
username
email
password
salt
last_login
created_at
company_id (FK)
我的用户实体如下所示:
<?php
namespace MyBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
//use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user", indexes=@ORM\Index(name="fk_user_company1_idx", columns="company_id"))
* @ORM\Entity
*/
class User extends BaseUser
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
protected $createdAt;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var \MyBundle\Entity\Company
*
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\Company")
* @ORM\JoinColumns(
* @ORM\JoinColumn(name="company_id", referencedColumnName="id")
* )
*/
protected $company;
public function __construct()
parent::__construct();
// your own logic
/**
* Set username
*
* @param string $username
* @return User
*/
public function setUsername($username)
$this->username = $username;
return $this;
/**
* Get username
*
* @return string
*/
public function getUsername()
return $this->username;
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
$this->email = $email;
return $this;
/**
* Get email
*
* @return string
*/
public function getEmail()
return $this->email;
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
$this->password = $password;
return $this;
/**
* Get password
*
* @return string
*/
public function getPassword()
return $this->password;
/**
* Set salt
*
* @param string $salt
* @return User
*/
public function setSalt($salt)
$this->salt = $salt;
return $this;
/**
* Get salt
*
* @return string
*/
public function getSalt()
return $this->salt;
/**
* Get id
*
* @return integer
*/
public function getId()
return $this->id;
/**
* Set company
*
* @param \MyBundle\Entity\Company $company
* @return User
*/
public function setCompany(\MyBundle\Entity\Company $company = null)
$this->company = $company;
return $this;
/**
* Get company
*
* @return \MyBundle\Entity\Company
*/
public function getCompany()
return $this->company;
现在当我转到“编辑帐户”页面时,我收到以下错误:
Warning: call_user_func() expects parameter 1 to be a valid callback, class 'MyBundle\Entity\User' does not have a method 'getGenderList'
我的数据库中的用户表中没有性别。我该如何解决这个问题?
更新:
当我将use FOS\UserBundle\Entity\User as BaseUser;
更改为use Sonata\UserBundle\Entity\BaseUser as BaseUser;
。然后我收到以下错误:
执行 'SELECT t0.username AS username1, t0.username_canonical AS username_canonical2, t0.email AS email3, t0.email_canonical AS email_canonical4, t0.enabled AS enabled5, t0.salt AS salt6, t0.password AS 时发生异常password7, t0.last_login AS last_login8, t0.locked AS locked9, t0.expired AS expired10, t0.expires_at AS expires_at11, t0.confirmation_token AS Confirm_token12, t0.password_requested_at AS password_requested_at13, t0.roles AS roles14, t0.credentials_expired AS credentials_expired t0.credentials_expire_at AS credentials_expire_at16, t0.created_at AS created_at17, t0.updated_at AS updated_at18, t0.date_of_birth AS date_of_birth19, t0.firstname AS firstname20, t0.lastname AS lastname21, t0.website AS website22, t0.biography AS biography23, t0.性别 AS gender24, t0.locale AS locale25, t0.timezone AS timezone26, t0.phone AS phone27, t0.facebook_uid AS facebook_uid28, t0.facebook_name AS facebook_name29, t0.facebook_data AS facebook_data30, t0.twitt er_uid AS twitter_uid31, t0.twitter_name AS twitter_name32, t0.twitter_data AS twitter_data33, t0.gplus_uid AS gplus_uid34, t0.gplus_name AS gplus_name35, t0.gplus_data AS gplus_data36, t0.token AS token37, t0.two_step_code AS two_step_code38, t0. id39, t0.company_id AS company_id40 FROM user t0 WHERE t0.id = ? LIMIT 1' 带参数1:
SQLSTATE[42S22]:找不到列:1054 未知列 't0.updated_at' 在“字段列表”中
我该如何解决这个问题?
【问题讨论】:
你必须在你的类中实现缺失的方法。这就是错误信息告诉你的全部内容。 遇到了同样的问题,在收到这个 sql 错误后,我不得不删除与此相关的表(fos_user、fos_group 和 fos_user_group)、所有迁移文件并重新创建它们。更改现有表不起作用。 【参考方案1】:您的 User 类从 FOSUserBundle 扩展,而不是 SonataUserBundle。你必须改变它:
//use FOS\UserBundle\Entity\User as BaseUser;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
查看https://github.com/sonata-project/SonataUserBundle/issues/459
编辑:如果你不想使用这个类(里面有很多无用的字段),那么你必须为你的 User 实体创建自己的管理类。这个页面有一个完整的指南:http://m2mdas.github.io/blog/2013/11/18/integrate-fosuserbundle-and-sonatauserbundle-easily/
更新后更新: 更改实体扩展的类后,您必须更新数据库架构以匹配您的模型:
php app/console doctrine:schema:update --force
【讨论】:
我已经更新了我的主题,有没有办法在不编写自己的管理类的情况下绕过它? 我已经更新了我的答案。您要么必须扩展适当的类,要么编写自己的管理类。以上是关于类 '...\Entity\User' 没有方法 'getGenderList'的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.InstantiationException: entity.User 实例化异常解决
用户 App Entity User 没有用户提供程序 您的用户提供程序的 supportsClass() 方法不应该为这个类名返回 true 吗?
Class ..Entity\User 不是有效的实体或映射的超类
symfony 3 [语义错误]类AppBundle Entity User中的注释@Doctrine ORM Mapping Model不存在,或者无法自动加载
Sonata-Project 的 Google Authenticator:来自 Application\Sonata\UserBundle\Entity\User 的未定义方法“getTwoStep