Skip to content

Commit a32c126

Browse files
committed
fix: upgrade doctrine queries
chore: migrate deprecated methods see https://www.doctrine-project.org/2021/03/29/dbal-2.13.html see doctrine/dbal#5556 Change-Id: If58dc2fc5e32799b8ba84fc2454bea7daf2ed77c
1 parent 6e3630c commit a32c126

20 files changed

+360
-239
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php namespace Libs\Utils\Doctrine;
2+
/*
3+
* Copyright 2024 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use Doctrine\DBAL\ParameterType;
15+
use Doctrine\DBAL\Statement;
16+
/**
17+
* Class DoctrineStatementValueBinder
18+
* @package Libs\Utils\Doctrine
19+
*/
20+
final class DoctrineStatementValueBinder
21+
{
22+
/**
23+
* @param $param
24+
* @return int
25+
*/
26+
public static function inferParamType($param):int
27+
{
28+
if(is_int($param)) return ParameterType::INTEGER;
29+
if(is_bool($param)) return ParameterType::BOOLEAN;
30+
if(is_string($param)) return ParameterType::STRING;
31+
if(is_array($param)) return ParameterType::INTEGER;
32+
return ParameterType::STRING;
33+
}
34+
35+
/**
36+
* @param Statement $stmt
37+
* @param array $params
38+
* @return Statement
39+
* @throws \Doctrine\DBAL\Exception
40+
*/
41+
public static function bind(Statement $stmt, array $params):Statement
42+
{
43+
foreach ($params as $key => $value) {
44+
$stmt->bindValue($key, $value, self::inferParamType($value));
45+
}
46+
return $stmt;
47+
}
48+
}

app/Models/Foundation/Main/Member.php

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,13 +1029,11 @@ public function belongsToGroup(string $code): bool
10291029
WHERE MemberID = :member_id AND `Group`.Code = :code
10301030
SQL;
10311031

1032-
$stmt = $this->prepareRawSQL($sql);
1033-
$res = $stmt->execute(
1034-
[
1035-
'member_id' => $this->getId(),
1036-
'code' => trim($code),
1037-
]
1038-
);
1032+
$stmt = $this->prepareRawSQL($sql, [
1033+
'member_id' => $this->getId(),
1034+
'code' => trim($code),
1035+
]);
1036+
$res = $stmt->executeQuery();
10391037
$res = $res->fetchFirstColumn();
10401038
return intval($res[0]) > 0;
10411039
} catch (\Exception $ex) {
@@ -1159,14 +1157,12 @@ public function getFavoritesEventsIds(Summit $summit)
11591157
WHERE MemberID = :member_id AND SummitEvent.Published = 1 AND SummitEvent.SummitID = :summit_id
11601158
SQL;
11611159

1162-
$stmt = $this->prepareRawSQL($sql);
1163-
$res = $stmt->execute(
1164-
[
1165-
'member_id' => $this->getId(),
1166-
'summit_id' => $summit->getId(),
1167-
]
1168-
);
1169-
return $res->fetchAllNumeric();
1160+
$stmt = $this->prepareRawSQL($sql,[
1161+
'member_id' => $this->getId(),
1162+
'summit_id' => $summit->getId(),
1163+
]);
1164+
$res = $stmt->executeQuery();
1165+
return $res->fetchFirstColumn();
11701166
}
11711167

