Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5690 | Rev 5692 | 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);
5689 anderson 72
        // $select->where->and->nest()
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
 
5690 anderson 83
        $select->where->and->nest()
5691 anderson 84
            ->nest()
5690 anderson 85
            ->equalTo('sender_id', $user1)
5689 anderson 86
            ->and->equalTo('receiver_id', $user2)
87
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
88
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL);
5671 anderson 89
 
1 www 90
        return $this->executeFetchOneObject($select, $prototype);
91
    }
2339 nelberth 92
 
93
 
94
 
1 www 95
    /**
96
     *
97
     * @param int $user_id
98
     * @return Conversation[]
99
     */
100
    public function fetchAllByUserId($user_id)
101
    {
102
        $prototype = new Conversation();
5671 anderson 103
 
1 www 104
        $select = $this->sql->select(self::_TABLE);
105
        $select->where->and->nest()
5671 anderson 106
            ->nest()
107
            ->equalTo('sender_id', $user_id)
108
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
109
            ->unnest()
110
            ->or->nest()
111
            ->equalTo('receiver_id', $user_id)
112
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
113
            ->unnest()
114
            ->unnest();
115
 
1 www 116
        return $this->executeFetchAllObject($select, $prototype);
117
    }
2339 nelberth 118
 
2350 nelberth 119
 
5671 anderson 120
 
1 www 121
    /**
122
     *
123
     * @param Conversation $conversation
124
     * @return boolean
125
     */
126
    public function insert($conversation)
127
    {
128
        $hydrator = new ObjectPropertyHydrator();
129
        $values = $hydrator->extract($conversation);
130
        $values = $this->removeEmpty($values);
5671 anderson 131
 
1 www 132
        $insert = $this->sql->insert(self::_TABLE);
133
        $insert->values($values);
5671 anderson 134
 
1 www 135
        $response = $this->executeInsert($insert);
5671 anderson 136
        if ($response) {
1 www 137
            $conversation->id = $this->lastInsertId;
138
        }
5671 anderson 139
 
1 www 140
        return $response;
141
    }
5671 anderson 142
 
143
 
1 www 144
    /**
145
     *
146
     * @param Conversation $conversation
147
     * @return boolean
148
     */
5671 anderson 149
    public function update($conversation)
1 www 150
    {
151
        $hydrator = new ObjectPropertyHydrator();
152
        $values = $hydrator->extract($conversation);
153
        $values = $this->removeEmpty($values);
5671 anderson 154
 
1 www 155
        $update = $this->sql->update(self::_TABLE);
156
        $update->set($values);
157
        $update->where->equalTo('id', $conversation->id);
5671 anderson 158
 
1 www 159
        return $this->executeUpdate($update);
160
    }
5671 anderson 161
}