2012-12-06 47 views
2

我有一個名爲Plato的類,它擴展了Producto witch包含deatribute precio。設置器定義如下:在symfony中插入 double值與doctrine

public function setPrecio(\double $precio) 
{ 
    $this->precio = $precio; 

    return $this; 
} 

而且我想一個新的元素添加到數據庫與:

$plato = new Plato; 
$em = $this->getEntityManager(); 
// I have tryed this three ways to insert 
$plato->SetPrecio(doubleval($precio)); 
$plato->SetPrecio((double) 2); 
$plato->SetPrecio(2.0); 

$em->flush(); 

它給了我下面的錯誤信息:

Catchable Fatal Error: Argument 1 passed to Servinow\EntitiesBundle\Entity\Producto::setPrecio() must be an instance of double, double given, called in /Users/luis/git/servinowServer-luis/src/Servinow/EntitiesBundle/Entity/PlatoRepository.php on line 41 and defined in /Users/luis/git/servinowServer-luis/src/Servinow/EntitiesBundle/Entity/Producto.php line 169

+0

我瞎搞試圖想辦法幫助,並且我在php'echo gettype(9.4)中發現了這個行爲; var_dump(9.4);'現在我的頭腦受傷了。 – Dale

回答

3

修改setPrecio()功能

public function setPrecio($precio) 
{ 
    $this->precio = $precio; 

    return $this; 
} 

你可以使用類似的東西

gettype($precio)[...]; 
if(is_numeric($precio))[...]; 

類型控制,先設置成$this


記住

Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.

+0

+1你也可以在裏面放一個'if(is_numeric)'來確保 – Dale

+0

@Dale在你寫評論時正在更新 – DonCallisto

+0

非常感謝。 – Cebado

相關問題