dongle7882 2011-11-29 20:30
浏览 38
已采纳

Doctrine2不会选择所有实体字段

I have Doctrine 2.1 entity:

/**
 * @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:

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:

UPDATE user SET username = ? WHERE id = ?

How can I fetch all fields of my entity? Why is it so?

  • 写回答

2条回答 默认 最新

  • doushupu2521 2011-11-29 21:29
    关注

    Doctrine 2 fetches all fields by default.

    Most likely the issue is one of the following:

    • I'm not sure if merge is the correct function to use. I'm pretty sure it's 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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了