简体   繁体   English

Doctrine2不会选择所有实体字段

[英]Doctrine2 does not select all entity fields

I have Doctrine 2.1 entity: 我有教义2.1实体:

/**
 * @Entity(repositoryClass="Varlab\Model\Repository\UserRepository")
 * @Table(name="user")
 */
class User 
{
/**
 * @Id @Column(name="id",type="integer")
 * @GeneratedValue(strategy="AUTO")
 * 
 * @var integer 
 */
protected $_id;

/**
 * @Column(name="username",type="string",nullable=false,length=50,unique=true)
 * @var string 
 */
protected $_username;

/**
 * User password
 * 
 * @Column(name="passwd",type="string",nullable=false,length=50)
 * @var string 
 */
protected $_password;

/**
 * @ManyToOne(targetEntity="Role")
 * @JoinColumn(name="role_id", referencedColumnName="id")
 * 
 * @var Role 
 */
protected $_role;

/**
 * @ManyToOne(targetEntity="UserStatus")
 * @JoinColumn(name="status_id", referencedColumnName="id")
 * 
 * @var UserStatus 
 */
protected $_status;

/**
 * @Column(name="address",type="string",nullable=false,length=100)
 * @var string
 */
protected $_address;

/**
 * @Column(name="description",type="text",nullable=true)
 * @var string
 */
protected $_description;

/**
 * @Column(name="firstname",type="string",nullable=false,length=50)
 * @var string
 */
protected $_firstname;

/**
 * @Column(name="lastname",type="string",nullable=false,length=50)
 * @var string
 */
protected $_lastname;

/**
 * @Column(name="patronymic",type="string",nullable=false,length=50)
 * @var string
 */
protected $_patronymic;

/**
 * @Column(name="phone",type="string",nullable=false,length=20)
 * @var string
 */
protected $_phone;

// ... some get/set methods here

}

I try to update user entity using this code: 我尝试使用以下代码更新用户实体:

$item = $em->find('User', 1);
$item->setFirstname('some name');
$em->merge($item);
$em->flush();

But nothing is happened with firstname field in database. 但是数据库中的名字字段什么也没有发生。 I look at Doctrine SQL log and see that doctrine does not fetch all fields: 我查看了Doctrine SQL日志,并发现该学说并未获取所有字段:

SELECT t0.id AS id1, t0.username AS username2, t0.passwd AS passwd3, t0.role_id AS role_id4, t0.status_id AS status_id5 FROM user t0 WHERE t0.id = ?

If i try to change username, it is all right and field is changed in database. 如果我尝试更改用户名,那么可以,并且数据库中的字段已更改。

$item = $em->find('User', 1);
$item->setUsername('some user');
$em->merge($item);
$em->flush();

Update SQL from Doctrine log: 从Doctrine日志中更新SQL:

UPDATE user SET username = ? WHERE id = ?

How can I fetch all fields of my entity? 如何获取实体的所有字段? Why is it so? 为什么会这样呢?

Doctrine 2 fetches all fields by default. 默认情况下,Doctrine 2会提取所有字段

Most likely the issue is one of the following: 该问题很可能是以下原因之一:

  • I'm not sure if merge is the correct function to use. 我不确定merge是否是要使用的正确函数。 I'm pretty sure it's persist 我很确定它会persist
  • Your proxies might not be up to date. 您的代理可能不是最新的。 Check them. 检查他们。
  • Your entity metadata might not be up to date. 您的实体元数据可能不是最新的。 If you cache your metadata, clear the cache and try again. 如果您缓存元数据,请清除缓存,然后重试。
  • There's an error in some of your mappings. 您的某些映射中有错误。 Run orm:validate-schema from the command line tool 从命令行工具运行orm:validate-schema

This is my fault. 这是我的错。 Lets me explain why. 让我解释一下原因。

I am using Zend 1.1. 我正在使用Zend 1.1。 + Doctrine 2.1 in my project. +我的项目中的教义2.1。 Doctrine is using Zend_Cache as cache adapter. Doctrine正在使用Zend_Cache作为缓存适配器。 I forgot to disable caching in development environment. 我忘了禁用开发环境中的缓存。

My first version of User model consisted of 4 fields: id, username, password, role and status. 我的第一个用户模型版本包含4个字段:ID,用户名,密码,角色和状态。 After some time I added other fields. 一段时间后,我添加了其他字段。 Doctrine cached only 4 fields in model metadata. 原则在模型元数据中仅缓存了4个字段。 Zend_Cache set www-data user owner for cache files (I can not clear metadata cache using doctrine-cli and (it is strange) there was no error). Zend_Cache为缓存文件设置了www-data用户所有者(我无法使用doctrine-cli清除元数据缓存,(很奇怪)没有错误)。

How did i understand that problem was in metadata cache? 我如何理解该问题出在元数据缓存中? Simple - I added metadata output like this: 简单-我添加了元数据输出,如下所示:

$item = $em->find('User', 1);
$item->setUsername('some user');
print_r($em->getClassMetadata($item)); // this is it!
$em->merge($item);
$em->flush();

Thanks to everybody who help me with this problem. 感谢所有帮助我解决此问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM