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

标签 php mysql zend-framework

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

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',
            ),
        ));        

    }
}

和 Controller :

<?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();
    }
}

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

最佳答案

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

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

应该是

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

关于php - 无法将数据从表单传递到模型 zend 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005041/

相关文章:

php - 使用 Doctrine EntityRepository 中的魔术查找方法好吗?

mysql - UNION SELECT 错误 1064 但如果单独执行则有效

php - 在 PHP(和/或 Zend Framework)中解析 XML 响应

php - 在 Linux 上的 Zend 配置 ini 文件中包含花括号内的变量

PHP Moved Temporarily 错误?我在 Android 中使用 c2dm

php - MySQL搜索与相邻记录的词距离

php - 启用了 MultiViews 的 Apache Clean-Urls

php - 我无法使用 PDO 过程更新数据中的状态行

java - Netbeans 中的 JTable

model-view-controller - 表数据网关和模型