2

我正在尝试在 UserTable 中使用 AbstractTableGateway 插入用户数据。但我收到错误。

我已经尝试了很多解决方案,但我不明白这里有什么问题。

 C:\wamp\www\1625\vendor\ZF2\library\Zend\Db\ResultSet\ResultSet.php:68

Message:

    Object must be of type ArrayObject, or at least implement exchangeArray

我正在尝试使用保存数据

$user = new User();
$user->userExchangeData($user_data);
$this->getUserTable()->saveUser($object);

我的用户表模型是

<?php 

namespace User\Model;

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

class UserTable extends AbstractTableGateway
{
    protected $table = 'y2m_user';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new User());

        $this->initialize();
    }

    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }

    public function getUser($user_id)
    {
        $id  = (int) $user_id;
        $rowset = $this->select(array('user_id' => $user_id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $user_id");
        }
        return $row;
    }

    public function saveUser(User $user)
    {
       $data = array(
            'user_given_name' => $user->user_given_name,
            'user_first_name'  => $user->user_first_name,
            'user_middle_name'  => $user->user_middle_name,
            'user_last_name'  => $user->user_last_name,
            'user_status'  => $user->user_status,
            'user_added_ip_address'  => $user->user_added_ip_address,
            'user_email'  => $user->user_email,
            'user_password'  => $user->user_password,
            'user_gender'  => $user->user_gender,
            'user_timeline_photo_id'  => $user->user_timeline_photo_id,
            'user_language_id'  => $user->user_language_id,
            'user_user_type_id'  => $user->user_user_type_id,
            'user_profile_photo_id'  => $user->user_profile_photo_id,
            'user_friend_request_reject_count'  => $user->user_friend_request_reject_count,
            'user_mobile'  => $user->user_mobile,
            'user_verification_key'  => $user->user_verification_key,
            'user_added_timestamp'  => $user->user_added_timestamp,
            'user_modified_timestamp'  => $user->user_modified_timestamp,
            'user_modified_ip_address'  => $user->user_modified_ip_address,     
        );
         $user_id = (int)$user->user_id;
        if ($user_id == 0) {
            $this->insert($data);
        } else {
            if ($this->getUser($user_id)) {
                $this->update($data, array('user_id' => $user_id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteUser($user_id)
    {
        $this->delete(array('user_id' => $user_id));
    }

}

我的用户模型是

<?php 

namespace User\Model;

use Zend\InputFilter\InputFilter;


class User  
{
    public $user_id;
    public $user_given_name;
    public $user_first_name;
    public $user_middle_name;
    public $user_last_name;
    public $user_status;
    public $user_added_ip_address;
    public $user_email;
    public $user_password;
    public $user_gender;
    public $user_timeline_photo_id;
    public $user_language_id;
    public $user_user_type_id;
    public $user_profile_photo_id;
    public $user_friend_request_reject_count;
    public $user_mobile;
    public $user_verification_key;
    public $user_added_timestamp;
    public $user_modified_timestamp;
    public $user_modified_ip_address;

    protected $inputFilter;

    /**
     * Used by ResultSet to pass each database row to the entity
     */ 
    public function userExchangeData($data)
    {
        $this->user_id     = (isset($data['user_id'])) ? $data['user_id'] : null;
        $this->user_given_name = (isset($data['user_given_name'])) ? $data['user_given_name'] : null;
        $this->user_first_name  = (isset($data['user_first_name'])) ? $data['user_first_name'] : null;
        $this->user_middle_name  = (isset($data['user_middle_name'])) ? $data['user_middle_name'] : null;
        $this->user_last_name  = (isset($data['user_last_name'])) ? $data['user_last_name'] : null;
        $this->user_status  = (isset($data['user_status'])) ? $data['user_status'] : null;
        $this->user_added_ip_address  = (isset($data['user_added_ip_address'])) ? $data['user_added_ip_address'] : null;
        $this->user_email  = (isset($data['user_email'])) ? $data['user_email'] : null;
        $this->user_password  = (isset($data['user_password'])) ? $data['user_password'] : null;
        $this->user_gender  = (isset($data['user_gender'])) ? $data['user_gender'] : null;
        $this->user_timeline_photo_id  = (isset($data['user_timeline_photo_id'])) ? $data['user_timeline_photo_id'] : null;
        $this->user_language_id  = (isset($data['user_language_id'])) ? $data['user_language_id'] : null;
        $this->user_user_type_id  = (isset($data['user_user_type_id'])) ? $data['user_user_type_id'] : null;
        $this->user_profile_photo_id  = (isset($data['user_profile_photo_id'])) ? $data['user_profile_photo_id'] : null;
        $this->user_friend_request_reject_count  = (isset($data['user_friend_request_reject_count'])) ? $data['user_friend_request_reject_count'] : null;
        $this->user_mobile  = (isset($data['user_mobile'])) ? $data['user_mobile'] : null;
        $this->user_verification_key  = (isset($data['title'])) ? $data['title'] : null;
        $this->user_added_timestamp  = (isset($data['user_added_timestamp'])) ? $data['user_added_timestamp'] : null;
        $this->user_modified_timestamp  = (isset($data['user_modified_timestamp'])) ? $data['user_modified_timestamp'] : null;
        $this->user_modified_ip_address  = (isset($data['user_modified_ip_address'])) ? $data['user_modified_ip_address'] : null;
    }

    public function getArrayCopy()
    {
        return get_object_vars($this);
    }





    function getUserIp(){
            $ip ="";
            //Test if it is a shared client
            if (!empty($_SERVER['HTTP_CLIENT_IP'])){
              $ip=$_SERVER['HTTP_CLIENT_IP'];
            //Is it a proxy address
            }elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
              $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
            }else{
              $ip=$_SERVER['REMOTE_ADDR'];
            }
            //The value of $ip at this point would look something like: "192.0.34.166"
            $ip = ip2long($ip);         
            return $ip;

     }

      function getUserNameSplit(){
            $ip ="";
            //Test if it is a shared client
            if (!empty($_SERVER['HTTP_CLIENT_IP'])){
              $ip=$_SERVER['HTTP_CLIENT_IP'];
            //Is it a proxy address
            }elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
              $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
            }else{
              $ip=$_SERVER['REMOTE_ADDR'];
            }
            //The value of $ip at this point would look something like: "192.0.34.166"
            $ip = ip2long($ip);         
            return $ip;

     }
}
4

1 回答 1

2

请使用“exchangeArray”而不是“userExchangeData”。

于 2013-05-29T13:19:02.847 回答