2
我想在UserTable中使用AbstractTableGateway插入用戶數據。但我收到錯誤。zf2插入使用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;
}
}
太好了。有效。 Byt爲什麼強迫保持這樣的名字。因爲這個,我無法編碼約定? – jyoti
thanx。我也是zf2的新手。它在內部使用這個功能。所以我們必須保持原樣。如果任何時候我會找到解決方案,那麼我會在這裏發佈。 – Hasina
如果你需要它,你可以離開'userExchangeData',並使其指向'exchangeArray' –