Zend framework 创建时的用户id不断返回0和can';不可用

Zend framework 创建时的用户id不断返回0和can';不可用,zend-framework,zend-db-table,Zend Framework,Zend Db Table,我有一个注册表格,允许用户通过多检查框接收他们选择的不同国家的信息。列表中的国家/地区也在一个表中,并且有一个id。我设计该列表的方式如下: users table ----------------- id mail pass role etc... pais(country) table --------------- id countryname users_has_pais --------------- id user_id pais_id public functi

我有一个注册表格,允许用户通过多检查框接收他们选择的不同国家的信息。列表中的国家/地区也在一个表中,并且有一个id。我设计该列表的方式如下:

users table
-----------------
id  mail  pass  role  etc...

pais(country) table
---------------
id  countryname

users_has_pais
---------------
id  user_id  pais_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']);
        //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));
    }
}
现在我不知道这是否是正确的处理方式,但我有一个用户模型,其中包含电子邮件、密码、角色、日期、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,……这是一件坏事,因为这样我就无法使用它将用户id与国家id关联起来

我们非常欢迎您的帮助,所以请提前感谢

编辑:总结一下应用程序\模型\用户中涉及的方法

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()的工作如下:

users table
-----------------
id  mail  pass  role  etc...

pais(country) table
---------------
id  countryname

users_has_pais
---------------
id  user_id  pais_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']);
        //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));
    }
}
假设$this->getDbTable()返回Zend\u Db\u Table\u Abstract,则:

        $id = $this->getDbTable()->insert($data);

如果
$user->getId()
总是返回0,那么getId()方法似乎有问题。也许你应该检查一下你的
应用程序\u Model\u Users
类。@Marcin刚刚更新了线程,这样你就可以检查它了……实际上我甚至不确定是否需要setId方法,因为我在数据库中使用一个自动递增的id,它是主键。@Xerkus$id=$this->getDbTable()->insert($data);var_dump($id);死亡;-->输出=int0@la-f0ka尝试使用未注释的unset($data['id']);我按照上次编辑时显示的方式修复了它。我创建行来保存数据,然后请求id。