0

我在客户和销售之间的数据库上有 2 个具有多对一关系的表,我想id_client在销售表格上选择。因为那个o用那个。

销售表格:

public function __construct(ClientTable $table)
    {
        parent::__construct('vente');

        $this->setAttribute('method', 'post');
        $this->clientTable = $table;     
        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type'  => 'hidden',
            ),
        ));
        $this->add(
                array(
                    'name' => 'id_client',
                    'type' => 'Select',
                    'attributes' => array(
                        'id'    => 'id_client'
                    ),
                    'options' => array(
                        'label' => 'Catégory',
                        'value_options' => $this->getClientOptions(),
                        'empty_option'  => '--- Sélectionnez une categorie---'
                    ),
                )
            );
         public function getClientOptions()
        {
            $data  = $this->clientTable->fetchAll()->toArray();
            $selectData = array();

            foreach ($data as $key => $selectOption) {
                $selectData[$selectOption["id"]] = $selectOption["nom_client"];
            }

            return $selectData;
        }
}

销售控制器:

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Caisse\Model\Sale;          
use Caisse\Form\SaleForm; 

class SaleController extends AbstractActionController
{
    protected $saleTable;
    protected $clientTable;

    public function addAction()
    {
        $form = new SaleForm($this->clientTable);
        $form->get('submit')->setValue('Ajouter');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $vente = new Sale();
            $form->setInputFilter($sale->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $vente->exchangeArray($form->getData());
                $this->getSaleTable()->saveSale($sale);


                return $this->redirect()->toRoute('sale');
            }
        }
        return array('form' => $form);

    }
}

但每次我遇到这个问题:

可捕获的致命错误:传递给 Caisse\Form\SaleForm::__construct() 的参数 1 必须是 Admin\Model\ClientTable 的实例,给定 null。

这是做到这一点的好方法吗,欢迎对同一示例进行任何参考。

谢谢

4

1 回答 1

1

在您的控制器内部,函数addAction,您永远不会设置变量clientTable,也许您忘记初始化它。

像这样

public function addAction()
{
    $this->clientTable = $this->getServiceLocator()->get('Client\Model\ClientTable');
    $form = new SaleForm($this->clientTable);
    // ...
}

关于

    public function getClientOptions()
    {
        $data  = $this->clientTable->fetchAll();
        $selectData = array();

        foreach ($data as $row) {
            $selectData[$row->id] = $row->nom_client;
        }

        return $selectData;
    }
于 2013-07-31T14:54:17.917 回答