更新域模型,但TYPO3 6.2中的一个属性除外 [英] Update Domain Model except for one property in TYPO3 6.2

查看:84
本文介绍了更新域模型,但TYPO3 6.2中的一个属性除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我维护一个TYPO3扩展,用于管理后端中的前端用户.因此,我用自己的模型扩展了FrontendUserRepository.我的扩展程序提供CRUD操作,我在更新现有人员的密码时遇到问题.想法是仅在填写编辑表单中的密码字段时更新密码,否则(如果保留为空)旧密码值仍保留在数据库中.

I maintain a TYPO3 extension that manages frontend users in the backend. Therefore I extend the FrontendUserRepository with my own model. My extension provides CRUD operations and I have a problem with updating the password of existing persons. The idea is to only update the password, if the password field in the edit form is filled, otherwise (if it's left empty) the old password value remains in the database.

现在TYPO3 4.5一切正常,但是现在我升级到6.2后,在提交带有空密码字段的编辑表单时,空字符串将保存到数据库中...

Now everything was working fine with TYPO3 4.5, but now after I upgraded to 6.2, an empty string is saved to the database when submitting the edit form with an empty password field...

这是我的updateAction():

/**
 * action update
 *
 * @param \My\Vendor\Domain\Model\Person $person
 *
 * @return void
 */
public function updateAction(\My\Vendor\Domain\Model\Person $person) {
    // only hash and set password if not empty
    if ($person->getPassword() == '') {
        // if password was left empty, get current password from database
        $oldPerson = $this->personRepository->findByUid($person->getUid());
        $person->setPassword($oldPerson->getPassword()));
    } else {
        $person->setPassword(md5($person->getPassword()));
    }

    // save updated person to repository
    $this->personRepository->update($person);

    $this->flashMessageContainer->add('The person data was saved.');
    $this->redirect('edit', NULL, NULL, array('person' => $person));
}

有人知道,为什么$oldPerson->getPassword()不从数据库返回密码字段的当前值?还是有另一种方法可以在更新所有其他属性的同时跳过"域模型的属性?奇怪的是,它可以在TYPO3 4.5中工作...

Does anybody know, why $oldPerson->getPassword() does not return the current value for the password field from the database? Or is there another way to "skip" a property of a domain model while updating all other properties? The strange thing is that it worked in TYPO3 4.5 ...

推荐答案

有人知道为什么$oldPerson->getPassword()不从数据库返回密码字段的当前值吗?

Does anybody know, why $oldPerson->getPassword() does not return the current value for the password field from the database?

这是因为Extbase具有一种一级缓存:如果一次从持久性中获取对象,则不会在同一请求期间第二次从数据库中获取该对象,而是直接从内存中返回该对象.

This is because Extbase has a kind of 1st level cache: if the object was fetched form persistence once, it will not be fetched second time from database during same request, but returned directly form a memory.

因此,在您的情况下,第一次映射是从数据库中获取的,当发生属性映射时(内部Extbase操作将POST数据转换为\My\Vendor\Domain\Model\Person的实例).

So, in your case $person object is fetched from database 1st time, when a property mapping happens (internal Extbase operation which translates your POST data to instance of \My\Vendor\Domain\Model\Person).

调用$this->personRepository->findByUid($person->getUid());时,Extbase不会进行数据库查找,而是直接从内存中获取对象,从而导致$oldPerson === $person.并且由于$person已经更改了密码(通过POST数据),所以$oldPerson->getPassword()还会返回更改后的值.

When you call $this->personRepository->findByUid($person->getUid()); Extbase doesn't make a database lookup, but fetches object directly form memory resulting in $oldPerson === $person. And since $person has already changed password (via POST data), $oldPerson->getPassword() returns also the changed value.

对此的可能解决方案是获得清洁财产

Possible solution for this is to get clean property

如果模型的属性已更改但尚未保存,则几乎总是有 可以获取原始值(例如db中存在的原始值).您可以为此使用_getCleanProperty($propertyName)模型方法:

If the property of model was changed but not yet saved there is almost always a possibility to fetch the original value (e.g. one, that exists in db). You can use a _getCleanProperty($propertyName) method of model for this:

$person->setPassword($oldPerson->_getCleanProperty('password')));

可选,如果您甚至不希望对password字段进行数据库更新,甚至可以存储干净属性状态,这将告诉Extbase:不要更新db中的属性:

Optionally if you don't even want a db update of password field, you can even memorize clean property state, which will tell Extbase: do not update the property in db:

$person->_memorizePropertyCleanState('password');

注意:记住属性状态后,_getCleanProperty()将返回由set*()方法设置的值-而不是db中存在的原始值(如果您设置了不同的网络值) ).

Note: after you memorize property state, _getCleanProperty() will return the value, which was set by set*() method - not the original one present in db (in case you set a differnet value).

这篇关于更新域模型,但TYPO3 6.2中的一个属性除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