Skip to content

Commit

Permalink
revert to accept only string foreign_table
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 16, 2022
1 parent 4c5f730 commit d91be39
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
4 changes: 2 additions & 2 deletions docs/persistence/sql/queries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,11 @@ and overwrite this simple method to support expressions like this, for example:
Joining with other tables
-------------------------

.. php:method:: join($foreign_table, $master_field, $join_kind)
.. php:method:: join($foreignTable, $master_field, $join_kind)
Join results with additional table using "JOIN" statement in your query.

:param string|array $foreign_table: table to join (may include field and alias)
:param string|array $foreignTable: table to join (may include field and alias)
:param mixed $master_field: main field (and table) to join on or Expression
:param string $join_kind: 'left' (default), 'inner', 'right' etc - which join type to use
:returns: $this
Expand Down
15 changes: 5 additions & 10 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Join
/**
* Foreign model or WITH/CTE alias when used with SQL persistence.
*
* @var Model|string
* @var string
*/
protected $foreign_table;

Expand Down Expand Up @@ -119,15 +119,12 @@ class Join
/** @var array<int, array<string, mixed>> Data indexed by spl_object_id(entity) which is populated here as the save/insert progresses. */
private $saveBufferByOid = [];

/**
* @param Model|string $foreignTable
*/
public function __construct($foreignTable = null)
public function __construct(string $foreignTable = null)
{
$this->foreign_table = $foreignTable;

// handle foreign table containing a dot - that will be reverse join
if (is_string($this->foreign_table) && strpos($this->foreign_table, '.') !== false) {
if (strpos($this->foreign_table, '.') !== false) {
// split by LAST dot in foreign_table name
[$this->foreign_table, $this->foreign_field] = preg_split('~\.+(?=[^.]+$)~', $this->foreign_table);
$this->reverse = true;
Expand Down Expand Up @@ -320,10 +317,9 @@ public function addFields(array $fields = [], array $defaults = [])
/**
* Another join will be attached to a current join.
*
* @param Model|string $foreignTable
* @param array<string, mixed> $defaults
*/
public function join($foreignTable, array $defaults = []): self
public function join(string $foreignTable, array $defaults = []): self
{
$defaults['joinName'] = $this->short_name;

Expand All @@ -333,10 +329,9 @@ public function join($foreignTable, array $defaults = []): self
/**
* Another leftJoin will be attached to a current join.
*
* @param Model|string $foreignTable
* @param array<string, mixed> $defaults
*/
public function leftJoin($foreignTable, array $defaults = []): self
public function leftJoin(string $foreignTable, array $defaults = []): self
{
$defaults['joinName'] = $this->short_name;

Expand Down
7 changes: 2 additions & 5 deletions src/Model/JoinsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Atk4\Data\Model;

use Atk4\Data\Exception;
use Atk4\Data\Model;

/**
* Provides native Model methods for join functionality.
Expand All @@ -22,10 +21,9 @@ trait JoinsTrait
* join will also query $foreignTable in order to find additional fields. When inserting
* the record will be also added inside $foreignTable and relationship will be maintained.
*
* @param Model|string $foreignTable
* @param array<string, mixed> $defaults
*/
public function join($foreignTable, array $defaults = []): Join
public function join(string $foreignTable, array $defaults = []): Join
{
$this->assertIsModel();

Expand All @@ -49,10 +47,9 @@ public function join($foreignTable, array $defaults = []): Join
*
* @see join()
*
* @param Model|string $foreignTable
* @param array<string, mixed> $defaults
*/
public function leftJoin($foreignTable, array $defaults = []): Join
public function leftJoin(string $foreignTable, array $defaults = []): Join
{
$defaults['weak'] = true;

Expand Down

0 comments on commit d91be39

Please sign in to comment.