forked from CakeDC/cakephp-phpstan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesTable.php
More file actions
176 lines (160 loc) · 5.3 KB
/
NotesTable.php
File metadata and controls
176 lines (160 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
/**
* Copyright 2023, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2023, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace App\Model\Table;
use App\Model\Entity\Note;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\Table;
/**
* @method \App\Model\Entity\Note|\Cake\Datasource\EntityInterface get(mixed $primaryKey, string[]|string $finder = 'all',\Psr\SimpleCache\CacheInterface|string|null $cache = null,\Closure|string|null $cacheKey = null, mixed ...$args)
* @property \App\Model\Table\VeryCustomize00009ArticlesTable&\Cake\ORM\Association\HasMany $VeryCustomize00009Articles
* @property \Cake\ORM\Association\BelongsTo<\App\Model\Table\UsersTable> $Users
* @property \Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable $MyUsers
*/
class NotesTable extends Table
{
/**
* @inheritDoc
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('notes');
$this->setDisplayField('note');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', ['dependent' => true, 'className' => MyUsersTable::class]);
$this->belongsTo('MyUsers', [
'dependent' => true,
]);
}
/**
* @return string[]
*/
public function warning(): array
{
$user = $this->MyUsers->get(1);
$user->name = 'John';
$this->MyUsers->logLastLogin($user);
$article = $this->MyUsers->Articles->newSample();
$article->id = '002';
// Test magic findBy methods on association chains (fixes issue #51)
$articleQuery = $this->MyUsers->Articles->findByTitle('Test Title');
$foundArticle = $articleQuery->first();
// Test findBy with And operator
$articleAndQuery = $this->MyUsers->Articles->findByTitleAndActive('Test', true);
// Test findBy with Or operator
$articleOrQuery = $this->MyUsers->Articles->findByTitleOrActive('Test', true);
$entity = $this->get(10, cache: 'my_cache');
if ($entity->note === 'Test') {
$entity = $this->newEmptyEntity();
$entity->user = $user;
$entity = $this->patchEntity($entity, ['note' => 'My Warning new']);
$entity->user_id = 1;
$this->Users->find('all', order: ['Users.id' => 'DESC'], limit: 12);
$entity = $this->saveOrFail($entity);
}
$this->find('optionsPacked');
$this->find(
'twoArgsButNotLegacy',
sort: ['Notes.note' => 'ASC'],
myType: 'featured'
);
$this->find('argsPacked');
return [
'type' => 'warning',
'note' => $entity->note,
];
}
/**
* @param \Cake\ORM\Query\SelectQuery $query
* @param string|int $year
* @param bool $fun
* @return \Cake\ORM\Query\SelectQuery
*/
public function findFeatured(SelectQuery $query, string|int $year, bool $fun): SelectQuery
{
$where = [
'year <=' => $year,
];
if ($fun === true) {
$where[] = $query->newExpr()->in(
'type',
['funny_stuff', 'funny_songs', 'funny_messages']
);
}
return $query->where($where)
->orderBy(['Notes.created' => 'DESC']);
}
/**
* @param \Cake\ORM\Query\SelectQuery $query
* @param string $myType
* @return \Cake\ORM\Query\SelectQuery
*/
public function findTwoArgsButNotLegacy(SelectQuery $query, string $myType): SelectQuery
{
$where = [
'year <=' => 2010,
'type' => $myType,
];
return $query->where($where)
->orderBy(['Notes.created' => 'DESC']);
}
/**
* @param \Cake\ORM\Query\SelectQuery $query
* @param array<string, mixed> $options
* @return \Cake\ORM\Query\SelectQuery
*/
public function findLegacy(SelectQuery $query, array $options): SelectQuery
{
return $query
->where([
'type' => $options['type'] ?? 'normal',
'active' => $options['active'] ?? false,
]);
}
/**
* @param \Cake\ORM\Query\SelectQuery $query
* @param mixed ...$options
* @return \Cake\ORM\Query\SelectQuery
*/
public function findOptionsPacked(SelectQuery $query, mixed ...$options): SelectQuery
{
return $query->select([
$options['labelField'] ?? 'note',
]);
}
/**
* @param \Cake\ORM\Query\SelectQuery $query
* @param mixed ...$args
* @return \Cake\ORM\Query\SelectQuery
*/
public function findArgsPacked(SelectQuery $query, mixed ...$args): SelectQuery
{
return $query->select([
$args['groupLabel'] ?? 'note',
]);
}
/**
* @return string
*/
public function getTypeTestTwoArgsButNotLegacy(): string
{
return 'myType';
}
/**
* @return iterable<\App\Model\Entity\Note>
*/
public function iterableItems(): iterable
{
return [new Note(), new Note()];
}
}