dongque5529 2016-06-08 21:57
浏览 38
已采纳

在Zend Framework 2中声明模型变量的顺序

I am a beginner of Zend Framework2. Following the example of "Zend Framework 2.0 by Example Beginner's Guide" I got stuck in a weird problem.

Here is my project structure.

project structure

I have a simple function creatureUser() in RegisterController.php:

<?php
namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;
use users\Model\User;
use Users\Model\UserTable;

class RegisterController extends AbstractActionController
{

    public function indexAction()
    {
        //$form = new RegisterForm();
        $form = $this->getServiceLocator()->get('RegisterForm');
        $viewModel  = new ViewModel(array('form' => $form)); 
        return $viewModel; 
    }

    public function confirmAction()
    {
        $viewModel  = new ViewModel(); 
        return $viewModel; 
    }

    public function processAction()
    {
        if (!$this->request->isPost()) {
            return $this->redirect()->toRoute(NULL , array( 
                    'controller' => 'register', 
                    'action' =>  'index' 
                ));
        }

        $post = $this->request->getPost();

        //$form = new RegisterForm();
        //$inputFilter = new RegisterFilter();
        //$form->setInputFilter($inputFilter);
        $form = $this->getServiceLocator()->get('RegisterForm');

        $form->setData($post);
        if (!$form->isValid()) {
            $model = new ViewModel(array(
                'error' => true,
                'form'  => $form,
            ));
            $model->setTemplate('users/register/index');
            return $model;
        }

        // Create user
        $this->createUser($form->getData());

        return $this->redirect()->toRoute(NULL , array( 
            'controller' => 'register', 
            'action' =>  'confirm' 
        ));
    }

    protected function createUser(array $data)
    {
        $user = new User();
        $sm = $this->getServiceLocator();
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new \Users\Model\User);
        $tableGateway = new \Zend\Db\TableGateway\TableGateway('user', $dbAdapter, null, $resultSetPrototype);

        //$user = new User();
        $user->exchangeArray($data);
        $userTable = new UserTable($tableGateway);

        $userTable->saveUser($user);
        return true;
    }
}

When run the above code I get an error:

Fatal error: Class 'users\Model\User' not found in C:\WebApp\ZF2Skeleton\module\Users\src\Users\Controller\RegisterController.php on line 90.

But If I move $user = new User(); below. Like this one:

protected function createUser(array $data)
{
    $sm = $this->getServiceLocator();
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
    $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
    $resultSetPrototype->setArrayObjectPrototype(new \Users\Model\User);
    $tableGateway = new \Zend\Db\TableGateway\TableGateway('user', $dbAdapter, null, $resultSetPrototype);

    $user = new User();
    $user->exchangeArray($data);
    $userTable = new UserTable($tableGateway);

    $userTable->saveUser($user);
    return true;
}

It will work perfectly. Can anyone tell me how this happens, please? Is it a sequence problem? Thank you!

  • 写回答

1条回答 默认 最新

  • duanjia3187 2016-06-09 00:32
    关注

    It's a simple typo in your controller:

    use users\Model\User;
    

    should be:

    use Users\Model\User;
    

    (note the capital 'U' in 'Users').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开