-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb9a3df
commit 405a38f
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
vendor | ||
composer.lock | ||
.php_cs.cache | ||
.phpunit.result.cache | ||
coverage |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace SimpleCrud\Query\Traits; | ||
|
||
use InvalidArgumentException; | ||
use RuntimeException; | ||
use SimpleCrud\Row; | ||
use SimpleCrud\RowCollection; | ||
use SimpleCrud\Table; | ||
|
||
trait HasJoin | ||
{ | ||
public function join(Table $table2, string $condition = null, ...$values) | ||
{ | ||
$table1 = $this->table; | ||
|
||
//Has One | ||
if ($field = $table1->getJoinField($table2)) { | ||
$this->query | ||
->join( | ||
'LEFT', | ||
(string) $table2, | ||
sprintf('%s = %s', $field, $table2->id) | ||
); | ||
|
||
//Has many | ||
} elseif ($field = $table->getJoinField($table1)) { | ||
$this->query | ||
->join( | ||
'LEFT', | ||
(string) $table2, | ||
sprintf('%s = %s', $field, $table1->id) | ||
); | ||
|
||
//Has many to many | ||
} elseif ($joinTable = $table1->getJoinTable($table2)) { | ||
$field1 = $joinTable->getJoinField($table1); | ||
$field2 = $joinTable->getJoinField($table2); | ||
|
||
$this->query | ||
->join( | ||
'LEFT', | ||
(string) $joinTable, | ||
sprintf('%s = %s', $field1, $table1->id) | ||
) | ||
->join( | ||
'LEFT', | ||
(string) $table2, | ||
sprintf('%s = %s', $field2, $table2->id) | ||
); | ||
|
||
} else { | ||
throw new RuntimeException( | ||
sprintf('The tables %s and %s are not related', $table1, $table2) | ||
); | ||
} | ||
|
||
if ($condition) { | ||
$this->query->catJoin($condition, $values); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters