将数据从Zend的表单插入数据库

时间:2016-04-23 03:41:13

标签: zend-framework2 zend-form

我在使用Zend FrameWork 2将记录插入数据库时​​遇到问题。 提交表单时没有保存到数据库中的记录,也没有出现错误消息,只有确认消息告诉我<< Registered Successfully >> 这是我的代码,提前谢谢:

 // App/config/autoload/global.php
 return array(
       'db'   => array(
            'driver'         => 'pdo',
            'dsn'            =>  'mysql:dbname=zf_app;host=localhost;port:8080',
            'username'       => 'zf_user',
            'password'       => 'zf_pass',
            'driver_options' => array(
                            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
                            ),

                    ),
      'service_manager'   => array(
            'factories'   => array(
                        'Zend\Db\Adapter\Adapter' =>'Zend\Db\Adapter\AdapterServiceFactory',
                        ),
            ),
 );

   #################################################################

 //App\module\Users\src\Users\Model\Usertable.php

   <?php

namespace Users\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class UserTable
{
protected $tableGateway ;

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

public function saveUser(User $user)
{
    $data = array(
            'email'     => $user->email ,
            'name'      => $user->name,
            '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("User ID does not Exist ");

        }
    }
}

public function getUser($id)
{
    $id      = (int)$id;
    $rowset  = $this->tableGateway->select(array('id' => $id ));
    $row     = $rowset->current();

    if(!$row)
    {
        throw new \Exception("Could not Find row $id ");
    }
    return $row ;
  }

}



   ##########################################################
  // App\module\Users\src\Users\Controller\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 Zend\Db\Adapter\Adapter;


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

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

  }


  public 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);
       $this->createUser($form->getData());
       return true;
     }



 }

2 个答案:

答案 0 :(得分:0)

Create  // App/config/autoload/local.php

// App/config/autoload/local.php

return array(
    'db' => array(
        'username' => "zf_user",
        'password' => "zf_pass",
    )
);

并从global.php中删除用户名和密码

答案 1 :(得分:0)

公共功能saveUser(User $ user)中的用户是什么?您没有使用用户文件。