diff --git a/lib/Model.php b/lib/Model.php index 4c597f1b..d2cc0804 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -224,6 +224,11 @@ class Model */ public static array $has_many; + /** + * @var array + */ + public static array $has_one; + /** * @var array */ diff --git a/lib/Relationship/HasOne.php b/lib/Relationship/HasOne.php index 9f9cdbd2..3dfccaa3 100644 --- a/lib/Relationship/HasOne.php +++ b/lib/Relationship/HasOne.php @@ -2,6 +2,8 @@ namespace ActiveRecord\Relationship; +use ActiveRecord\Types; + /** * One-to-one relationship. * @@ -13,16 +15,26 @@ * # Table name: people * # Foreign key: state_id * class Person extends ActiveRecord\Model { - * static $has_one = array(array('state')); + * static array $has_one = [ + * 'state' => true + * ]; * } * ``` * - * @package ActiveRecord + * @phpstan-import-type HasManyOptions from Types * * @see http://www.phpactiverecord.org/guides/associations */ class HasOne extends HasMany { + /** + * @param HasManyOptions $options + */ + public function __construct(string $attribute, array $options = []) + { + parent::__construct($attribute, $options); + } + public function is_poly(): bool { return false; diff --git a/lib/Table.php b/lib/Table.php index 08ffeff9..583f14b5 100644 --- a/lib/Table.php +++ b/lib/Table.php @@ -594,7 +594,7 @@ private function set_associations(): void break; case 'has_one': - $relationship = new HasOne($definition[0], $definition); + $relationship = new HasOne($attribute, $definition); break; case 'belongs_to': diff --git a/test/RelationshipTest.php b/test/RelationshipTest.php index de2f8a11..0bc53466 100644 --- a/test/RelationshipTest.php +++ b/test/RelationshipTest.php @@ -55,7 +55,9 @@ public function setUp($connection_name=null): void ] ]; Venue::$has_one = []; - Employee::$has_one = [['position']]; + Employee::$has_one = [ + 'position' => [] + ]; Host::$has_many = [ 'events' => [ 'order' => 'id asc' @@ -516,7 +518,9 @@ public function testHasManyThroughWithInvalidClassName() Event::$belongs_to = [ 'host' => true ]; - Venue::$has_one = [['invalid_assoc']]; + Venue::$has_one = [ + 'invalid_assoc' => true + ]; Venue::$has_many = [ 'hosts' => [ 'through' => 'invalid_assoc' @@ -557,13 +561,18 @@ public function testHasOneBasic() public function testHasOneWithExplicitClassName() { - Employee::$has_one = [['explicit_class_name', 'class_name' => 'Position']]; + Employee::$has_one = [ + 'explicit_class_name' => [ + 'class_name' => 'Position' + ] + ]; $this->assert_default_has_one($this->get_relationship(), 'explicit_class_name'); } public function testHasOneWithSelect() { - Employee::$has_one[0]['select'] = 'title'; + $ho = Employee::$has_one; + Employee::$has_one['position']['select'] = 'title'; $employee = $this->get_relationship(); $this->assert_default_has_one($employee); @@ -577,7 +586,7 @@ public function testHasOneWithSelect() public function testHasOneWithOrder() { - Employee::$has_one[0]['order'] = 'title'; + Employee::$has_one['position']['order'] = 'title'; $employee = $this->get_relationship(); $this->assert_default_has_one($employee); $this->assert_sql_has('ORDER BY title', Position::table()->last_sql); @@ -585,7 +594,7 @@ public function testHasOneWithOrder() public function testHasOneWithConditionsAndNonQualifyingRecord() { - Employee::$has_one[0]['conditions'] = "title = 'programmer'"; + Employee::$has_one['position']['conditions'] = "title = 'programmer'"; $employee = $this->get_relationship(); $this->assertEquals(1, $employee->id); $this->assertNull($employee->position); @@ -593,13 +602,13 @@ public function testHasOneWithConditionsAndNonQualifyingRecord() public function testHasOneWithConditionsAndQualifyingRecord() { - Employee::$has_one[0]['conditions'] = "title = 'physicist'"; + Employee::$has_one['position']['conditions'] = "title = 'physicist'"; $this->assert_default_has_one($this->get_relationship()); } public function testHasOneWithReadonly() { - Employee::$has_one[0]['readonly'] = true; + Employee::$has_one['position']['readonly'] = true; $employee = $this->get_relationship(); $this->assert_default_has_one($employee); @@ -629,7 +638,13 @@ public function testHasOneWithJoins() public function testHasOneWithExplicitKeys() { - Book::$has_one = [['explicit_author', 'class_name' => 'Author', 'foreign_key' => 'parent_author_id', 'primary_key' => 'secondary_author_id']]; + Book::$has_one = [ + 'explicit_author' => [ + 'class_name' => 'Author', + 'foreign_key' => 'parent_author_id', + 'primary_key' => 'secondary_author_id' + ] + ]; $book = Book::find(1); $this->assertEquals($book->secondary_author_id, $book->explicit_author->parent_author_id); diff --git a/test/models/Author.php b/test/models/Author.php index 6a4757ad..8d1702db 100644 --- a/test/models/Author.php +++ b/test/models/Author.php @@ -7,14 +7,19 @@ class Author extends Model { public static $pk = 'author_id'; - // static $has_one = array(array('awesome_person', 'foreign_key' => 'author_id', 'primary_key' => 'author_id'), - // array('parent_author', 'class_name' => 'Author', 'foreign_key' => 'parent_author_id')); public static array $has_many = [ 'books' => true ]; - public static $has_one = [ - ['awesome_person', 'foreign_key' => 'author_id', 'primary_key' => 'author_id'], - ['parent_author', 'class_name' => 'Author', 'foreign_key' => 'parent_author_id']]; + public static array $has_one = [ + 'awesome_person' => [ + 'foreign_key' => 'author_id', + 'primary_key' => 'author_id' + ], + 'parent_author' => [ + 'class_name' => 'Author', + 'foreign_key' => 'parent_author_id' + ] + ]; public static array $belongs_to = []; public function set_password($plaintext) diff --git a/test/models/AuthorAttrAccessible.php b/test/models/AuthorAttrAccessible.php index a1430f0e..93d5c83b 100644 --- a/test/models/AuthorAttrAccessible.php +++ b/test/models/AuthorAttrAccessible.php @@ -15,8 +15,12 @@ class AuthorAttrAccessible extends Model 'primary_key' => 'book_id' ] ]; - public static $has_one = [ - ['parent_author', 'class_name' => 'AuthorAttrAccessible', 'foreign_key' => 'parent_author_id', 'primary_key' => 'author_id'] + public static array $has_one = [ + 'parent_author' => [ + 'class_name' => 'AuthorAttrAccessible', + 'foreign_key' => 'parent_author_id', + 'primary_key' => 'author_id' + ] ]; public static array $belongs_to = []; diff --git a/test/models/Book.php b/test/models/Book.php index c55ac0c4..5f89ed0f 100644 --- a/test/models/Book.php +++ b/test/models/Book.php @@ -9,7 +9,7 @@ class Book extends Model public static array $belongs_to = [ 'author' => true ]; - public static $has_one = []; + public static array $has_one = []; public function upper_name() { diff --git a/test/models/Employee.php b/test/models/Employee.php index 90269a64..619f7502 100644 --- a/test/models/Employee.php +++ b/test/models/Employee.php @@ -6,5 +6,5 @@ class Employee extends Model { - public static $has_one; + public static array $has_one; }