Newbie question alert...
Ok so currently trying to Convert a user inputted value (a mass) from what ever Measure they wish to use, into Mg and then store it in the database.
Currently I have this:
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;
}
The "new Mass" function is something created by TriplePoint for converting from measure to measure.
https://github.com/triplepoint/php-units-of-measure/
Now if I explicitly set the $unit variable to 'grams' and 'kg' or what ever I choose It will do the conversion however I don't want to force the user to use just one measure obviously. So I thought creating a new variable $unit and the setters/getters to go with it, the text entered to the unit box would then be accepted in the newMass function. Instead I get this:
Unknown unit of measure ($unit)
Which I think means it is trying to use '$this->unit' as the variable and not the text in the text field.
Like I said I am really new to this stuff, sorry if I annoy anyone with this question.
Once I have this in place I wish to create an array for the user to choose from I think I can manage this myself though.
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,
));
}
This is the code for my form.
So I wish the value of $unit to be what ever is selected from the unit choice array. Which is why I am trying to load it by using:
public function setSize($size)
{
$unit = $this->unit;
Though I am thinking this is not the correct way to get the set variable from "public function setUnit"
Edit -
If I do :
$unit = 'g';
$this->size = $size = new Mass($size, $unit);
$this->size = $size->toUnit('mg');
return $this;
It works perfectly fine.
So really all Im asking is.. how do I access the variable set in the setUnit method?
The error message Unknown unit of measure ($unit)
is giving you enough information.
In this line: $this->size = $size = new Mass($size, $unit);
, the $unit
is not recognized.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.