本文介绍了SIlverStripe-没有数据写入beforeWrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题相关,是扩展了DataObject >方法不会在数据库上写入特定的属性值.详细信息:

Related to this issue, a DataObject extended with onBeforeWrite method doesn't write a specific property value on DB. In detail:

DataObject:

DataObject:

[...] 
/**
 * Classe Prodotto
 */
class Prodotto extends DataObject
{
// Dichiarazione Proprietà
private static $db = [
    [...] 
    'PrezzoIva' => 'Currency',
    [...] 

onBeforeWrite方法:

/**
     * Metodo gestione azioni salvataggio
     * Calcolo automatico prezzo lordo e scontato
     * Setter
     * @return void
     */
    public function onBeforeWrite()
    {
        // Controllo Record
        if (!$this->isInDb()) {
            $this->setPrezzoIva();
        }
        if ((!isset($this->record['PrezzoIva'])) || ($this->record['PrezzoIva'] == 0)) {
            $this->setPrezzoIva();
        }

        parent::onBeforeWrite();
    }

上述方法调用的方法:

/**
     * Metodo calcolo prezzo lordo IVA
     * Setter
     * @return void
     */
    public function setPrezzoIva()
    {
        // Controllo IVA
        if ((isset($this->PrezzoIva)) && ($this->PrezzoIva == 0)) {
            $prezzoIva = ($this->PrezzoUnitario * $this->Iva) + $this->PrezzoUnitario;

            // Salvataggio IVA
            $this->PrezzoIva = $prezzoIva;
        }
    }

没有引发异常.基本上,在第一个write()以及其他保存中,PrezzoIva都不会被更新(保留默认值).经过几次DataObject编辑后,这是我的数据库的摘录:

There's no exception thrown. Basically, both on the first write() as well as in other saves, PrezzoIva is not being updated (it keeps the default one).Here an extract to my DB after few DataObject edits:

目前,我不知道是什么原因造成的.任何帮助将不胜感激.

For now, I not figured out what causing this. A help of any kind will be appreciated.

提前感谢大家.

推荐答案

您必须在SilverStripe中观看设置器. SilverStripe使用__set函数.

You have to watch the setters in SilverStripe. SilverStripe uses the __set function.

调用$this->PrezzoIva时,它将搜索称为setPrezzoIva的方法.如果找不到具有该名称的方法,它将调用方法setField.实际上,这将根据您的需要设置该字段.

When you call $this->PrezzoIva it will search for a method called setPrezzoIva. If it cannot find a method with that name, it will call the method setField. Which will actually set the field like you want.

所以您遇到的问题是因为您的方法称为setPrezzoIva.而不是设置值,而是执行您的方法.

So the problem you're experiencing is because your method is called setPrezzoIva. Instead of setting the value, it is excecuting your method.

要解决此问题,请更改

$this->PrezzoIva = $prezzoIva;

$this->setField('PrezzoIva', $prezzoIva);

在旁注中,我认为罗比是正确的,你的条件太严格了.

On a sidenote, I think Robbie is right, your conditions are too strict.

这篇关于SIlverStripe-没有数据写入beforeWrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 20:31