Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Model::delete_all()](#modeldelete_all)
- [Model::update_all()](#modelupdate_all)
- [Model::find_all_by_...()](#modelfind_all_by_attribute)
- [Model::table()](#modeltable)

#### static properties

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

#### other changes
- [Table::delete](#tabledelete)
- [Table::update](#tableupdate)
- [Config::set_model_directory](#configset_model_directory)
- [exceptions location](#exceptions-location)

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

## `Model::table`
The static `table` accessor on `Model` is now protected. If you were making calls directly on `Table`, you will need to refactor your code.
```php
// 1.x
Book::table()->update($attributes, $where);

// 2.0
Book::where($where)->update_all($attributes);
```

If you do need access to the table instance for some reason, you can still get to it:
```php
$table = Table::load(Book::class);
```


# static properties

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

# other changes

## `Table::update`
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:
```php
// 1.x
$table = Book::table();
$table->update([ 'title' => 'Walden` ], ['author_id` => 1]);

// 2.0
$table = Table::load(Book::class);
$options = [
'conditions' => [new WhereClause(['author_id` => 1])]
];
$table->update([ 'title' => 'Walden' ], $options); // where $options is a RelationOptions.
```

## `Table::delete`
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:
```php
// 1.x
$table = Book::table();
$table->delete(['author_id' => 1]);

// 2.0
$table = Table::load(Book::class);
$options = [
'conditions' => [new WhereClause(['author_id` => 1])]
];
$table->delete($options); // where $options is a RelationOptions.
```

## `Config::set_model_directory`

`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.
Expand Down
2 changes: 1 addition & 1 deletion lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ public static function reestablish_connection()
*
* @return Table
*/
public static function table()
protected static function table()
{
$table = Table::load(get_called_class());

Expand Down
6 changes: 4 additions & 2 deletions lib/Relationship/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ protected function query_and_attach_related_models_eagerly(Table $table, array $
$class = $this->options['namespace'] . '\\' . $class;
}

$through_table = $class::table();
assert(class_exists($class));
$through_table = Table::load($class);
} else {
$class = $options['class_name'];
if (isset($this->options['namespace']) && !class_exists($class)) {
$class = $this->options['namespace'] . '\\' . $class;
}
$relation = $class::table()->get_relationship($options['through']);
$relation = Table::load($class)->get_relationship($options['through']);
assert(!is_null($relation));
$through_table = $relation->get_table();
}
$options['joins'] = $this->construct_inner_join_sql($through_table, true);
Expand Down
2 changes: 1 addition & 1 deletion lib/Relationship/HasAndBelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function load(Model $model): mixed
*/
$rel = new Relation($this->class_name, [], []);
$rel->from($this->attribute_name);
$other_table = $model->table()->table;
$other_table = Table::load(get_class($model))->table;
$rel->where($other_table . '. ' . $this->options['foreign_key'] . ' = ?', $model->{$model->get_primary_key()});
$rel->joins([$other_table]);

Expand Down
3 changes: 2 additions & 1 deletion lib/Relationship/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ public function load(Model $model): mixed
$this->set_keys($this->get_table()->class->getName(), true);

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

Expand Down
2 changes: 1 addition & 1 deletion lib/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public function insert(array &$data, string|int $pk = null, string $sequence_nam

/**
* @param string|Attributes $attributes
* @param Attributes $options
* @param RelationOptions $options
*
* @throws Exception\ActiveRecordException
*/
Expand Down
3 changes: 2 additions & 1 deletion test/ActiveRecordCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use activerecord\Cache;
use activerecord\Config;
use ActiveRecord\Table;
use test\models\Author;

class ActiveRecordCacheTest extends DatabaseTestCase
Expand Down Expand Up @@ -42,7 +43,7 @@ public function testCachesColumnMetaData()
static::resetTableData();
Author::first();

$table_name = Author::table()->table;
$table_name = Table::load(Author::class)->table;
$value = Cache::$adapter->read("get_meta_data-$table_name");
$this->assertTrue(is_array($value));
}
Expand Down
5 changes: 3 additions & 2 deletions test/ActiveRecordFindByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use ActiveRecord\Exception\ActiveRecordException;
use ActiveRecord\Exception\DatabaseException;
use ActiveRecord\Table;
use test\models\Author;
use test\models\Venue;

class ActiveRecordFindByTest extends \DatabaseTestCase
{
public function testEscapeQuotes()
{
$author = Author::find_by_name("Tito's");
$this->assertNotEquals("Tito's", Author::table()->last_sql);
Author::find_by_name("Tito's");
$this->assertNotEquals("Tito's", Table::load(Author::class)->last_sql);
}

public function testFindByWithInvalidFieldName()
Expand Down
13 changes: 7 additions & 6 deletions test/ActiveRecordFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use ActiveRecord\Exception\RecordNotFound;
use ActiveRecord\Exception\ValidationsArgumentError;
use ActiveRecord\Model;
use ActiveRecord\Table;
use test\models\Author;
use test\models\HonestLawyer;
use test\models\JoinBook;
Expand Down Expand Up @@ -141,7 +142,7 @@ public function testFindByPkWithOptions()
{
$author = Author::order('name')->find(3);
$this->assertEquals(3, $author->id);
$this->assertTrue(false !== strpos(Author::table()->last_sql, 'ORDER BY name'));
$this->assertTrue(false !== strpos(Table::load(Author::class)->last_sql, 'ORDER BY name'));
}

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

public function testFindAll()
Expand Down Expand Up @@ -281,14 +282,14 @@ public function testJoinsOnModelWithAssociationAndExplicitJoins()
'author'=>true
];
JoinBook::joins(['author', 'LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)'])->first();
$this->assert_sql_includes('INNER JOIN authors ON(books.author_id = authors.author_id)', JoinBook::table()->last_sql);
$this->assert_sql_includes('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)', JoinBook::table()->last_sql);
$this->assert_sql_includes('INNER JOIN authors ON(books.author_id = authors.author_id)', Table::load(JoinBook::class)->last_sql);
$this->assert_sql_includes('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)', Table::load(JoinBook::class)->last_sql);
}

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

public function testFindNonExistentPrimaryKey()
Expand All @@ -312,7 +313,7 @@ public function testFindByNull()
public function testFindByPkShouldNotUseLimit()
{
Author::find(1);
$this->assert_sql_includes('SELECT * FROM authors WHERE author_id = ?', Author::table()->last_sql);
$this->assert_sql_includes('SELECT * FROM authors WHERE author_id = ?', Table::load(Author::class)->last_sql);
}

public function testFindsDatetime()
Expand Down
7 changes: 4 additions & 3 deletions test/ActiveRecordFirstLastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace test;

use ActiveRecord\Exception\UndefinedPropertyException;
use ActiveRecord\Table;
use test\models\Author;

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

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

public function testFirstNoResults()
Expand Down
5 changes: 3 additions & 2 deletions test/ActiveRecordGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace test;

use ActiveRecord;
use ActiveRecord\Table;
use test\models\Author;
use test\models\Venue;

Expand Down Expand Up @@ -41,7 +42,7 @@ public function testGroupWithOrderAndLimitAndHaving(): void

$venues = $relation->to_a();
$this->assertTrue(count($venues) > 0);
$this->assert_sql_includes('SELECT state FROM venues GROUP BY state HAVING length(state) = 2 ORDER BY state', Venue::table()->last_sql);
$this->assert_sql_includes('SELECT state FROM venues GROUP BY state HAVING length(state) = 2 ORDER BY state', ActiveRecord\Table::load(Venue::class)->last_sql);
}

public function testHaving(): void
Expand All @@ -50,6 +51,6 @@ public function testHaving(): void
->group('date(created_at)')
->having("date(created_at) > '2009-01-01'")
->first();
$this->assert_sql_includes("GROUP BY date(created_at) HAVING date(created_at) > '2009-01-01'", Author::table()->last_sql);
$this->assert_sql_includes("GROUP BY date(created_at) HAVING date(created_at) > '2009-01-01'", Table::load(Author::class)->last_sql);
}
}
7 changes: 4 additions & 3 deletions test/ActiveRecordTakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace test;

use ActiveRecord\Exception\UndefinedPropertyException;
use ActiveRecord\Table;
use test\models\Author;

class ActiveRecordTakeTest extends \DatabaseTestCase
Expand Down Expand Up @@ -32,14 +33,14 @@ public function testChainedFromWhere()
public function testNoImplicitOrder()
{
Author::where(['author_id IN(?)', [1, 2, 3]])->take();
$this->assert_sql_doesnt_has('ORDER BY', Author::table()->last_sql);
$this->assert_sql_doesnt_has('ORDER BY', Table::load(Author::class)->last_sql);
}

public function testSortsBySuppliedOrder()
{
Author::order('name')->where(['author_id IN(?)', [1, 2, 3]])->take();
$this->assert_sql_includes('ORDER BY name', Author::table()->last_sql);
$this->assert_sql_doesnt_has('ORDER BY author_id ASC', Author::table()->last_sql);
$this->assert_sql_includes('ORDER BY name', Table::load(Author::class)->last_sql);
$this->assert_sql_doesnt_has('ORDER BY author_id ASC', Table::load(Author::class)->last_sql);
}

public function testNoResults()
Expand Down
5 changes: 3 additions & 2 deletions test/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use ActiveRecord\Exception\ReadOnlyException;
use ActiveRecord\Exception\RelationshipException;
use ActiveRecord\Exception\UndefinedPropertyException;
use ActiveRecord\Table;
use test\models\Author;
use test\models\AwesomePerson;
use test\models\Book;
Expand Down Expand Up @@ -120,8 +121,8 @@ public function testReloadProtectedAttribute()

public function testNamespaceGetsStrippedFromTableName()
{
$model = new \test\models\namespacetest\Book();
$this->assertEquals('books', $model->table()->table);
new \test\models\namespacetest\Book();
$this->assertEquals('books', Table::load(\test\models\namespacetest\Book::class)->table);
}

public function testNamespaceGetsStrippedFromInferredForeignKey()
Expand Down
14 changes: 7 additions & 7 deletions test/ActiveRecordWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public function testFullyQualifiedNameWithExplicitDbName()
public function testSequenceWasExplicitlySet()
{
if (ConnectionManager::get_connection()->supports_sequences()) {
$this->assertEquals(AuthorExplicitSequence::$sequence, AuthorExplicitSequence::table()->sequence);
$this->assertEquals(AuthorExplicitSequence::$sequence, Table::load(AuthorExplicitSequence::class)->sequence);
} else {
$this->assertNull(Author::table()->sequence);
$this->assertNull(Table::load(Author::class)->sequence);
}
}

Expand Down Expand Up @@ -220,8 +220,8 @@ public function testIdType()
public function testDirtyAttributesClearedAfterSaving()
{
$book = $this->make_new_book_and();
$this->assertTrue(false !== strpos($book->table()->last_sql, 'name'));
$this->assertTrue(false !== strpos($book->table()->last_sql, 'special'));
$this->assertTrue(false !== strpos(Table::load(Book::class)->last_sql, 'name'));
$this->assertTrue(false !== strpos(Table::load(Book::class)->last_sql, 'special'));
$this->assertEquals([], $book->dirty_attributes());
}

Expand Down Expand Up @@ -304,7 +304,7 @@ public function testCreateShouldSetCreatedAt()
public function testUpdateWithNoPrimaryKeyDefined()
{
$this->expectException(ActiveRecordException::class);
Author::table()->pk = [];
Table::load(Author::class)->pk = [];
$author = Author::first();
$author->name = 'blahhhhhhhhhh';
$author->save();
Expand All @@ -313,7 +313,7 @@ public function testUpdateWithNoPrimaryKeyDefined()
public function testDeleteWithNoPrimaryKeyDefined()
{
$this->expectException(ActiveRecordException::class);
Author::table()->pk = [];
Table::load(Author::class)->pk = [];
$author = author::first();
$author->delete();
}
Expand Down Expand Up @@ -444,7 +444,7 @@ public function testUpdateAllWithLimitAndOrder()
->update_all('parent_author_id = 2');

$this->assertEquals(1, $num_affected);
$this->assertTrue(false !== strpos(Author::table()->last_sql, 'ORDER BY name asc LIMIT 1'));
$this->assertTrue(false !== strpos(Table::load(Author::class)->last_sql, 'ORDER BY name asc LIMIT 1'));
}

public function testUpdateNativeDatetime()
Expand Down
5 changes: 3 additions & 2 deletions test/CacheModelTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use ActiveRecord\Cache;
use ActiveRecord\Table;
use test\models\Author;
use test\models\Publisher;

Expand Down Expand Up @@ -37,12 +38,12 @@ public function tearDown(): void

public function testDefaultExpire()
{
$this->assertEquals(30, Author::table()->cache_model_expire);
$this->assertEquals(30, Table::load(Author::class)->cache_model_expire);
}

public function testExplicitExpire()
{
$this->assertEquals(2592000, Publisher::table()->cache_model_expire);
$this->assertEquals(2592000, Table::load(Publisher::class)->cache_model_expire);
}

public function testCacheKey()
Expand Down
Loading