diff --git a/src/Queries/Mysql/Count.php b/src/Queries/Mysql/Count.php index 5a69a6c..719e49a 100644 --- a/src/Queries/Mysql/Count.php +++ b/src/Queries/Mysql/Count.php @@ -11,7 +11,7 @@ */ class Count extends BaseQuery { - use WhereTrait; + use WhereExtendedTrait; use LimitTrait; /** @@ -62,6 +62,7 @@ public function __toString() { $query = "SELECT COUNT(*) FROM `{$this->entity->table}`"; + $query .= $this->fromToString(); $query .= $this->whereToString(); $query .= $this->limitToString(); diff --git a/src/Queries/Mysql/LimitTrait.php b/src/Queries/Mysql/LimitTrait.php index 5e07ac8..515a84c 100644 --- a/src/Queries/Mysql/LimitTrait.php +++ b/src/Queries/Mysql/LimitTrait.php @@ -1,7 +1,6 @@ from[] = $table; - - 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"); - } - /** * Adds an ORDER BY clause * @@ -159,7 +97,7 @@ public function run() /** * Run the query and return all values - * + * * @param boolean $idAsKey * * @return RowCollection @@ -226,15 +164,9 @@ public function __toString() $query .= ', '.static::buildFields($join['entity']->table, array_keys($join['entity']->fields), $join['entity']->name); } - foreach ($this->fields as $field) { - $query .= ', '.$field; - } - + $query .= $this->fieldsToString(); $query .= ' FROM `'.$this->entity->table.'`'; - - if (!empty($this->from)) { - $query .= ', `'.implode('`, `', $this->from).'`'; - } + $query .= $this->fromToString(); foreach ($this->leftJoin as $join) { $query .= ' LEFT JOIN `'.$join['entity']->table.'`"'; diff --git a/src/Queries/Mysql/Sum.php b/src/Queries/Mysql/Sum.php index 56f93cf..bd1f090 100644 --- a/src/Queries/Mysql/Sum.php +++ b/src/Queries/Mysql/Sum.php @@ -9,9 +9,9 @@ /** * Manages a database select count query in Mysql databases */ -class Count extends BaseQuery +class Sum extends BaseQuery { - use WhereTrait; + use WhereExtendedTrait; use LimitTrait; protected $field; @@ -92,6 +92,7 @@ public function __toString() { $query = "SELECT SUM(`{$this->field}`) FROM `{$this->entity->table}`"; + $query .= $this->fromToString(); $query .= $this->whereToString(); $query .= $this->limitToString(); diff --git a/src/Queries/Mysql/WhereExtendedTrait.php b/src/Queries/Mysql/WhereExtendedTrait.php new file mode 100644 index 0000000..15ec254 --- /dev/null +++ b/src/Queries/Mysql/WhereExtendedTrait.php @@ -0,0 +1,108 @@ +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).'`' : ''; + } +} diff --git a/src/Queries/Mysql/WhereTrait.php b/src/Queries/Mysql/WhereTrait.php index 8a2745b..5847822 100644 --- a/src/Queries/Mysql/WhereTrait.php +++ b/src/Queries/Mysql/WhereTrait.php @@ -1,7 +1,6 @@ idAsKey === false) { $this->rows[] = $value; + return; } diff --git a/tests/RowTest.php b/tests/RowTest.php index ac5e9bb..8f51209 100644 --- a/tests/RowTest.php +++ b/tests/RowTest.php @@ -1,6 +1,5 @@