Zend Framewok没有填充所有表格字段的值

时间:2016-06-03 12:51:39

标签: zend-framework

我是Zend Framework的初学者,并使用Zend Framework 2.5 veresion。我遇到了同样的问题,无法解决。我的Model.php与上面显示的不同。

Model.php

命名空间用户; 使用Zend \ ModuleManager \ Feature \ AutoloaderProviderInterface;

使用Zend \ ModuleManager \ Feature \ ConfigProviderInterface;

class Module实现AutoloaderProviderInterface,ConfigProviderInterface {

public function getAutoloaderConfig(){
    return array(
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ =>    __DIR__.'/src/'.__NAMESPACE__,
            )
        ) 
    );
}
public function getConfig(){
    return include __DIR__. '/config/module.config.php';
}

}

我的' tbl_user'有字段' _'喜欢' first_name',' last_name',' contact_num'哪些没有列出。其他没有下划线' _'字段列出。 我有什么问题,任何人都可以帮助我吗?

我的输出是:

  

User \ Model \ User Object   (       [id:protected] => 4       [first_name:protected] =>       [last_name:protected] =>       [contact_num:protected] =>       [email:protected] => dev@email.com       [指定:受保护] => C ++程序员       [text:protected] =>       [name:protected] =>       [profile_pic:protected] =>   )

这是我的模型' User.php'

<?php
namespace User\Model;

class User implements UserInterface{
    protected $id;
    protected $first_name;
    protected $last_name;
    protected $contact_num;
    protected $email;
    protected $designation;
    protected $text;
    protected $name;
    protected $profile_pic;

    public function getId(){
        return $this->id;
    }
    public function setId($id){
        $this->id = $id;
    }
    public function getName(){
        return $this->name;
    }
    public function setName($first_name, $last_name){
        $this->name = $first_name.' '.$last_name;
    }       
    public function getContact(){
        return $this->contact_num;
    }
    public function setContact($contact_num){
        $this->contact_num = $contact_num;
    }
    public function getEmail(){
        return $this->email;
    }
    public function setEmail($email){
        $this->email = $email;
    }
    public function getDesignation(){
        return $this->designation;
    }
    public function setDesignation($designation){
        $this->designation = $designation;
    }
    public function getProfilePic(){
        return $this->profile_pic;
    }
    public function setProfilePic($profile_pic){
        $this->profile_pic = $profile_pic;
    }
    /*public function getText(){
        return $this->text;
    }
    public function setText($text){
        $this->text = $text;
    }*/
}

&GT;

这是我的ZendDbSqlMapper.php&#39;

<?php
namespace User\Mapper;

use User\Model\UserInterface;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Update;

class ZendDbSqlMapper implements UserMapperInterface{
    protected $dbAdapter;
    protected $hydrator;
    protected $userPrototype;

    public function __construct(
        AdapterInterface $dbAdapter,
        HydratorInterface $hydrator,
        UserInterface $userPrototype
    ){
        $this->dbAdapter = $dbAdapter;
        $this->hydrator  = $hydrator;
        $this->userPrototype = $userPrototype;
    }

    public function find($id){
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select('tbl_users');
        $select->where(array('id = ?' => $id));
        $stmt = $sql->prepareStatementForSqlObject($select);
        $result = $stmt->execute();
        if($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()){
            return $this->hydrator->hydrate($result->current(), $this->userPrototype);
        }
        throw new \InvalidArgumentException("User with given ID:{$id} not found");

    }

    public function findAll(){
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select('tbl_users');
        $stmt = $sql->prepareStatementForSqlObject($select);
        $result = $stmt->execute();
        //\Zend\Debug\Debug::dump($result); die;
        if($result instanceof ResultInterface && $result->isQueryResult()){
            //$resultSet = new ResultSet();
            $resultSet = new HydratingResultSet($this->hydrator, $this->userPrototype);
            //\Zend\Debug\Debug::dump($resultSet->initialize($result)); die;
            return $resultSet->initialize($result);
        }
        return array();         

    }

    public function save(UserInterface $userObject){
        $userData = $this->hydrator->extract($userObject);
        unset($userData['id']);
        if($userObject->getId()){
            $action = new Update('tbl_users');
            $action->setData($userData);
            $action->where(array('id = ?' => $userObject->getId()));
        }else{
            $action = new Insert('tbl_users');
            $action->values($userData);
        }
        $sql = new Sql($this->dbAdapter);
        $stmt = $sql->prepareStatementForSqlObject($action);
        $result = $stmt->execute();

        if($result instanceof ResultInterface){
            if($newId = $result->getGeneratedValue()){
                $userObject->setId($newId);
            }
            return $userObject;
        }
        return new \Exception("Database Error");
    }
}

&GT;

这里是&#39; ListController.php&#39;

<?php
namespace User\Controller;

use User\Service\UserServiceInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ListController extends AbstractActionController{
    protected $userService; 

    public function __construct(UserServiceInterface $userService){
        $this->userService = $userService;
    }

    public function indexAction(){
        return new ViewModel(array(
            'users' => $this->userService->findAllUsers()
        ));
    }

    public function detailAction(){
        $id = $this->params()->fromRoute('id');
        try {
            $user = $this->userService->findUser($id);
        }catch(\InvalidArgumentException $ex){
            return $this->redirect()->toRoute('user');
        }
        return new ViewModel(
            array( 'user' =>$user )
        );  
    }
}

&GT;

谢谢。

0 个答案:

没有答案