创建时的用户ID始终返回0,无法使用
||
我有一个注册表,该注册表使用户可以通过多复选框接收有关他们选择的不同国家的信息。列表中的国家/地区也在表格中并具有ID。我的设计方式如下:
users table
-----------------
id mail pass role etc...
pais(country) table
---------------
id countryname
users_has_pais
---------------
id user_id pais_id
现在我不知道这是否是正确的工作方式,但是我有一个带有电子邮件,密码,角色,日期,ID等的用户模型……getter和setter,以及一个看起来类似的用户映射器像这样:
class Application_Model_UsersMapper
{
protected $_dbTable;
protected $_salt = \'$2a$07$lalalalalasomething$\';
public function setDbTable($dbTable)
{
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception(\'Invalid table data gateway provided\');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable()
{
if (null === $this->_dbTable) {
$this->setDbTable(\'Application_Model_DbTable_Users\');
}
return $this->_dbTable;
}
public function save(Application_Model_Users $user)
{
$data = array(
\'email\' => $user->getEmail(),
\'password\' => crypt($user->getPassword(), $this->_salt),
\'fecha\' => Zend_Date::now()->toString(\'yyyyMMddHHmmss\'),
\'url\' => $user->getUrl(),
\'responsable\' => $user->getResponsable(),
\'role\' => $user->getRole(),
\'id\' => $user->getId(),
);
if (0 === ($id = $user->getId())) {
//unset($data[\'id\']);
$this->getDbTable()->insert($data);
//los paises estan en otra table
require_once \'UserHasPais.php\';
$paisTableModel = new Model_UsersHasPais();
$paisTableModel->updateUserPais($id, $user->getPais());
} else {
$this->getDbTable()->update($data, array(\'id = ?\' => $id));
}
}
public function find($id, Application_Model_Users $user)
{
$result = $this->getDbTable()->find($id);
if (0 == count($result)) {
return;
}
$row = $result->current();
$user->setId($row->id)
->setEmail($row->email)
->setPassword($row->password)
->setFecha($row->fecha)
->setUrl($row->url)
->setResponsable($row->responsable)
->setRole($row->role);
}
public function fetchAll()
{
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Guestbook();
$entry->setId($row->id)
->setEmail($row->email)
->setPassword($row->password)
->setFecha($row->fecha)
->setUrl($row->url)
->setResponsable($row->responsable)
->setRole($row->role);
$entries[] = $entry;
}
return $entries;
}
}
我实际上是在遵循ZendFramework快速入门,并使其适应我的需要...但是,每当我尝试使用var_dump调试它时,我总会看到当我使用映射器的save()方法时,id始终为0, ...这是一件坏事,因为这样我就无法使用它来将user_id与country_id相关联。
任何帮助都将受到欢迎,因此在此先感谢...
编辑:总结这些是Application_Model_Users中涉及的方法
class Application_Model_Users
{
protected $_responsable;
protected $_fecha;
protected $_url;
protected $_role;
protected $_password;
protected $_email;
protected $_pais;
protected $_id;
public function __construct(array $options = null)
{
if (is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = \'set\' . $name;
if ((\'mapper\' == $name) || !method_exists($this, $method)) {
throw new Exception(\'Invalid user property\');
}
$this->$method($value);
}
public function __get($name)
{
$method = \'get\' . $name;
if ((\'mapper\' == $name) || !method_exists($this, $method)) {
throw new Exception(\'Invalid user property\');
}
return $this->$method();
}
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = \'set\' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
public function setId($id)
{
$this->_id = (int) $id;
return $this;
}
public function getId()
{
return $this->_id;
}
}
Edit2:我通过更改save()方法内的$ this-> getDbTable()-> insert()来实现以下目的:
public function save(Application_Model_Users $user)
{
$data = array(
\'email\' => $user->getEmail(),
\'password\' => crypt($user->getPassword(), $this->_salt),
\'fecha\' => Zend_Date::now()->toString(\'yyyyMMddHHmmss\'),
\'url\' => $user->getUrl(),
\'responsable\' => $user->getResponsable(),
\'role\' => $user->getRole(),
\'id\' => $user->getId(),
);
if (0 === ($id = $user->getId())) {
//unset($data[\'id\']);
//this is a test
$user_id = $this->getDbTable()->createRow();
$user_id->email = $data[\'email\'];
$user_id->password = crypt($data[\'password\'], $this->_salt);
$user_id->url = $data[\'url\'];
$user_id->responsable = $data[\'responsable\'];
$user_id->role = $data[\'role\'];
$user_id->fecha = Zend_Date::now()->toString(\'yyyyMMddHHmmss\');
$user_id->save();
//till here
//los paises estan en otra table
require_once \'UserHasPais.php\';
$paisTableModel = new Model_UsersHasPais();
$paisTableModel->updateUserPais($user_id->id, $user->getPais());
} else {
$this->getDbTable()->update($data, array(\'id = ?\' => $id));
}
}
没有找到相关结果
已邀请:
1 个回复
厘恼轨
假设$ this-> getDbTable()返回Zend_Db_Table_Abstract,则: