-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathJoinsTrait.php
62 lines (50 loc) · 1.55 KB
/
JoinsTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Atk4\Data\Model;
use Atk4\Data\Exception;
/**
* Provides native Model methods for join functionality.
*/
trait JoinsTrait
{
/**
* The class used by join() method.
*
* @var array
*/
public $_default_seed_join = [Join::class];
/**
* Creates an objects that describes relationship between multiple tables (or collections).
*
* When object is loaded, then instead of pulling all the data from a single table,
* 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 array<string, mixed> $defaults
*/
public function join(string $foreignTable, array $defaults = []): Join
{
$this->assertIsModel();
$defaults[0] = $foreignTable;
$join = Join::fromSeed($this->_default_seed_join, $defaults);
if ($this->hasElement($name = $join->getDesiredName())) {
throw (new Exception('Join with such name already exists'))
->addMoreInfo('name', $name)
->addMoreInfo('foreignTable', $foreignTable);
}
$this->add($join);
return $join;
}
/**
* Add left/weak join.
*
* @see join()
*
* @param array<string, mixed> $defaults
*/
public function leftJoin(string $foreignTable, array $defaults = []): Join
{
$defaults['weak'] = true;
return $this->join($foreignTable, $defaults);
}
}