创建时的用户ID保持返回0并且不能使用

时间:2011-05-25 14:57:39

标签: 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

现在我不知道这是否是使用此方法的正确方法,但我有一个用户模型,包括电子邮件,密码,角色,日期,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调试它时,我总是看到当我使用mapper的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:我让它正在改变$ this-> getDbTable() - > insert()在save()方法中以下内容:

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 个答案:

答案 0 :(得分:0)

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';

假设$ this-> getDbTable()返回Zend_Db_Table_Abstract,这个:

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