Skip to content

Commit 8973a2f

Browse files
authored
fix type (#114)
1 parent e533a31 commit 8973a2f

18 files changed

+116
-48
lines changed

UPGRADE.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Model::delete_all()](#modeldelete_all)
1010
- [Model::update_all()](#modelupdate_all)
1111
- [Model::find_all_by_...()](#modelfind_all_by_attribute)
12+
- [Model::table()](#modeltable)
1213

1314
#### static properties
1415

@@ -18,6 +19,8 @@
1819
- [Model::$validates_inclusion_of](#modelvalidates_inclusion_of)
1920

2021
#### other changes
22+
- [Table::delete](#tabledelete)
23+
- [Table::update](#tableupdate)
2124
- [Config::set_model_directory](#configset_model_directory)
2225
- [exceptions location](#exceptions-location)
2326

@@ -129,6 +132,22 @@ $books = Book::find_all_by_title('Ubik');
129132
$books = Book::where('title = ?', 'Ubik')->to_a();
130133
```
131134

135+
## `Model::table`
136+
The static `table` accessor on `Model` is now protected. If you were making calls directly on `Table`, you will need to refactor your code.
137+
```php
138+
// 1.x
139+
Book::table()->update($attributes, $where);
140+
141+
// 2.0
142+
Book::where($where)->update_all($attributes);
143+
```
144+
145+
If you do need access to the table instance for some reason, you can still get to it:
146+
```php
147+
$table = Table::load(Book::class);
148+
```
149+
150+
132151
# static properties
133152

134153
The static relationship properties have changed shape, moving from a flat array to a key-config format:
@@ -238,6 +257,36 @@ class Book extends ActiveRecord
238257

239258
# other changes
240259

260+
## `Table::update`
261+
You generally shouldn't be working directly with a `Table` instance, but if you are you should be aware that the `update` method has changed shape:
262+
```php
263+
// 1.x
264+
$table = Book::table();
265+
$table->update([ 'title' => 'Walden` ], ['author_id` => 1]);
266+
267+
// 2.0
268+
$table = Table::load(Book::class);
269+
$options = [
270+
'conditions' => [new WhereClause(['author_id` => 1])]
271+
];
272+
$table->update([ 'title' => 'Walden' ], $options); // where $options is a RelationOptions.
273+
```
274+
275+
## `Table::delete`
276+
You generally shouldn't be working directly with a `Table` instance, but if you are you should be aware that the `delete` method has changed shape:
277+
```php
278+
// 1.x
279+
$table = Book::table();
280+
$table->delete(['author_id' => 1]);
281+
282+
// 2.0
283+
$table = Table::load(Book::class);
284+
$options = [
285+
'conditions' => [new WhereClause(['author_id` => 1])]
286+
];
287+
$table->delete($options); // where $options is a RelationOptions.
288+
```
289+
241290
## `Config::set_model_directory`
242291

243292
`Config::set_model_directory` has been removed, meaning that the active record library no longer maintains its own autoloader or knowledge of where your models are kept. In 2.0 it is recommended to use your own autoloader to manage your models as you would any other classes in your project.

lib/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ public static function reestablish_connection()
908908
*
909909
* @return Table
910910
*/
911-
public static function table()
911+
protected static function table()
912912
{
913913
$table = Table::load(get_called_class());
914914

lib/Relationship/AbstractRelationship.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,15 @@ protected function query_and_attach_related_models_eagerly(Table $table, array $
157157
$class = $this->options['namespace'] . '\\' . $class;
158158
}
159159

160-
$through_table = $class::table();
160+
assert(class_exists($class));
161+
$through_table = Table::load($class);
161162
} else {
162163
$class = $options['class_name'];
163164
if (isset($this->options['namespace']) && !class_exists($class)) {
164165
$class = $this->options['namespace'] . '\\' . $class;
165166
}
166-
$relation = $class::table()->get_relationship($options['through']);
167+
$relation = Table::load($class)->get_relationship($options['through']);
168+
assert(!is_null($relation));
167169
$through_table = $relation->get_table();
168170
}
169171
$options['joins'] = $this->construct_inner_join_sql($through_table, true);

lib/Relationship/HasAndBelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function load(Model $model): mixed
4747
*/
4848
$rel = new Relation($this->class_name, [], []);
4949
$rel->from($this->attribute_name);
50-
$other_table = $model->table()->table;
50+
$other_table = Table::load(get_class($model))->table;
5151
$rel->where($other_table . '. ' . $this->options['foreign_key'] . ' = ?', $model->{$model->get_primary_key()});
5252
$rel->joins([$other_table]);
5353

lib/Relationship/HasMany.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ public function load(Model $model): mixed
154154
$this->set_keys($this->get_table()->class->getName(), true);
155155

156156
$class = $this->class_name;
157-
$relation = $class::table()->get_relationship($this->through);
157+
$relation = Table::load($class)->get_relationship($this->through);
158+
assert(!is_null($relation));
158159
$through_table = $relation->get_table();
159160
$this->options['joins'] = $this->construct_inner_join_sql($through_table, true);
160161

lib/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public function insert(array &$data, string|int $pk = null, string $sequence_nam
423423

424424
/**
425425
* @param string|Attributes $attributes
426-
* @param Attributes $options
426+
* @param RelationOptions $options
427427
*
428428
* @throws Exception\ActiveRecordException
429429
*/

test/ActiveRecordCacheTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use activerecord\Cache;
44
use activerecord\Config;
5+
use ActiveRecord\Table;
56
use test\models\Author;
67

78
class ActiveRecordCacheTest extends DatabaseTestCase
@@ -42,7 +43,7 @@ public function testCachesColumnMetaData()
4243
static::resetTableData();
4344
Author::first();
4445

45-
$table_name = Author::table()->table;
46+
$table_name = Table::load(Author::class)->table;
4647
$value = Cache::$adapter->read("get_meta_data-$table_name");
4748
$this->assertTrue(is_array($value));
4849
}

test/ActiveRecordFindByTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
use ActiveRecord\Exception\ActiveRecordException;
66
use ActiveRecord\Exception\DatabaseException;
7+
use ActiveRecord\Table;
78
use test\models\Author;
89
use test\models\Venue;
910

1011
class ActiveRecordFindByTest extends \DatabaseTestCase
1112
{
1213
public function testEscapeQuotes()
1314
{
14-
$author = Author::find_by_name("Tito's");
15-
$this->assertNotEquals("Tito's", Author::table()->last_sql);
15+
Author::find_by_name("Tito's");
16+
$this->assertNotEquals("Tito's", Table::load(Author::class)->last_sql);
1617
}
1718

1819
public function testFindByWithInvalidFieldName()

test/ActiveRecordFindTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use ActiveRecord\Exception\RecordNotFound;
44
use ActiveRecord\Exception\ValidationsArgumentError;
55
use ActiveRecord\Model;
6+
use ActiveRecord\Table;
67
use test\models\Author;
78
use test\models\HonestLawyer;
89
use test\models\JoinBook;
@@ -141,7 +142,7 @@ public function testFindByPkWithOptions()
141142
{
142143
$author = Author::order('name')->find(3);
143144
$this->assertEquals(3, $author->id);
144-
$this->assertTrue(false !== strpos(Author::table()->last_sql, 'ORDER BY name'));
145+
$this->assertTrue(false !== strpos(Table::load(Author::class)->last_sql, 'ORDER BY name'));
145146
}
146147

147148
public function testFindByPkArray()
@@ -156,7 +157,7 @@ public function testFindByPkArrayWithOptions()
156157
{
157158
$authors = Author::order('name')->find(1, '2');
158159
$this->assertEquals(2, count($authors));
159-
$this->assertTrue(false !== strpos(Author::table()->last_sql, 'ORDER BY name'));
160+
$this->assertTrue(false !== strpos(Table::load(Author::class)->last_sql, 'ORDER BY name'));
160161
}
161162

162163
public function testFindAll()
@@ -281,14 +282,14 @@ public function testJoinsOnModelWithAssociationAndExplicitJoins()
281282
'author'=>true
282283
];
283284
JoinBook::joins(['author', 'LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)'])->first();
284-
$this->assert_sql_includes('INNER JOIN authors ON(books.author_id = authors.author_id)', JoinBook::table()->last_sql);
285-
$this->assert_sql_includes('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)', JoinBook::table()->last_sql);
285+
$this->assert_sql_includes('INNER JOIN authors ON(books.author_id = authors.author_id)', Table::load(JoinBook::class)->last_sql);
286+
$this->assert_sql_includes('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)', Table::load(JoinBook::class)->last_sql);
286287
}
287288

288289
public function testJoinsOnModelWithExplicitJoins()
289290
{
290291
JoinBook::joins(['LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)'])->first();
291-
$this->assert_sql_includes('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)', JoinBook::table()->last_sql);
292+
$this->assert_sql_includes('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)', Table::load(JoinBook::class)->last_sql);
292293
}
293294

294295
public function testFindNonExistentPrimaryKey()
@@ -312,7 +313,7 @@ public function testFindByNull()
312313
public function testFindByPkShouldNotUseLimit()
313314
{
314315
Author::find(1);
315-
$this->assert_sql_includes('SELECT * FROM authors WHERE author_id = ?', Author::table()->last_sql);
316+
$this->assert_sql_includes('SELECT * FROM authors WHERE author_id = ?', Table::load(Author::class)->last_sql);
316317
}
317318

318319
public function testFindsDatetime()

test/ActiveRecordFirstLastTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace test;
44

55
use ActiveRecord\Exception\UndefinedPropertyException;
6+
use ActiveRecord\Table;
67
use test\models\Author;
78

89
class ActiveRecordFirstLastTest extends \DatabaseTestCase
@@ -41,14 +42,14 @@ public function testFirstChainedFromWhere()
4142
public function testFirstSortsByPkByDefault()
4243
{
4344
Author::where(['author_id IN(?)', [1, 2, 3]])->first();
44-
$this->assert_sql_includes('ORDER BY author_id ASC', Author::table()->last_sql);
45+
$this->assert_sql_includes('ORDER BY author_id ASC', Table::load(Author::class)->last_sql);
4546
}
4647

4748
public function testFirstSortsBySuppliedOrder()
4849
{
4950
Author::order('name')->where(['author_id IN(?)', [1, 2, 3]])->first();
50-
$this->assert_sql_includes('ORDER BY name', Author::table()->last_sql);
51-
$this->assert_sql_doesnt_has('ORDER BY author_id ASC', Author::table()->last_sql);
51+
$this->assert_sql_includes('ORDER BY name', Table::load(Author::class)->last_sql);
52+
$this->assert_sql_doesnt_has('ORDER BY author_id ASC', Table::load(Author::class)->last_sql);
5253
}
5354

5455
public function testFirstNoResults()

0 commit comments

Comments
 (0)