| @@ 48-90 (lines=43) @@ | ||
| 45 | * @param bool|int $deleted |
|
| 46 | * @return Notebook[]|Notebook |
|
| 47 | */ |
|
| 48 | public function find($notebook_id = null, $user_id = null, $deleted = false) { |
|
| 49 | $qb = $this->db->getQueryBuilder(); |
|
| 50 | $qb->select('g.*', 'g.guid as guid')//'COUNT(n.id) as note_count' |
|
| 51 | ->selectAlias($qb->createFunction('COUNT(' . $qb->getColumnName('n.id') . ')'), 'note_count') |
|
| 52 | ->from('nextnote_groups', 'g') |
|
| 53 | ->leftJoin('g', 'nextnote_notes', 'n', $qb->expr()->eq('g.id', 'n.notebook'))->groupBy(['g.id']); |
|
| 54 | ||
| 55 | $where = []; |
|
| 56 | if (!is_null($notebook_id)) { |
|
| 57 | $where['g.id'] = $notebook_id; |
|
| 58 | } |
|
| 59 | ||
| 60 | if ($user_id !== null) { |
|
| 61 | $where['g.uid'] = $user_id; |
|
| 62 | } |
|
| 63 | ||
| 64 | if ($deleted !== false) { |
|
| 65 | $where['g.deleted'] = $deleted; |
|
| 66 | } |
|
| 67 | $i = 0; |
|
| 68 | foreach ($where as $field => $value) { |
|
| 69 | if ($i === 0) { |
|
| 70 | $qb->where($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
|
| 71 | } else { |
|
| 72 | $qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
|
| 73 | } |
|
| 74 | $i++; |
|
| 75 | } |
|
| 76 | ||
| 77 | $result = $qb->execute(); |
|
| 78 | /** |
|
| 79 | * @var $results Notebook[] |
|
| 80 | */ |
|
| 81 | $results = []; |
|
| 82 | while ($item = $result->fetch()) { |
|
| 83 | $results[] = $this->makeEntityFromDBResult($item); |
|
| 84 | } |
|
| 85 | $result->closeCursor(); |
|
| 86 | if (count($results) === 1) { |
|
| 87 | return reset($results); |
|
| 88 | } |
|
| 89 | return $results; |
|
| 90 | } |
|
| 91 | ||
| 92 | /** |
|
| 93 | * @param $group_name |
|
| @@ 98-141 (lines=44) @@ | ||
| 95 | * @param bool $deleted |
|
| 96 | * @return Notebook[]|Notebook |
|
| 97 | */ |
|
| 98 | public function findByName($group_name, $user_id = null, $deleted = false) { |
|
| 99 | $qb = $this->db->getQueryBuilder(); |
|
| 100 | $qb->select('g.*', 'g.guid as guid')//'COUNT(n.id) as note_count' |
|
| 101 | ->selectAlias($qb->createFunction('COUNT(' . $qb->getColumnName('n.id') . ')'), 'note_count') |
|
| 102 | ->from('nextnote_groups', 'g') |
|
| 103 | ->leftJoin('g', 'nextnote_notes', 'n', $qb->expr()->eq('g.id', 'n.notebook'))->groupBy(['g.id']); |
|
| 104 | ||
| 105 | $where = []; |
|
| 106 | if ($group_name) { |
|
| 107 | $where['g.name'] = $group_name; |
|
| 108 | } |
|
| 109 | ||
| 110 | if ($user_id !== null) { |
|
| 111 | $where['g.uid'] = $user_id; |
|
| 112 | } |
|
| 113 | ||
| 114 | if ($deleted !== false) { |
|
| 115 | $where['g.deleted'] = $deleted; |
|
| 116 | } |
|
| 117 | $i = 0; |
|
| 118 | foreach ($where as $field => $value) { |
|
| 119 | if ($i === 0) { |
|
| 120 | $qb->where($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
|
| 121 | } else { |
|
| 122 | $qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
|
| 123 | } |
|
| 124 | $i++; |
|
| 125 | } |
|
| 126 | ||
| 127 | $result = $qb->execute(); |
|
| 128 | ||
| 129 | /** |
|
| 130 | * @var $results Notebook[] |
|
| 131 | */ |
|
| 132 | $results = []; |
|
| 133 | while ($item = $result->fetch()) { |
|
| 134 | $results[] = $this->makeEntityFromDBResult($item); |
|
| 135 | } |
|
| 136 | $result->closeCursor(); |
|
| 137 | if (count($results) === 1) { |
|
| 138 | return reset($results); |
|
| 139 | } |
|
| 140 | return $results; |
|
| 141 | } |
|
| 142 | ||
| 143 | /** |
|
| 144 | * Creates a group |
|