2014-12-11 43 views
1

我的设置涉及音乐家和唱片与Zend框架形式收集结合形式概念的基本证据。绑定使用窗体集合到一个实体表单中ZF2

这里是音乐家类别:

<?php 

namespace Application\Entity; 


class Musician { 

protected $name; 

protected $albums; 

public function setName($name) 
{ 
    $this->name = $name; 
    return $this; 
} 
public function getName() 
{ 
    return $this->name; 
} 
public function setAlbums($album) 
{ 
    $this->album = $album; 
    return $this; 
} 
public function getAlbums() 
{ 
    return $this->albums; 
} 

这里是相册类别:

<?php 

namespace Application\Entity; 


class Album { 

protected $name; 

protected $releaseYear; 

public function setName($name) 
{ 
    $this->name = $name; 
    return $this; 
} 
public function getName() 
{ 
    return $this->name; 

} 
public function setReleaseYear($releaseYear) 
{ 
    $this->releaseYear = $releaseYear; 
    return $this; 
} 
public function getReleaseYear() 
{ 
    return $this->releaseYear; 

} 

}

相册字段集:

相册字段集:

这里是音乐家形式

<?php 

namespace Application\Form\Music; 

use Application\Entity\Album; 
use Zend\Form\Form; 
use Zend\Form\Element\Collection; 
use Zend\Stdlib\Hydrator\ClassMethods; 
use Zend\Stdlib\Hydrator\ObjectProperty; 
use Zend\Validator; 
use Zend\Form\Element; 
use Application\Form\Music\AlbumFieldset; 
use Zend\ServiceManager\ServiceLocatorAwareInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use Zend\InputFilter\InputFilterProviderInterface; 

class MusicianForm extends Form implements InputFilterProviderInterface, ServiceLocatorAwareInterface 
{ 


    public function __construct() 
    { 

     parent::__construct(''); 

    } 

    public function init() 
    { 

    } 



    public function setMusician($musician) { 

     $this->setHydrator(new ClassMethods()); 
     $this->add(array(
      'type' => 'Text', 
      'name' => 'name', 
      'options' => [ 
      ] 
     )); 




     $this->buildFields(); 
     $this->bind($musician); 

    } 


    public function buildFields() { 


     $fs = new AlbumFieldSet(); 
     $fs->setHydrator(new ObjectProperty()); 
     $fs->setObject(new Album()); 

     $this->add(array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'albums', 
      'options' => array(
       'label' => 'Form Values', 
       'count' => 2, 
       'allow_add' => false, 
       'allow_remove' => false, 
       'should_create_template' => false, 
       'target_element' => $fs, 
       'use_as_base_fieldset' => true, 
      ), 
     )); 

    } 



    /** 
    * Should return an array specification compatible with 
    * {@link Zend\InputFilter\Factory::createInputFilter()}. 
    * 
    * @return array 
    */ 
    public function getInputFilterSpecification() 
    { 
     return [ 
      'name' => array(
       'required' => true, 
       'validators' => array(
       ) 
      ), 
     ]; 
    } 

    /** 
    * Set service locator 
    * 
    * @param ServiceLocatorInterface $serviceLocator 
    */ 
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) 
    { 
     $this->sl = $serviceLocator; 
    } 

    /** 
    * Get service locator 
    * 
    * @return ServiceLocatorInterface 
    */ 
    public function getServiceLocator() 
    { 
     return $this->sl; 
    } 

} 

控制器代码:

<?php 

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Application\Entity\Musician as Musician; 
use Application\Entity\Album as Album; 


class MusiciansController extends AbstractActionController 
{ 

    public function createMusicianAction() 
    { 


     $musician = new Musician(); 
     $albumOne = new Album(); 
     $albumTwo = new Album(); 
     $albumOne->setName('The White Album'); 
     $albumTwo->setName('Sgt. Pepper'); 
     $albumOne->setReleaseYear('1974'); 
     $albumTwo->setReleaseYear('1967'); 

     $albums = array(
      $albumOne, 
      $albumTwo 
     ); 

     $musician->setName('The Beatles'); 
     $musician->setAlbums($albums); 

     $form = $this->getServiceLocator()->get('FormElementManager')->get('MusicianForm'); 
     $form->setMusician($musician); 

     return new ViewModel([ 

]); 

    } 
} 

,当我尝试绑定的形式,我结束了以下错误:

Zend\Form\Element\Collection::setObject expects an array or Traversable object argument; received "Application\Entity\Musician" 

我尝试在音乐家类中实现迭代器,但是这里的解决方案似乎很复杂,不太清楚。如何让这个绑定正常工作?

回答

1

我想通了!

这里的问题是,Zend框架需要相关的形式有他们自己的字段集为绑定正常工作,所有实体!

在这个具体的例子中,我创建了一个音乐家字段集,将其设置为在音乐家形式的碱字段集,并且音乐家字段集内创建的相册的形式收集。瞧!一切都很好地填充。