ZF2 - 如何解决Zend Framework 2数据库连接问题

时间:2014-04-20 21:56:09

标签: zend-framework2

我已经开始学习ZF2,并且一直在努力让基本的CRUD正常工作。要学习如何做到这一点,我已经学习了很多教程:

  1. 从书中,ZF2,通过实例学习
  2. https://zf2.readthedocs.org/en/release-2.0.0/user-guide/database-and-models.html
  3. 目前我无法让任何教程在逐字复制的情况下工作。我一直打404页。这对我来说是一个路由问题,但是我不熟悉这个问题,我确信有很多变量需要考虑。

    所以我的问题:

    1. 我在哪里可以找到一个简单的,工作的CRUD模块,它连接到一个简单的数据库,并且可以从word go开始工作?这将有助于消除模块成为问题,然后我将能够查看其他潜在问题。

    2. 使用ZF2进行故障排除时,最佳做法是什么?例如,如果我获得404页面,我如何知道请求了哪个页面?

    3. [编辑] - 添加更多评论和一些代码:

      在下面的示例中,只要我不尝试将信息添加到数据库,注册过程就可以顺利进行。所以,如果我发表评论:

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

      我转到了注册完整页面。这意味着该问题与以下内容有关:

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

      我有什么想法可以解决上述问题?

      查看/用户/注册/ index.phtml

      <section class="register">
      <h2>Register</h2>
      <?php if ($this->error): ?>
          <p class="error">
              There were one or more issues with your submission.
              Please correct them as
              indicated below.
          </p>
      <?php endif ?>
      
      <?php
      $form = $this->form;
      $form->prepare();
      $form->setAttribute('action', $this->url(NULL,
          array('controller'=>'register', 'action' => 'process')));
      $form->setAttribute('method', 'post');
      echo $this->form()->openTag($form);
      ?>
      
      <dl class="zend_form">
          <dt><?php echo $this->formLabel($form->get('name')); ?></dt>
          <dd><?php
              echo $this->formElement($form->get('name'));
              echo $this->formElementErrors($form->get('name'));
              ?></dd>
          <dt><?php echo $this->formLabel($form->get('email')); ?></dt>
          <dd><?php
              echo $this->formElement($form->get('email'));
              echo $this->formElementErrors($form->get('email'));
              ?></dd>
          <dt><?php echo $this->formLabel($form->get('password'));
              ?></dt>
          <dd><?php
              echo $this->formElement($form->get('password'));
              echo $this->formElementErrors($form->get('password'));
              ?></dd>
          <dt><?php echo $this->formLabel($form->get('confirm_password')); ?></dt>
          <dd><?php
              echo $this->formElement($form->get('confirm_password'));
              echo $this->formElementErrors($form->get('confirm_password'));
              ?></dd>
          <dd><?php
              echo $this->formElement($form->get('submit'));
              echo $this->formElementErrors($form->get('submit'));
              ?></dd>
      </dl>
      <?php echo $this->form()->closeTag() ?>
      

      SRC /用户/窗体/ RegisterForm.php

      <?php
      // filename : module/Users/src/Users/Form/RegisterForm.php
      
      namespace Users\Form;
      use Zend\Form\Form;
      
      class RegisterForm extends Form
      {
      public function __construct($name = null)
      {
          parent::__construct('Register');
          $this->setAttribute('method', 'post');
          $this->setAttribute('enctype','multipart/form-data');
      
          //Add the required fields using the add method
          $this->add(array(
              'name' => 'name',
              'attributes' => array('type' => 'text',),
              'options' => array(
                  'label' => 'Full Name',
              ),
          ));
      
          //Additional validators and filters
          $this->add(array(
              'name' => 'email',
              'options' => array('label' => 'Email',),
              'attributes' => array('required' => 'required','type' => 'email'),
              'filters' => array( array( 'name' => 'StringTrim' ), ),
              'validators' => array(
                  array(
                      'name' => 'EmailAddress',
                      'options' => array(
                          'messages' => array(
                              \Zend\Validator\EmailAddress::INVALID_FORMAT => 'Email address     format is invalid'
                          )
                      )
                  )
              )
          ));
      
          //Add a password field
          $this->add(array(
              'name' => 'password',
              'attributes' => array('type' => 'password',),
              'options' => array(
                  'label' => 'Password',
              ),
          ));
      
          //Add a confirm password field
          $this->add(array(
              'name' => 'confirm_password',
              'attributes' => array('type' => 'password',),
              'options' => array(
                  'label' => 'Confirm password',
              ),
          ));
      
          $this->add(array(
              'name' => 'submit',
              'attributes' => array('type' => 'submit', 'value' => 'Add'),
              'options' => array(
                  'label' => 'Register',
              ),
          ));
      
      
      }
      }
      

      的src /用户/控制器/ RegisterController.php

      <?php
      namespace Users\Controller;
      
      use Zend\Mvc\Controller\AbstractActionController;
      use Zend\View\Model\ViewModel;
      use Users\Form\RegisterForm;
      use Users\Form\RegisterFilter;
      
      class RegisterController extends AbstractActionController
      {
      
      public function indexAction()
      {
          $form = new 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->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)
      {
          $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;
      }
      
      }
      

2 个答案:

答案 0 :(得分:1)

对于排除ZF2应用程序(我发现)的最佳方法是使用XDebug(或类似程序)并能够逐步执行。

除此之外,Zend Developer Tools模块在​​开发过程中可能非常有用。

如果您在display_exceptions配置中启用view_manager,您应该能够看到实际导致此问题的原因。

答案 1 :(得分:0)

谷歌搜索后我发现了问题。本质上,数据库详细信息不会自动包含在内。要包含它们并解决此问题,我将以下内容添加到application.conofig.php和module_listener_options:

    'config_glob_paths' => array(
        __DIR__ . '/autoload/{,*.}{global,local}.php'
    ),