@@ -19,12 +19,13 @@ public function __construct(\PDO $pdo)
19
19
public function store (array $ ads ): void
20
20
{
21
21
$ stmt = $ this ->pdo ->prepare (
22
- 'insert into ad (id, name, cost, impressions, clicks, cpm, cpc, ctr, source, date)
23
- values (:id, :name, :cost, :impressions, :clicks, :cpm, :cpc, :ctr, :source, :date) '
22
+ 'insert into ad (id, account_id, name, cost, impressions, clicks, cpm, cpc, ctr, source, date)
23
+ values (:id, :account_id, : name, :cost, :impressions, :clicks, :cpm, :cpc, :ctr, :source, :date) '
24
24
);
25
25
26
26
foreach ($ ads as $ ad ) {
27
27
$ stmt ->bindValue ('id ' , $ ad ->id );
28
+ $ stmt ->bindValue ('account_id ' , $ ad ->account ->id );
28
29
$ stmt ->bindValue ('name ' , $ ad ->name );
29
30
$ stmt ->bindValue ('cost ' , $ ad ->cost );
30
31
$ stmt ->bindValue ('impressions ' , $ ad ->impressions , \PDO ::PARAM_INT );
@@ -33,7 +34,7 @@ public function store(array $ads): void
33
34
$ stmt ->bindValue ('cpc ' , $ ad ->cpc );
34
35
$ stmt ->bindValue ('ctr ' , $ ad ->ctr );
35
36
$ stmt ->bindValue ('source ' , $ ad ->source );
36
- $ stmt ->bindValue ('date ' , $ ad ->date -> format ('Y-m-d ' ));
37
+ $ stmt ->bindValue ('date ' , $ ad ->date instanceof DateTime ? $ ad -> date -> format ('Y-m-d ' ) : $ ad -> date ); // Type-safety!!!
37
38
38
39
$ stmt ->execute ();
39
40
}
@@ -44,6 +45,7 @@ public function store(array $ads): void
44
45
*/
45
46
public function items (): array
46
47
{
48
+ /** @var array<int, Ad>|false $results */
47
49
$ results = $ this ->pdo ->query ('select * from ad order by timestamp desc ' )
48
50
->fetchAll (\PDO ::FETCH_CLASS , Ad::class);
49
51
@@ -64,6 +66,14 @@ public function drop(string $source, DateTime $date): void
64
66
*/
65
67
public function accounts (): array
66
68
{
67
- return $ this ->pdo ->query ('select * from account ' )->fetchAll (\PDO ::FETCH_CLASS , Account::class);
69
+ /** @var array<int, Account>|false $results */
70
+ $ results = $ this ->pdo ->query ('select * from account ' )
71
+ ->fetchAll (\PDO ::FETCH_CLASS , Account::class);
72
+
73
+ if ($ results === false ) {
74
+ return [];
75
+ }
76
+
77
+ return $ results ;
68
78
}
69
79
}
0 commit comments