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

在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
    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').

    点赞 评论

相关推荐