2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class Syj_Model_UserMapper
8 protected $_tableInfos = array('name' => 'users');
10 public function getDbTable() {
11 if (null === $this->_dbTable) {
12 $this->_dbTable = new Zend_Db_Table($this->_tableInfos);
14 return $this->_dbTable;
17 public function find($id, Syj_Model_User $user) {
18 $result = $this->getDbTable()->find((int)$id);
19 if (0 == count($result)) {
22 $row = $result->current();
23 $this->_itemFromRow($user, $row);
27 public function findByEmail($email, Syj_Model_User $user) {
28 $table = $this->getDbTable();
29 $select = $table->select()->where('email = ?', (string)$email);
30 $row = $table->fetchRow($select);
34 $this->_itemFromRow($user, $row);
38 public function fetchAll() {
39 $resultSet = $this->getDbTable()->fetchAll();
41 foreach ($resultSet as $row) {
42 $entry = new Syj_Model_User();
43 $this->_itemFromRow($entry, $row);
49 protected function _itemFromRow(Syj_Model_User $item, Zend_Db_Table_Row $row) {
50 $item->setId($row->id)
51 ->setPassword($row->password)
52 ->setPseudo($row->pseudo)
53 ->setEmail($row->email)
55 ->setCreationAddr($row->creation_addr);
58 public function save (Syj_Model_User $user) {
60 'pseudo'=> (string) $user->pseudo,
61 'password'=> (string)$user->password,
62 'email'=> (string) $user->email,
63 'lang'=> (string) $user->lang,
64 'creation_addr'=> (string) $user->creationAddr
66 if (null === ($id = $user->getId())) {
67 $user->id = $this->getDbTable()->insert($data);
69 $this->getDbTable()->update($data, array('id = ?' => $id));
71 $user->notifyPendings();
74 public function delete (Syj_Model_User $pending) {
75 if (null !== ($id = $pending->getId())) {
76 $this->getDbTable()->delete(array('id = ?' => $id));