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
5 changes: 5 additions & 0 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ class Model
*/
public static array $has_many;

/**
* @var array<string,HasManyOptions>
*/
public static array $has_one;

/**
* @var array<string,BelongsToOptions>
*/
Expand Down
16 changes: 14 additions & 2 deletions lib/Relationship/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ActiveRecord\Relationship;

use ActiveRecord\Types;

/**
* One-to-one relationship.
*
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
33 changes: 24 additions & 9 deletions test/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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);

Expand All @@ -577,29 +586,29 @@ 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);
}

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);
}

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);

Expand Down Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions test/models/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions test/models/AuthorAttrAccessible.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
2 changes: 1 addition & 1 deletion test/models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion test/models/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class Employee extends Model
{
public static $has_one;
public static array $has_one;
}