2013-03-20 29 views
1

我想要按照 http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html 的示例並創建自定義數量類型。 introducting主義2自定義類型

public function convertToDatabaseValue($value, AbstractPlatform $platform) 
{ 
    return $value->toDecimal(); 
} 

我保存時收到以下錯誤,當

class Quantity extends Type 
{ 
const QUANTITY = 'quantity'; // modify to match your type name 

public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform) 
{ 
    return 'decimal(10,2)'; 
} 

public function getName() 
{ 
    return self::QUANTITY; 
} 

}

但是:

FatalErrorException: Error: Call to a member function toDecimal() on a non-object in ...\Types\Quantity.php line 26

+0

我懷疑「return $ value-> toDecimal();」是出於演示的原因 – mentalic 2013-03-26 10:14:28

回答

1

$value你當數量類型被定義爲一切工作正常得到是stringnull。你不能打電話給toDecimal。嘗試:

public function convertToDatabaseValue($value, AbstractPlatform $platform) { 
    return $value === null ? null : (float) $value; 
}