Fixing minor bugs for insert, such as accessibility and query building in loops.

This commit is contained in:
Dennis Eichhorn 2015-12-20 22:28:40 +01:00
parent fdef759d37
commit e247c6a627
3 changed files with 44 additions and 18 deletions

View File

@ -249,7 +249,9 @@ abstract class DataMapperAbstract implements DataMapperInterface
foreach ($properties as $property) { foreach ($properties as $property) {
foreach (static::$columns as $key => $column) { foreach (static::$columns as $key => $column) {
if ($column['internal'] === $property) { if ($column['internal'] === $property->getName()) {
$property->setAccessible(true);
$value = $property->getValue($obj); $value = $property->getValue($obj);
if ($column['type'] === 'DateTime') { if ($column['type'] === 'DateTime') {
@ -257,7 +259,9 @@ abstract class DataMapperAbstract implements DataMapperInterface
} }
$query->insert($column['name']) $query->insert($column['name'])
->values($value); ->value($value);
// todo: do i have to reverse the accessibility or is there no risk involved here?
break; break;
} }
@ -381,11 +385,11 @@ abstract class DataMapperAbstract implements DataMapperInterface
$query = new Builder($this->db); $query = new Builder($this->db);
$query->prefix($this->db->getPrefix()) $query->prefix($this->db->getPrefix())
->select('*') ->select('*')
->from(static::$table) ->from(static::$table)
->limit(1); ->limit(1);
if(isset(static::$createdAt)) { if (isset(static::$createdAt)) {
$query->orderBy(static::$table . '.' . static::$columns[static::$createdAt]['name'], 'DESC'); $query->orderBy(static::$table . '.' . static::$columns[static::$createdAt]['name'], 'DESC');
} else { } else {
$query->orderBy(static::$table . '.' . static::$columns[static::$primaryField]['name'], 'DESC'); $query->orderBy(static::$table . '.' . static::$columns[static::$primaryField]['name'], 'DESC');
@ -481,9 +485,9 @@ abstract class DataMapperAbstract implements DataMapperInterface
{ {
$query = new Builder($this->db); $query = new Builder($this->db);
$query->prefix($this->db->getPrefix()) $query->prefix($this->db->getPrefix())
->select('*') ->select('*')
->from(static::$table) ->from(static::$table)
->where(static::$table . '.' . static::$primaryField, '=', $primaryKey); ->where(static::$table . '.' . static::$primaryField, '=', $primaryKey);
$sth = $this->db->con->prepare($query->toSql()); $sth = $this->db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();
@ -522,9 +526,9 @@ abstract class DataMapperAbstract implements DataMapperInterface
if (!isset($value['mapper'])) { if (!isset($value['mapper'])) {
$query = new Builder($this->db); $query = new Builder($this->db);
$query->prefix($this->db->getPrefix()) $query->prefix($this->db->getPrefix())
->select($value['table'] . '.' . $value['src']) ->select($value['table'] . '.' . $value['src'])
->from($value['table']) ->from($value['table'])
->where($value['table'] . '.' . $value['dst'], '=', $primaryKey); ->where($value['table'] . '.' . $value['dst'], '=', $primaryKey);
$sth = $this->db->con->prepare($query->toSql()); $sth = $this->db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();
@ -560,8 +564,8 @@ abstract class DataMapperAbstract implements DataMapperInterface
{ {
$query = new Builder($this->db); $query = new Builder($this->db);
$query->prefix($this->db->getPrefix()) $query->prefix($this->db->getPrefix())
->select('*') ->select('*')
->from(static::$table); ->from(static::$table);
$sth = $this->db->con->prepare($query->toSql()); $sth = $this->db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();

View File

@ -17,6 +17,7 @@
namespace phpOMS\DataStorage\Database\Query; namespace phpOMS\DataStorage\Database\Query;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Query;
/** /**
* Database query builder. * Database query builder.
@ -174,7 +175,7 @@ class Builder
* @var QueryType * @var QueryType
* @since 1.0.0 * @since 1.0.0
*/ */
protected $type = null; protected $type = QueryType::INSERT;
protected $unionLimit = null; protected $unionLimit = null;
@ -792,6 +793,26 @@ class Builder
return $this; return $this;
} }
/**
* Values to insert.
*
* @param mixed $value Values
*
* @return Builder
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function value($value) : Builder
{
end($this->values);
$key = key($this->values);
$this->values[$key][] = $value;
reset($this->values);
return $this;
}
public function update() : Builder public function update() : Builder
{ {
$this->type = QueryType::UPDATE; $this->type = QueryType::UPDATE;
@ -877,7 +898,8 @@ class Builder
return $this->type; return $this->type;
} }
public function merge($query) : Builder { public function merge($query) : Builder
{
return clone($this); return clone($this);
} }
} }

View File

@ -169,10 +169,10 @@ class Grammar extends \phpOMS\DataStorage\Database\Grammar
$components = $this->insertComponents; $components = $this->insertComponents;
break; break;
case QueryType::UPDATE: case QueryType::UPDATE:
$components = null; $components = [];
break; break;
case QueryType::DELETE: case QueryType::DELETE:
$components = null; $components = [];
break; break;
default: default:
throw new \InvalidArgumentException('Unknown query type.'); throw new \InvalidArgumentException('Unknown query type.');