zend framework 2找不到用户类

时间:2014-04-30 15:18:30

标签: zend-framework2

我正在使用ZF2类进行插入用户信息抛出表单。我有一个User类,UserTable类,我已经对我的控制器进行了一些更改。

***User.php***
 <?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Users\Model;

/**
 * Description of User
 *
 * @author mlorente
 */
class User {

    public $id;
    public $name;
    public $email;
    public $password;

    public function setPassword($clear_password) {
        $this->password = md5($clear_password);
    }

    function exchangeArray($data){
        $this->name = (isset($data['name']))?$data['name']:null;
        $this->password = (isset($data['email']))?$data['email']:null;
        if (isset($data['password'])){
            $this->setPassword($data['password']);
        }
    }
}

UserTable.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Users\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

/**
 * Description of userTable
 *
 * @author mlorente
 */
class UserTable {

    protected $tableGateway;

    function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function saveUser(User $user){

        $data = array(
            'name' => $user->name,
            'email' => $user->email,
            'password' => $user->password,
        );

        $id = (int)$user->id;

        if($id == 0){
            $this->tableGateway->insert($data);
        }else{
            if($this->getUser($id)){
                $this->tableGateway->update($data, array('id' => $id));
            }else{
                throw new Exception('El ID de usuario no existe');
            }
        }
    }

    public function getUser ($id){
        $id = (int)$id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row){
            throw new Exception('No se ha encontrado la fila con ID '.$id);
        }

        return $row;
    }

}

RegisterController.php

public function processAction(){
        $form = new RegisterForm();
        $form->get('submit')->setValue('Enviar');

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

        $registro = new Register();
        $form->setInputFilter($registro->getInputFilter());
        $form->setData($request->getPost());

        if($form->isValid()){
            $registro->exchangeArray($form->getData());
            $this->createUser($form->getData());
            return $this->redirect()->toRoute(NULL, array('controller' => 'register','action' => 'confirm'));
        }else{
            $model = new ViewModel(array(
                'error' => true,
                'form' => $form,
            ));
            $model->setTemplate('users/register/index');
            return $model;
        }

        return array('form' => $form);
    }

    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;
    }

Las代码是控制器的一部分。当我测试应用程序时,它提到了这个错误

  

致命错误:类&#39;用户\控制器\用户&#39;在C:\ Program中找不到   文件\的EasyPHP-DevServer-14.1VC11 \数据\ localweb \ ZF2_TUTORIAL1 \模块\用户\ SRC \用户\控制器\ RegisterController.php   第74行

我正在寻找这条线,但我不知道如何修复它。我认为这比看起来简单。

1 个答案:

答案 0 :(得分:0)

由于您(可能)在控制器类的顶部声明了命名空间Users\Controller,因此当您调用new User();时,PHP认为您的意思是new \Users\Controller\User(),这不存在(因此错误)。要解决此问题,请将用户模型导入当前范围。你需要为UserTable做同样的事情,否则你会遇到同样的问题。

将这两行添加到控制器类的顶部(在命名空间声明之后):

use Users\Model\User;
use Users\Model\UserTable;

然后它应该工作。