Skip to content

Commit a2ff26a

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 3839ae3 commit a2ff26a

21 files changed

+362
-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
@@ -978,13 +978,11 @@ public function belongsToGroup(string $code): bool
978978
WHERE MemberID = :member_id AND `Group`.Code = :code
979979
SQL;
980980

981-
$stmt = $this->prepareRawSQL($sql);
982-
$res = $stmt->execute(
983-
[
984-
'member_id' => $this->getId(),
985-
'code' => trim($code),
986-
]
987-
);
981+
$stmt = $this->prepareRawSQL($sql, [
982+
'member_id' => $this->getId(),
983+
'code' => trim($code),
984+
]);
985+
$res = $stmt->executeQuery();
988986
$res = $res->fetchFirstColumn();
989987
return intval($res[0]) > 0;
990988
} catch (\Exception $ex) {
@@ -1108,14 +1106,12 @@ public function getFavoritesEventsIds(Summit $summit)
11081106
WHERE MemberID = :member_id AND SummitEvent.Published = 1 AND SummitEvent.SummitID = :summit_id
11091107
SQL;
11101108

1111-
$stmt = $this->prepareRawSQL($sql);
1112-
$res = $stmt->execute(
1113-
[
1114-
'member_id' => $this->getId(),
1115-
'summit_id' => $summit->getId(),
1116-
]
1117-
);
1118-
return $res->fetchAllNumeric();
1109+
$stmt = $this->prepareRawSQL($sql,[
1110+
'member_id' => $this->getId(),
1111+
'summit_id' => $summit->getId(),
1112+
]);
1113+
$res = $stmt->executeQuery();
1114+
return $res->fetchFirstColumn();
11191115
}
11201116

11211117
/**
@@ -1279,14 +1275,12 @@ public function getScheduledEventsIds(Summit $summit)
12791275
WHERE MemberID = :member_id AND SummitEvent.Published = 1 AND SummitEvent.SummitID = :summit_id
12801276
SQL;
12811277

1282-
$stmt = $this->prepareRawSQL($sql);
1283-
$res = $stmt->execute(
1284-
[
1285-
'member_id' => $this->getId(),
1286-
'summit_id' => $summit->getId(),
1287-
]
1288-
);
1289-
return $res->fetchAllNumeric();
1278+
$stmt = $this->prepareRawSQL($sql,[
1279+
'member_id' => $this->getId(),
1280+
'summit_id' => $summit->getId(),
1281+
]);
1282+
$res = $stmt->executeQuery();
1283+
return $res->fetchFirstColumn();
12901284
}
12911285

12921286
/**
@@ -1766,14 +1760,12 @@ public function getSponsorMembershipIds(Summit $summit): array
17661760
WHERE MemberID = :member_id AND Sponsor.SummitID = :summit_id
17671761
SQL;
17681762

1769-
$stmt = $this->prepareRawSQL($sql);
1770-
$res = $stmt->execute(
1771-
[
1772-
'member_id' => $this->getId(),
1773-
'summit_id' => $summit->getId(),
1774-
]
1775-
);
1776-
return $res->fetchAllNumeric();
1763+
$stmt = $this->prepareRawSQL($sql, [
1764+
'member_id' => $this->getId(),
1765+
'summit_id' => $summit->getId(),
1766+
]);
1767+
$res = $stmt->executeQuery();
1768+
return $res->fetchFirstColumn();
17771769
}
17781770

17791771
public function hasSponsorMembershipsFor(Summit $summit, Sponsor $sponsor = null): bool
@@ -1799,8 +1791,8 @@ public function hasSponsorMembershipsFor(Summit $summit, Sponsor $sponsor = null
17991791
$params['sponsor_id'] = $sponsor->getId();
18001792
}
18011793

1802-
$stmt = $this->prepareRawSQL($sql);
1803-
$res = $stmt->execute($params);
1794+
$stmt = $this->prepareRawSQL($sql, $params);
1795+
$res = $stmt->executeQuery();
18041796
$res = $res->fetchFirstColumn();
18051797
return intval($res[0]) > 0;
18061798
} catch (\Exception $ex) {
@@ -2019,14 +2011,14 @@ public function hasPermissionForOnGroup(Summit $summit, string $groupSlug): bool
20192011
SummitAdministratorPermissionGroup_Summits.SummitID = :summit_id
20202012
SQL;
20212013

2022-
$stmt = $this->prepareRawSQL($sql);
2023-
$res = $stmt->execute(
2014+
$stmt = $this->prepareRawSQL($sql,
20242015
[
20252016
'member_id' => $this->getId(),
20262017
'summit_id' => $summit->getId()
20272018
]
20282019
);
2029-
$allowed_summits = $res->fetchAllNumeric();
2020+
$res = $stmt->executeQuery();
2021+
$allowed_summits = $res->fetchFirstColumn();
20302022
return count($allowed_summits) > 0 && $this->isOnGroup($groupSlug);
20312023
}
20322024

@@ -2046,14 +2038,14 @@ public function hasPermissionFor(Summit $summit): bool
20462038
SummitAdministratorPermissionGroup_Summits.SummitID = :summit_id
20472039
SQL;
20482040

2049-
$stmt = $this->prepareRawSQL($sql);
2050-
$res = $stmt->execute(
2041+
$stmt = $this->prepareRawSQL($sql,
20512042
[
20522043
'member_id' => $this->getId(),
20532044
'summit_id' => $summit->getId()
20542045
]
20552046
);
2056-
$allowed_summits = $res->fetchAllNumeric();
2047+
$res = $stmt->executeQuery();
2048+
$allowed_summits = $res->fetchFirstColumn();
20572049
return count($allowed_summits) > 0;
20582050
}
20592051

@@ -2074,16 +2066,14 @@ public function getPaidSummitTicketsIds(Summit $summit)
20742066
SummitAttendeeTicket.Status = :ticket_status AND SummitAttendeeTicket.IsActive = 1
20752067
SQL;
20762068

2077-
$stmt = $this->prepareRawSQL($sql);
2078-
$res = $stmt->execute(
2079-
[
2080-
'member_id' => $this->getId(),
2081-
'member_email' => $this->email,
2082-
'ticket_status' => IOrderConstants::PaidStatus,
2083-
'summit_id' => $summit->getId(),
2084-
]
2085-
);
2086-
return $res->fetchAllNumeric();
2069+
$stmt = $this->prepareRawSQL($sql, [
2070+
'member_id' => $this->getId(),
2071+
'member_email' => $this->email,
2072+
'ticket_status' => IOrderConstants::PaidStatus,
2073+
'summit_id' => $summit->getId(),
2074+
]);
2075+
$res = $stmt->executeQuery();
2076+
return $res->fetchFirstColumn();
20872077
}
20882078

20892079
/**
@@ -2344,11 +2334,11 @@ public function getElectionApplicationsCountFor(Election $election): int
23442334
WHERE C.ElectionID = :election_id AND
23452335
C.CandidateID = :candidate_id
23462336
SQL;
2347-
$stmt = $this->prepareRawSQL($sql);
2348-
$res = $stmt->execute([
2337+
$stmt = $this->prepareRawSQL($sql, [
23492338
'election_id' => $election->getId(),
23502339
'candidate_id' => $this->id
23512340
]);
2341+
$res = $stmt->executeQuery();
23522342
$res = $res->fetchFirstColumn();
23532343
return count($res) > 0 ? $res[0] : 0;
23542344
} 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)