2014-03-05 116 views
0

新手问题警报...其他功能访问变量 - Symfony2的

确定,所以目前正试图转换从什么都测量他们希望使用,为镁,然后将其存储在用户输入值(质量)数据库。

目前我有这样的:

public function setUnit($unit) 
{ 
    $this->unit = $unit; 

    return $unit; 
} 
public function getUnit() 
{ 
    return $this->unit; 
} 
public function setSize($size) 
{ 
    $unit = $this->unit; 

    $this->size = $size = new Mass($size, $unit); 
    $this->size = $size->toUnit('mg'); 

    return $this; 
} 

“新大众”功能是通过TriplePoint创造了从测量,测量转换的东西。

https://github.com/triplepoint/php-units-of-measure/

现在,如果我明确设置$单元变量“克”和“公斤”或什么都我选择然而,它会进行转换,我不希望强迫用户只使用一个措施明显。所以我想创建一个新的变量$单位和设置器/ getters去与它,输入到单位框的文本然后将在newMass函数接受。相反,我得到这个:

Unknown unit of measure ($unit) 

我想这意味着它试图用“$这个 - >单元”作为变量,而不是在文本字段。

就像我说的,我对这个东西真的很陌生,对不起,如果我惹恼任何人这个问题。

一旦我有这个地方,我希望创建一个数组供用户选择,我想我可以自己管理这个。

EDIT--


public function buildForm(FormBuilderInterface $builder, array $options) 
{ 

    $builder 
     ->add('prod') 
     ->add('product_code') 
     ->add('size') 
     ->add('cost') 
     ->add('discount') 
     ->add('foodcat') 
     ->add('unit', 'choice', array(
'choices' => array(
    'g' => 'G', 
    'kg' => 'Kg', 
    'mg' => 'Mg', 
    'oz' => 'Oz', 
    'lb' => 'Lb', 
), 
'multiple' => false, 
));   
} 

这是我的形式的代码。

所以我希望$ unit的值是从单位选择数组中选择的值。这就是为什么我试图通过加载它:

public function setSize($size) 
{ 
    $unit = $this->unit; 

虽然我想这是不是得到“公共职能setUnit”

编辑组变量的正确方法 -

如果我做的:

 $unit = 'g'; 

    $this->size = $size = new Mass($size, $unit); 
    $this->size = $size->toUnit('mg'); 

    return $this; 

它完美的罚款。

因此,真的所有我问的是..我如何访问setUnit方法中设置的变量?

回答

1

错误消息Unknown unit of measure ($unit)正在为您提供足够的信息。

在这一行中:$this->size = $size = new Mass($size, $unit);$unit未被识别。

+0

错误来自此行: $ this-> size = $ size = new Mass($ size,$ unit); $单位变量没有从表单中设置。 - > toUnit('mg')完美无缺。 – Doug

+0

@Doug那么'$ unit'的值是多少? – xdazz

+0

编辑了我的问题。 – Doug