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 findByPseudo($pseudo, Syj_Model_User $user) {
28 $table = $this->getDbTable();
29 $select = $table->select()->where('pseudo = ?', (string)$pseudo);
30 $row = $table->fetchRow($select);
34 $this->_itemFromRow($user, $row);
38 public function findByEmail($email, Syj_Model_User $user) {
39 $table = $this->getDbTable();
40 $select = $table->select()->where('email = ?', (string)$email);
41 $row = $table->fetchRow($select);
45 $this->_itemFromRow($user, $row);
49 public function fetchAll() {
50 $resultSet = $this->getDbTable()->fetchAll();
52 foreach ($resultSet as $row) {
53 $entry = new Syj_Model_User();
54 $this->_itemFromRow($entry, $row);
60 protected function _itemFromRow(Syj_Model_User $item, Zend_Db_Table_Row $row) {
61 $item->setId($row->id)
62 ->setPassword($row->password)
63 ->setPseudo($row->pseudo)
64 ->setEmail($row->email)
65 ->setLang($row->lang);
68 public function save (Syj_Model_User $user) {
70 'pseudo'=> (string) $user->pseudo,
71 'password'=> (string)$user->password,
72 'email'=> (string) $user->email,
73 'lang'=> (string) $user->lang
75 if (null === ($id = $user->getId())) {
76 $user->id = $this->getDbTable()->insert($data);
78 $this->getDbTable()->update($data, array('id = ?' => $id));
80 $user->notifyPendings();
83 public function delete (Syj_Model_User $pending) {
84 if (null !== ($id = $pending->getId())) {
85 $this->getDbTable()->delete(array('id = ?' => $id));