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
6 changes: 6 additions & 0 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
* please consult our {@link http://www.phpactiverecord.org/guides Guides}.
*
* @phpstan-import-type HasManyOptions from Types
* @phpstan-import-type HasAndBelongsToManyOptions from Types
* @phpstan-import-type BelongsToOptions from Types
* @phpstan-import-type SerializeOptions from Serialize\Serialization
* @phpstan-import-type ValidationOptions from Validations
Expand Down Expand Up @@ -225,6 +226,11 @@ class Model
*/
public static array $has_many;

/**
* @var array<string, HasAndBelongsToManyOptions>
*/
public static array $has_and_belongs_to_many;

/**
* @var array<string,HasManyOptions>
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/Relationship/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ abstract class AbstractRelationship
* @var array<string>
*/
protected static $valid_association_options = [
'association_foreign_key',
'class_name',
'foreign_key',
'conditions',
'select',
'join_table',
'readonly',
'namespace'
];
Expand Down Expand Up @@ -339,7 +341,7 @@ protected function create_conditions_from_keys(Model $model, array $condition_ke
*
* @return string SQL INNER JOIN fragment
*/
public function construct_inner_join_sql(Table $from_table, $using_through = false, $alias = null)
public function construct_inner_join_sql(Table $from_table, bool $using_through = false, string $alias = null)
{
if ($using_through) {
$join_table_name = $from_table->get_fully_qualified_table_name();
Expand Down
64 changes: 52 additions & 12 deletions lib/Relationship/HasAndBelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,78 @@

namespace ActiveRecord\Relationship;

use ActiveRecord\Inflector;
use ActiveRecord\Model;
use ActiveRecord\Relation;
use ActiveRecord\Table;
use ActiveRecord\Types;
use ActiveRecord\Utils;

/**
* @todo implement me
*
* @package ActiveRecord
* @template TModel of Model
*
* @see http://www.phpactiverecord.org/guides/associations
* @phpstan-import-type HasAndBelongsToManyOptions from Types
*/
class HasAndBelongsToMany extends AbstractRelationship
{
public function __construct($options = [])
protected string $association_foreign_key = '';

/**
* @param HasAndBelongsToManyOptions $options
*/
public function __construct(string $attribute, array $options = [])
{
/* options =>
* join_table - name of the join table if not in lexical order
* foreign_key -
* association_foreign_key - default is {assoc_class}_id
* uniq - if true duplicate assoc objects will be ignored
* validate
*/
parent::__construct($options[0], $options);
parent::__construct($attribute, $options);

$this->set_class_name($this->inferred_class_name(Utils::singularize($attribute)));

$this->options['association_foreign_key'] ??= Inflector::keyify($this->class_name);
}

public function is_poly(): bool
{
return true;
}

/**
* @return array<TModel>
*/
public function load(Model $model): mixed
{
throw new \Exception("HasAndBelongsToMany doesn't need to load anything.");
/**
* @var Relation<TModel>
*/
$rel = new Relation($this->class_name, [], []);
$rel->from($this->attribute_name);
$other_table = $model->table()->table;
$rel->where($other_table . '. ' . $this->options['foreign_key'] . ' = ?', $model->{$model->get_primary_key()});
$rel->joins([$other_table]);

return $rel->to_a();
}

public static function inferJoiningTableName(string $class_name, string $association_name): string
{
$parts = [$association_name, $class_name];
sort($parts);

return implode('_', $parts);
}

public function construct_inner_join_sql(Table $from_table, bool $using_through = false, string $alias = null): string
{
$other_table = Table::load($this->class_name);
$associated_table_name = $other_table->table;
$from_table_name = $from_table->table;
$foreign_key = $this->options['foreign_key'];
$join_primary_key = $this->options['association_foreign_key'];
$linkingTableName = $this->options['join_table'];
$res = 'INNER JOIN ' . $linkingTableName . " ON ($from_table_name.$foreign_key = " . $linkingTableName . ".$foreign_key) "
. 'INNER JOIN ' . $associated_table_name . ' ON ' . $associated_table_name . '.' . $join_primary_key . ' = ' . $linkingTableName . '.' . $join_primary_key;

return $res;
}

public function load_eagerly($models, $attributes, $includes, Table $table): void
Expand Down
4 changes: 3 additions & 1 deletion lib/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,9 @@ private function set_associations(): void
break;

case 'has_and_belongs_to_many':
$relationship = new HasAndBelongsToMany($definition);
$definition['join_table'] ??= HasAndBelongsToMany::inferJoiningTableName($this->table, $attribute);
$definition['foreign_key'] ??= $this->pk[0];
$relationship = new HasAndBelongsToMany($attribute, $definition);
break;
}

Expand Down
11 changes: 9 additions & 2 deletions lib/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
* set?: string|array<string, mixed>
* }
* @phpstan-type HasManyOptions array{
* group?: string,
* limit?: int,
* offset?: int,
* primary_key?: string|array<string>,
* group?: string,
* order?: string,
* primary_key?: string|array<string>,
* through?: string
* }
* @phpstan-type HasAndBelongsToManyOptions array{
* join_table?: string,
* foreign_key?: string,
* association_foreign_key?: string,
* uniq?: bool,
* validate?: bool
* }
* @phpstan-type BelongsToOptions array{
* conditions?: array<mixed>,
* foreign_key?: string,
Expand Down
Loading