1

I used Zend Framework 2.1(not 2.0x) method to populate a select/drop down that is described in following links:

Though it seems I have done as they told I got a error message like:

*... ::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, none given, called in ...*

which seems service locator is not used properly.

My form code that adds my FieldSet SupplierFieldset:

namespace Inventory\Form;
use Zend\Form\Form;
use Inventory\Model;

class ItemForm extends Form
{
    public function init()
    {
        $this->add(array(
                'name' => 'sup_code',
                'type' => 'Inventory\Form\SupplierFieldset'
        ));
    }
}

My 'SupplierFieldset' class:

namespace Inventory\Form;

use Inventory\Model;
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\InputFilter\InputFilterProviderInterface;

use Inventory\Model\SupplierTable;
use Inventory\Model\Supplier;

class SupplierFieldset extends Fieldset implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;
    protected $supplierTable;

    public function init()
    {
        parent::__construct('Suppliers Code');
        $this->setLabel('Supplier Code');
        $this->setName('supplier_code');
        $suppliers = $this->getSupplierTable()->fetchAll();

       $select = new Element\Select('supplier_code');
        $options = array();
        foreach ($suppliers as $supplier) {
            $options[$supplier->id] = $supplier->sup_code;
        }
        $select->setValueOptions($options);
    }

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

    public function getSupplierTable()
    {
        if (!$this->supplierTable) {
            $sm = $this->getServiceLocator();
            $this->supplierTable = $sm->get('Inventory\Model\SupplierTable');
        }
        return $this->supplierTable;
    }
}

My Module.php getFormElementConfig() function:

public function getFormElementConfig()
    {
        return array(
            'factories' => array(
                'SupplierFieldset' => function($sm) {
                    $serviceLocator = $sm->getServiceLocator();
                    $supplierTable = $serviceLocator->get('Inventory\Model\SupplierTable');
                    $fieldset = new SupplierFieldset($supplierTable);
                    return $fieldset;
                 }
            )
        );
    }

My SupplierTable.php model:

namespace Inventory\Model;

use Zend\Db\TableGateway\TableGateway;

class SupplierTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
}

I know SupplierTable model's constructor needs a TableGateway $tableGateway parameter. But this model is working properly when called from SupplierController.

4

0 回答 0