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
    打赏 评论
  • dongye8110 2011-11-30 07:00

    This is my fault. Lets me explain why.

    I am using Zend 1.1. + Doctrine 2.1 in my project. Doctrine is using Zend_Cache as cache adapter. I forgot to disable caching in development environment.

    My first version of User model consisted of 4 fields: id, username, password, role and status. After some time I added other fields. Doctrine cached only 4 fields in model metadata. 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).

    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.

    打赏 评论

相关推荐 更多相似问题