today i started reading myself into the features of Zend\Form. I found a great tutorial from Michael Gallego in which he explains how to use some new cool features.
The example works fine so far if we're handling 1-1 Relationships. Doctrine covers them fine.
What i want to do is
- Instead of having textarea for the related value, I'd like a select box
- The select box should have valid options, depending on what's at the database
- For editing purpose later, the currently selected value needs to be selected
- Doctrine should not add new rows to the One-Table
As you can see at my github sources i made use of the example in the tutorial, but shortened it to "Product" and "Brand". Brands - in my example - is a DB-Table with predefined Brands (Nike, Adidas, Puma, whatever) and when you create a new Product from the form you get those Brands as a select menu.
Right now, the way i add the options isn't working. I know i can manually set the options with an array like
$form->get('product')->get('brand')->setAttribute('options', array('Nike'=>'1', 'Adidas'=>'2', etc);
But i strongly assume that there is a more automated way to do this. I simply do not understand all this Hydrator classes provided with Zend.
The Problem is, even if i manually define the array as described above, the mapping of Product and Brand is not working correctly. The dump of $product right now looks like this
object(Application\Entity\Product)[210]
protected 'id' => null
protected 'name' => string 'asdasd' (length=6)
protected 'price' => string '123123' (length=6)
protected 'brand' =>
object(Application\Entity\Brand)[215]
protected 'id' => null
protected 'name' => string '1' (length=1)
Obviously the brand is mapped completely wrong (for what i want to achieve, zend probably sees this as right, since the VALUE of my select is 1).
Question How do i tell my Form to map the select-value to the mapped object ID? Though maybe the way i set up my product-model is wrong in that case.
Any help will be greatly appreciated :)
setValueOptions. How do you link an associated entity with a select dropdown? E.g.: a user can have multiple clients (ManyToMany), so when I edit a user I need a<select>dropdown of client names. - hohner