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

View File

@ -17,6 +17,7 @@
namespace phpOMS\DataStorage\Database\Query;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Query;
/**
* Database query builder.
@ -174,7 +175,7 @@ class Builder
* @var QueryType
* @since 1.0.0
*/
protected $type = null;
protected $type = QueryType::INSERT;
protected $unionLimit = null;
@ -792,6 +793,26 @@ class Builder
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
{
$this->type = QueryType::UPDATE;
@ -877,7 +898,8 @@ class Builder
return $this->type;
}
public function merge($query) : Builder {
public function merge($query) : Builder
{
return clone($this);
}
}

View File

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