]> dev.renevier.net Git - syj.git/blob - application/models/UserMapper.php
version 0.1
[syj.git] / application / models / UserMapper.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class Syj_Model_UserMapper
6 {
7     protected $_dbTable;
8     protected $_tableInfos = array('name' => 'users');
9
10     public function getDbTable() {
11         if (null === $this->_dbTable) {
12             $this->_dbTable = new Zend_Db_Table($this->_tableInfos);
13         }
14         return $this->_dbTable;
15     }
16
17     public function find($id, Syj_Model_User $user) {
18         $result = $this->getDbTable()->find((int)$id);
19         if (0 == count($result)) {
20             return false;
21         }
22         $row = $result->current();
23         $this->_itemFromRow($user, $row);
24         return true;
25     }
26
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);
31         if (!$row) {
32             return false;
33         }
34         $this->_itemFromRow($user, $row);
35         return true;
36     }
37
38     public function fetchAll() {
39         $resultSet = $this->getDbTable()->fetchAll();
40         $entries   = array();
41         foreach ($resultSet as $row) {
42             $entry = new Syj_Model_User();
43             $this->_itemFromRow($entry, $row);
44             $entries[] = $entry;
45         }
46         return $entries;
47     }
48
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)
54             ->setLang($row->lang)
55             ->setCreationAddr($row->creation_addr);
56     }
57
58     public function save (Syj_Model_User $user) {
59         $data = array(
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
65         );
66         if (null === ($id = $user->getId())) {
67             $user->id = $this->getDbTable()->insert($data);
68         } else {
69             $this->getDbTable()->update($data, array('id = ?' => $id));
70         }
71         $user->notifyPendings();
72     }
73
74     public function delete (Syj_Model_User $pending) {
75          if (null !== ($id = $pending->getId())) {
76             $this->getDbTable()->delete(array('id = ?' => $id));
77          }
78     }
79 }
80