11721168
/**
@@ -1330,14 +1326,12 @@ public function getScheduledEventsIds(Summit $summit)
13301326
WHERE MemberID = :member_id AND SummitEvent.Published = 1 AND SummitEvent.SummitID = :summit_id
13311327
SQL;
13321328

1333-
$stmt = $this->prepareRawSQL($sql);
1334-
$res = $stmt->execute(
1335-
[
1336-
'member_id' => $this->getId(),
1337-
'summit_id' => $summit->getId(),
1338-
]
1339-
);
1340-
return $res->fetchAllNumeric();
1329+
$stmt = $this->prepareRawSQL($sql,[
1330+
'member_id' => $this->getId(),
1331+
'summit_id' => $summit->getId(),
1332+
]);
1333+
$res = $stmt->executeQuery();
1334+
return $res->fetchFirstColumn();
13411335
}
13421336

13431337
/**
@@ -1817,14 +1811,12 @@ public function getSponsorMembershipIds(Summit $summit): array
18171811
WHERE MemberID = :member_id AND Sponsor.SummitID = :summit_id
18181812
SQL;
18191813

1820-
$stmt = $this->prepareRawSQL($sql);
1821-
$res = $stmt->execute(
1822-
[
1823-
'member_id' => $this->getId(),
1824-
'summit_id' => $summit->getId(),
1825-
]
1826-
);
1827-
return $res->fetchAllNumeric();
1814+
$stmt = $this->prepareRawSQL($sql, [
1815+
'member_id' => $this->getId(),
1816+
'summit_id' => $summit->getId(),
1817+
]);
1818+
$res = $stmt->executeQuery();
1819+
return $res->fetchFirstColumn();
18281820
}
18291821

18301822
public function hasSponsorMembershipsFor(Summit $summit, Sponsor $sponsor = null): bool
@@ -1850,8 +1842,8 @@ public function hasSponsorMembershipsFor(Summit $summit, Sponsor $sponsor = null
18501842
$params['sponsor_id'] = $sponsor->getId();
18511843
}
18521844

1853-
$stmt = $this->prepareRawSQL($sql);
1854-
$res = $stmt->execute($params);
1845+
$stmt = $this->prepareRawSQL($sql, $params);
1846+
$res = $stmt->executeQuery();
18551847
$res = $res->fetchFirstColumn();
18561848
return intval($res[0]) > 0;
18571849
} catch (\Exception $ex) {
@@ -2070,14 +2062,14 @@ public function hasPermissionForOnGroup(Summit $summit, string $groupSlug): bool
20702062
SummitAdministratorPermissionGroup_Summits.SummitID = :summit_id
20712063
SQL;
20722064

2073-
$stmt = $this->prepareRawSQL($sql);
2074-
$res = $stmt->execute(
2065+
$stmt = $this->prepareRawSQL($sql,
20752066
[
20762067
'member_id' => $this->getId(),
20772068
'summit_id' => $summit->getId()
20782069
]
20792070
);
2080-
$allowed_summits = $res->fetchAllNumeric();
2071+
$res = $stmt->executeQuery();
2072+
$allowed_summits = $res->fetchFirstColumn();
20812073
return count($allowed_summits) > 0 && $this->isOnGroup($groupSlug);
20822074
}
20832075

@@ -2097,14 +2089,14 @@ public function hasPermissionFor(Summit $summit): bool
20972089
SummitAdministratorPermissionGroup_Summits.SummitID = :summit_id
20982090
SQL;
20992091

2100-
$stmt = $this->prepareRawSQL($sql);
2101-
$res = $stmt->execute(
2092+
$stmt = $this->prepareRawSQL($sql,
21022093
[
21032094
'member_id' => $this->getId(),
21042095
'summit_id' => $summit->getId()
21052096
]
21062097
);
2107-
$allowed_summits = $res->fetchAllNumeric();
2098+
$res = $stmt->executeQuery();
2099+
$allowed_summits = $res->fetchFirstColumn();
21082100
return count($allowed_summits) > 0;
21092101
}
21102102

@@ -2125,16 +2117,14 @@ public function getPaidSummitTicketsIds(Summit $summit)
21252117
SummitAttendeeTicket.Status = :ticket_status AND SummitAttendeeTicket.IsActive = 1
21262118
SQL;
21272119

2128-
$stmt = $this->prepareRawSQL($sql);
2129-
$res = $stmt->execute(
2130-
[
2131-
'member_id' => $this->getId(),
2132-
'member_email' => $this->email,
2133-
'ticket_status' => IOrderConstants::PaidStatus,
2134-
'summit_id' => $summit->getId(),
2135-
]
2136-
);
2137-
return $res->fetchAllNumeric();
2120+
$stmt = $this->prepareRawSQL($sql, [
2121+
'member_id' => $this->getId(),
2122+
'member_email' => $this->email,
2123+
'ticket_status' => IOrderConstants::PaidStatus,
2124+
'summit_id' => $summit->getId(),
2125+
]);
2126+
$res = $stmt->executeQuery();
2127+
return $res->fetchFirstColumn();
21382128
}
21392129

21402130
/**
@@ -2395,11 +2385,11 @@ public function getElectionApplicationsCountFor(Election $election): int
23952385
WHERE C.ElectionID = :election_id AND
23962386
C.CandidateID = :candidate_id
23972387
SQL;
2398-
$stmt = $this->prepareRawSQL($sql);
2399-
$res = $stmt->execute([
2388+
$stmt = $this->prepareRawSQL($sql, [
24002389
'election_id' => $election->getId(),
24012390
'candidate_id' => $this->id
24022391
]);
2392+
$res = $stmt->executeQuery();
24032393
$res = $res->fetchFirstColumn();
24042394
return count($res) > 0 ? $res[0] : 0;
24052395
} catch (\Exception $ex) {

app/Models/Foundation/Main/Strategies/MemberSummitStrategy.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php namespace App\Models\Foundation\Main\Strategies;
2-
32
/**
43
* Copyright 2024 OpenStack Foundation
54
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,11 +11,14 @@
1211
* See the License for the specific language governing permissions and
1312
* limitations under the License.
1413
**/
15-
1614
use LaravelDoctrine\ORM\Facades\Registry;
15+
use Libs\Utils\Doctrine\DoctrineStatementValueBinder;
1716
use models\summit\Summit;
1817
use models\utils\SilverstripeBaseModel;
19-
18+
/**
19+
* Class MemberSummitStrategy
20+
* @package App\Models\Foundation\Main\Strategies
21+
*/
2022
class MemberSummitStrategy implements IMemberSummitStrategy
2123
{
2224
private $member_id;
@@ -44,13 +46,15 @@ public function getAllAllowedSummitIds(): array
4446
WHERE SummitAdministratorPermissionGroup_Members.MemberID = :member_id
4547
SQL;
4648

47-
$stmt = $em->getConnection()->prepare($sql);
48-
$res = $stmt->execute(
49+
$stmt = DoctrineStatementValueBinder::bind(
50+
$em->getConnection()->prepare($sql),
4951
[
5052
'member_id' => $this->member_id,
5153
]
5254
);
53-
return $res->fetchAllNumeric();
55+
56+
$res = $stmt->executeQuery();
57+
return $res->fetchFirstColumn();
5458
}
5559

5660
/**
@@ -71,13 +75,14 @@ public function isSummitAllowed(Summit $summit): bool
7175
AND SummitAdministratorPermissionGroup_Summits.SummitID = :summit_id
7276
SQL;
7377

74-
$stmt = $em->getConnection()->prepare($sql);
75-
$res = $stmt->execute(
78+
$stmt = DoctrineStatementValueBinder::bind(
79+
$em->getConnection()->prepare($sql),
7680
[
7781
'member_id' => $this->member_id,
7882
'summit_id' => $summit->getId(),
7983
]
8084
);
85+
$res = $stmt->executeQuery();
8186
$res = $res->fetchFirstColumn();
8287
return intval($res[0]) > 0;
8388
} catch (\Exception $ex) {

app/Models/Foundation/Main/Strategies/SponsorMemberSummitStrategy.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
<?php namespace App\Models\Foundation\Main\Strategies;
2-
3-
use LaravelDoctrine\ORM\Facades\Registry;
4-
use models\summit\Summit;
5-
use models\utils\SilverstripeBaseModel;
6-
72
/**
83
* Copyright 2024 OpenStack Foundation
94
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +11,14 @@
1611
* See the License for the specific language governing permissions and
1712
* limitations under the License.
1813
**/
19-
14+
use LaravelDoctrine\ORM\Facades\Registry;
15+
use Libs\Utils\Doctrine\DoctrineStatementValueBinder;
16+
use models\summit\Summit;
17+
use models\utils\SilverstripeBaseModel;
18+
/**
19+
* Class SponsorMemberSummitStrategy
20+
* @package App\Models\Foundation\Main\Strategies
21+
*/
2022
class SponsorMemberSummitStrategy implements IMemberSummitStrategy
2123
{
2224
private $member_id;
@@ -42,13 +44,14 @@ public function getAllAllowedSummitIds(): array
4244
WHERE Sponsor_Users.MemberID = :member_id;
4345
SQL;
4446

45-
$stmt = $em->getConnection()->prepare($sql);
46-
$res = $stmt->execute(
47+
$stmt = DoctrineStatementValueBinder::bind(
48+
$em->getConnection()->prepare($sql),
4749
[
4850
'member_id' => $this->member_id,
4951
]
5052
);
51-
return $res->fetchAllNumeric();
53+
$res = $stmt->executeQuery();
54+
return $res->fetchFirstColumn();
5255
}
5356

5457
/**
@@ -67,13 +70,14 @@ public function isSummitAllowed(Summit $summit): bool
6770
AND Sponsor.SummitID = :summit_id
6871
SQL;
6972

70-
$stmt = $em->getConnection()->prepare($sql);
71-
$res = $stmt->execute(
73+
$stmt = DoctrineStatementValueBinder::bind(
74+
$em->getConnection()->prepare($sql),
7275
[
7376
'member_id' => $this->member_id,
7477
'summit_id' => $summit->getId(),
7578
]
7679
);
80+
$res = $stmt->executeQuery();
7781
$res = $res->fetchFirstColumn();
7882
return intval($res[0]) > 0;
7983
} catch (\Exception $ex) {

app/Models/Foundation/Main/SummitAdministratorPermissionGroup.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,11 @@ public function getMembersIds(): array
116116
WHERE SummitAdministratorPermissionGroup_Members.SummitAdministratorPermissionGroupID = :group_id
117117
SQL;
118118

119-
$stmt = $this->prepareRawSQL($sql);
120-
$res = $stmt->execute(
121-
[
122-
'group_id' => $this->getId(),
123-
]
124-
);
125-
return $res->fetchAllNumeric();
126-
119+
$stmt = $this->prepareRawSQL($sql, [
120+
'group_id' => $this->id,
121+
]);
122+
$res = $stmt->executeQuery();
123+
return $res->fetchFirstColumn();
127124
}
128125

129126
/**
@@ -157,13 +154,11 @@ public function getSummitsIds(): array
157154
WHERE SummitAdministratorPermissionGroup_Summits.SummitAdministratorPermissionGroupID = :group_id;
158155
SQL;
159156

160-
$stmt = $this->prepareRawSQL($sql);
161-
$res = $stmt->execute(
162-
[
163-
'group_id' => $this->getId(),
164-
]
165-
);
166-
return $res->fetchAllNumeric();
157+
$stmt = $this->prepareRawSQL($sql, [
158+
'group_id' => $this->id,
159+
]);
160+
$res = $stmt->executeQuery();
161+
return $res->fetchFirstColumn();
167162

168163
}
169164

0 commit comments

Comments
 (0)