Hi, i'm having some trouble populating my fieldset/collection from an object.

Simply, i have an Animal object that has an array of 1->N breed objects.
Animal::getBreeding returns this array.

The form AnimalBreeding defines the following:

/ public function init (){
        $this->setName('animal-breeding');
        $this->setLabel('Animal Breeding');
        $this->setAttribute('method', 'post');
        $this->setObject(new \Application\Model\Animal\Animal());
        
        $this->add(array(
                        'name' => 'animalId',
                        'attributes' => array(
                                        'value' => ''
                        ),
                        'type' => 'hidden'
        ));
        
        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'breeding',
            'options' => array(
                'label' => 'Select the breeding of the animal',
                'count' => 1,
                // 'template_placeholder' => '__placeholder__',
                'should_create_template' => true,
                'allow_add' => true,
                'target_element' => array(
                    'type' => 'Animal\Breed\BreedListFieldset'
                )
            )
        ));
/

... with Animal\Breed\BreedListFieldset being defined as:
/
class BreedListFieldset extends Fieldset implements
InputFilterProviderInterface
{
    /**
     * 
     * @param \Application\Model\Animal\Breed\Mapper $breedMapper
     */
    public function __construct (\Application\Model\Animal\Breed\Mapper
$breedMapper){
        $this->setBreedMapper($breedMapper);
        $this->setObject(new \Application\Model\Animal\Breed\Breed());
        parent::__construct('breed');
    }

    public function init (){   
       $breedList = array();
       $breeds = $this->getBreedMapper()->fetchAll();
       foreach($breeds as $breed){
           $breedList[$breed->getId()] = $breed->getName();
       }
       
       $this->add(array(
                'name' => 'breedId',
                'type' => 'Zend\Form\Element\Select',
                'options' => array(
                                'label' => 'Animal Breed:',
                                'value_options' => $breedList
                )
       ));
        
        parent::init();
    }
}/

A loaded animal object has the method Animal::getBreeding() which returns an
array of breed objects, a very simple class of just an id and name. ie:
/array{
    'breed [object]' => array { 
        'id' => '7',
        'name' => 'Border Collie'},
    'breed [object]' => array{
        'id' => '8',
        'name' => 'Golden Retriever'}
}/
... which indicates the animal is a border collie cross golden retriever. An
animal can have 1 -> N breeds. What i am trying to achieve is for the
collection to load with N amount of select elements, pre-selected to the
animals breed. In the above instance i would expect two select elements, one
pre-selected to border collie, the other pre-selected to golden retriever.

I have followed the forms collection tutorial and cannot see where i am
going wrong, but the form does not seem to be pulling the breed information
from the Animal object.

Any pointers would be greatly appreciated.





--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/zf2-populating-fieldset-collection-tp4660890.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com


Reply via email to