无法将数据从表单传递到模型zend 2.3

时间:2015-08-14 07:57:06

标签: php mysql zend-framework

我正在尝试使用tablegateway将数据插入到seviral表中。我收到以下错误。

Fatal error: Call to a member function saveStudent() on a non-object in C:\xampp\htdocs\disability\module\Admin\src\Admin\Controller\AdminController.php on line 48

我尝试实施此处给出的解决方案Zend\Form: Call to a member function insert() on a non-object in Zend/Form/Fieldset.php

但没有改变错误是一样的。这是我的表格代码

<?php
namespace Admin\Form;
use Zend\Form\Element;
use Zend\Form\Form;

class AddstudentForm extends Form
{
    public function __construct($name = null)
    {
        // we want to ignore the name passed
      //  parent::__construct('addstudent');
         parent::__construct('AddstudentForm');


        $this->add(array(
            'name' => 'fio',
            'type' => 'Text',
            'options' => array(
                'label' => 'Фио',
            ),
        ));
        $this->add(array(
            'name' => 'gender',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Пол',
                'value_options' => array(
                    '0' => 'М',
                    '1' => 'Ж',
                ),

            ),
        ));
        $this->add(array(
            'name' => 'birthdate',
            'type' => 'Text',
            'options' => array(
                'label' => 'Дата рождения',
            ),
        ));

        $this->add(array(
            'name' => 'edge',
            'type' => 'Text',
            'options' => array(
                'label' => 'Возраст',
            ),
        ));

        $this->add(array(
            'name' => 'university',
            'type' => 'Text',
            'options' => array(
                'label' => 'Учебное заведение',
            ),
        ));
        $this->add(array(
            'name' => 'group',
            'type' => 'Text',
            'options' => array(
                'label' => 'Группа/класс',
            ),
        ));
        $this->add(array(
            'name' => 'department',
            'type' => 'Text',
            'options' => array(
                'label' => 'Факультет',
            ),
        ));
        $this->add(array(
            'name' => 'grate',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Курс',
                'value_options' => array(
                    '0' => '1',
                    '1' => '2',
                    '2' => '3',
                    '3' => '4',
                    '4' => '5',
                    '5' => '6',

                ),

            ),
        ));
        $this->add(array(
            'name' => 'enterence',
            'type' => 'Text',
            'options' => array(
                'label' => 'Год поступления',
            ),
        ));

        $this->add(array(
            'name' => 'financesource',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Источник финансирования',
                'value_options' => array(
                    '0' => 'Бютжет',
                    '1' => 'Контракт',

                ),

            ),
        ));

        $this->add(array(
            'name' => 'studyform',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Форма обучения',
                'value_options' => array(
                    '0' => 'Дневная',
                    '1' => 'Заочная',

                ),

            ),
        ));

        $this->add(array(
            'name' => 'homeaddress',
            'type' => 'Text',
            'options' => array(
                'label' => 'Домашний адрес',
            ),
        ));

        $this->add(array(
            'name' => 'actualaddress',
            'type' => 'Text',
            'options' => array(
                'label' => 'Фактический адрес',
            ),
        ));
        $this->add(array(
            'name' => 'phone',
            'type' => 'Text',
            'options' => array(
                'label' => 'Телефон',
            ),
        ));

        $this->add(array(
            'name' => 'workplace',
            'type' => 'Text',
            'options' => array(
                'label' => 'Место работы',
            ),
        ));
        $this->add(array(
            'name' => 'services',
            'type' => 'Zend\Form\Element\Textarea',
            'options' => array(
                'label' => 'Услуги',
            ),
        ));

$this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'Сохранить',
                'id' => 'submitbutton',
            ),
        ));        

    }
}

和控制器:

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Admin for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Admin;
use Admin\Form\AddstudentForm;
class AdminController extends AbstractActionController
{
    protected $StudentTable;

    public function indexAction()
    {
        return array();
    }

    public function getStudentTable()
    {
        if (!$this->StudentTable) {
            $sm = $this->getServiceLocator();
            $this->studentTable = $sm->get('Admin\Model\StudentTable');
        }
        return $this->StudentTable;
    }

public function addstudentAction()
{

    $form = new AddstudentForm();
  //  $form->get('submit')->setValue('Сохранить');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $admin = new Admin();
        $form->setInputFilter($admin->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $admin->exchangeArray($form->getData());
           $this->getStudentTable()->saveStudent($admin);
                // Redirect to list of albums
          //  return $this->redirect()->toRoute('admin');
        }
    }
    return array('form' => $form);


  //  return array();
}
    public function fooAction()
    {
        // This shows the :controller and :action parameters in default route
        // are working when you browse to /admin/admin/foo
        return array();
    }
}

这可能有什么问题。或者你需要什么信息?

1 个答案:

答案 0 :(得分:0)

您的getStudenTable()方法中存在拼写错误,因此$this->getStudentTable()返回null。

$this->studentTable = $sm->get('Admin\Model\StudentTable');

应该是

$this->StudentTable = $sm->get('Admin\Model\StudentTable');