-
Notifications
You must be signed in to change notification settings - Fork 59
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
693ebad
commit 4acbe68
Showing
9 changed files
with
121 additions
and
82 deletions.
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
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
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
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
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,108 @@ | ||
<?php | ||
namespace SimpleCrud\Queries\Mysql; | ||
|
||
use SimpleCrud\RowInterface; | ||
|
||
/** | ||
* Common function to manage WHERE clause | ||
*/ | ||
trait WhereExtendedTrait | ||
{ | ||
use WhereTrait; | ||
|
||
protected $from = []; | ||
protected $fields = []; | ||
|
||
/** | ||
* Adds new extra table to the query | ||
* | ||
* @param string $table | ||
* | ||
* @return self | ||
*/ | ||
public function from($table) | ||
{ | ||
$this->from[] = $table; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Adds a new extra field to the query | ||
* | ||
* @param string $field | ||
* | ||
* @return self | ||
*/ | ||
public function field($field) | ||
{ | ||
$this->fields[] = $field; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Adds a WHERE according with the relation of other entity | ||
* | ||
* @param RowInterface $row | ||
* @param string $through | ||
* | ||
* @return self | ||
*/ | ||
public function relatedWith(RowInterface $row, $through = null) | ||
{ | ||
$entity = $row->getEntity(); | ||
|
||
if ($through !== null) { | ||
$through = $this->entity->getDb()->$through; | ||
|
||
if (!$through->hasOne($entity)) { | ||
throw new SimpleCrudException("The relationship between '{$through->table}' and '{$entity->table}' must be RELATION_HAS_ONE"); | ||
} | ||
if (!$through->hasOne($this->entity)) { | ||
throw new SimpleCrudException("The relationship between '{$through->table}' and '{$this->entity->table}' must be RELATION_HAS_ONE"); | ||
} | ||
|
||
$this->from($through->table); | ||
$this->from($entity->table); | ||
|
||
$this->fields[] = "`{$through->table}`.`{$entity->foreignKey}`"; | ||
|
||
$this->where("`{$through->table}`.`{$this->entity->foreignKey}` = `{$this->entity->table}`.`id`"); | ||
$this->where("`{$through->table}`.`{$entity->foreignKey}` = `{$entity->table}`.`id`"); | ||
$this->where("`{$entity->table}`.`id` IN (:{$through->name})", [":{$through->name}" => $row->get('id')]); | ||
|
||
return $this; | ||
} | ||
|
||
if ($this->entity->hasOne($entity)) { | ||
return $this->by($entity->foreignKey, $row->get('id')); | ||
} | ||
|
||
if ($this->entity->hasMany($entity)) { | ||
return $this->byId($row->get($this->entity->foreignKey)); | ||
} | ||
|
||
throw new SimpleCrudException("The tables {$this->entity->table} and {$entity->table} are no related"); | ||
} | ||
|
||
/** | ||
* add extra fields to the code | ||
* | ||
* @return string | ||
*/ | ||
protected function fieldsToString($prepend = ', ') | ||
{ | ||
return $this->fields ? $prepend.implode(', ', $this->fields) : ''; | ||
} | ||
|
||
/** | ||
* add extra fields to the code | ||
* | ||
* @return string | ||
*/ | ||
protected function fromToString($prepend = ', ') | ||
{ | ||
return $this->from ? $prepend.'`'.implode('`, `', $this->from).'`' : ''; | ||
} | ||
} |
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
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
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
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,6 +1,5 @@ | ||
<?php | ||
use SimpleCrud\SimpleCrud; | ||
use SimpleCrud\Entity; | ||
|
||
class RowTest extends PHPUnit_Framework_TestCase | ||
{ | ||
|