User id on creation keeps returning 0 and can't be used
I have a registration form which allows users to receive information about different countries they've selected through a multicheckbox. The countries from the list are also in a table and have an id. The way in which I'm designing this is the following:
users table
-----------------
id mail pass role etc...
pais(country) table
---------------
id countryname
users_has_pais
---------------
id user_id pais_id
Now I don't know if this is the right way of working with this or not but I have a users model with email, password, role, date, id, etc...getters and setters, and a users mapper which looks like this:
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;
}
}
I'm actually following the ZendFramework quickstart and adapting it to my needs... however, whenever I try to debug this with var_dump I always see that when I use the save() method of the mapper, the id is always 0,...which is a bad thing because then I can't use it to relate the user_id with the country_id.
Any help will be more than welcomed, so thanks in advance...
EDIT: To summarize these are the methods involved in the 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: I got it working changing the $this->getDbTable()->insert() inside the save() method for the following:
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()->upd开发者_如何学JAVAate($data, array('id = ?' => $id));
}
}
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']);
$id = $this->getDbTable()->insert($data);
//los paises estan en otra table
require_once 'UserHasPais.php';
Assuming $this->getDbTable() return Zend_Db_Table_Abstract, this:
$id = $this->getDbTable()->insert($data);
精彩评论