Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5673 | Rev 5675 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
5671 anderson 8
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
1 www 9
use LeadersLinked\Mapper\Common\MapperCommon;
10
use LeadersLinked\Model\Conversation;
11
 
12
class ConversationMapper extends MapperCommon
13
{
14
    const _TABLE = 'tbl_conversations';
15
 
5671 anderson 16
 
1 www 17
    /**
18
     *
19
     * @var ConversationMapper
20
     */
21
    private static $_instance;
5671 anderson 22
 
1 www 23
    /**
24
     *
25
     * @param AdapterInterface $adapter
26
     */
27
    private function __construct($adapter)
28
    {
29
        parent::__construct($adapter);
30
    }
5671 anderson 31
 
1 www 32
    /**
33
     *
34
     * @param AdapterInterface $adapter
35
     * @return ConversationMapper
36
     */
37
    public static function getInstance($adapter)
38
    {
5671 anderson 39
        if (self::$_instance == null) {
1 www 40
            self::$_instance = new ConversationMapper($adapter);
41
        }
42
        return self::$_instance;
43
    }
44
 
5671 anderson 45
 
1 www 46
    /**
47
     *
48
     * @param int $id
49
     * @return Conversation
50
     */
51
    public function fetchOne($id)
52
    {
53
        $select = $this->sql->select(self::_TABLE);
54
        $select->where->equalTo('id', $id);
5671 anderson 55
 
1 www 56
        $prototype = new Conversation();
57
        return $this->executeFetchOneObject($select, $prototype);
58
    }
5671 anderson 59
 
60
 
61
 
62
 
1 www 63
    /**
64
     * @param int $user1
65
     * @param int $user2
66
     * @return Conversation
67
     */
68
    public function fetchOneByUserId1AndUserId2($user1, $user2)
69
    {
70
        $prototype = new Conversation();
71
        $select = $this->sql->select(self::_TABLE);
72
        $select->where->and->nest()
5671 anderson 73
            ->nest()
74
            ->equalTo('sender_id', $user1)
75
            ->and->equalTo('receiver_id', $user2)
76
            ->unnest()
77
            ->or->nest()
78
            ->equalTo('receiver_id', $user1)
79
            ->and->equalTo('sender_id', $user2)
80
            ->unnest()
81
            ->unnest();
82
 
5674 anderson 83
        echo "<pre></pre>";
84
        print_r($select);
85
        echo "</pre>";
86
 
1 www 87
        return $this->executeFetchOneObject($select, $prototype);
88
    }
2339 nelberth 89
 
90
 
91
 
1 www 92
    /**
93
     *
94
     * @param int $user_id
95
     * @return Conversation[]
96
     */
97
    public function fetchAllByUserId($user_id)
98
    {
99
        $prototype = new Conversation();
5671 anderson 100
 
1 www 101
        $select = $this->sql->select(self::_TABLE);
102
        $select->where->and->nest()
5671 anderson 103
            ->nest()
104
            ->equalTo('sender_id', $user_id)
105
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
106
            ->unnest()
107
            ->or->nest()
108
            ->equalTo('receiver_id', $user_id)
109
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
110
            ->unnest()
111
            ->unnest();
112
 
1 www 113
        return $this->executeFetchAllObject($select, $prototype);
114
    }
2339 nelberth 115
 
2350 nelberth 116
 
5671 anderson 117
 
1 www 118
    /**
119
     *
120
     * @param Conversation $conversation
121
     * @return boolean
122
     */
123
    public function insert($conversation)
124
    {
125
        $hydrator = new ObjectPropertyHydrator();
126
        $values = $hydrator->extract($conversation);
127
        $values = $this->removeEmpty($values);
5671 anderson 128
 
1 www 129
        $insert = $this->sql->insert(self::_TABLE);
130
        $insert->values($values);
5671 anderson 131
 
1 www 132
        $response = $this->executeInsert($insert);
5671 anderson 133
        if ($response) {
1 www 134
            $conversation->id = $this->lastInsertId;
135
        }
5671 anderson 136
 
1 www 137
        return $response;
138
    }
5671 anderson 139
 
140
 
1 www 141
    /**
142
     *
143
     * @param Conversation $conversation
144
     * @return boolean
145
     */
5671 anderson 146
    public function update($conversation)
1 www 147
    {
148
        $hydrator = new ObjectPropertyHydrator();
149
        $values = $hydrator->extract($conversation);
150
        $values = $this->removeEmpty($values);
5671 anderson 151
 
1 www 152
        $update = $this->sql->update(self::_TABLE);
153
        $update->set($values);
154
        $update->where->equalTo('id', $conversation->id);
5671 anderson 155
 
1 www 156
        return $this->executeUpdate($update);
157
    }
5671 anderson 158
}