gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:07:34 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com