mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-10 22:18:40 +00:00
Implementing has many selects and inserts
This commit is contained in:
parent
009d07ec76
commit
d4321df72f
|
|
@ -164,7 +164,7 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
protected function extend($class)
|
private function extend($class)
|
||||||
{
|
{
|
||||||
/* todo: have to implement this in the queries, so far not used */
|
/* todo: have to implement this in the queries, so far not used */
|
||||||
$this->collection['primaryField'][] = $class::$primaryField;
|
$this->collection['primaryField'][] = $class::$primaryField;
|
||||||
|
|
@ -256,7 +256,7 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->insert($column['name'])
|
$query->insert($column['name'])
|
||||||
->value($value);
|
->value($value, $column['type']);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -273,16 +273,15 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
/* is a has many property */
|
/* is a has many property */
|
||||||
$property = $reflectionClass->getProperty($member); // throws ReflectionException
|
$property = $reflectionClass->getProperty($member); // throws ReflectionException
|
||||||
|
|
||||||
if(!($isPublic = $property->isPublic())) {
|
if (!($isPublic = $property->isPublic())) {
|
||||||
$property->setAccessible(true);
|
$property->setAccessible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
$values = $property->getValue($obj);
|
$values = $property->getValue($obj);
|
||||||
$temp = end($values);
|
$temp = reset($values);
|
||||||
reset($values);
|
$pname = $property->getName();
|
||||||
$pname = $property->getName();
|
|
||||||
|
|
||||||
if(!($isPublic)) {
|
if (!($isPublic)) {
|
||||||
$property->setAccessible(false);
|
$property->setAccessible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,9 +296,8 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
/* todo: I should set the objId here as well before inserting (if many->1)!!!! */
|
/* todo: I should set the objId here as well before inserting (if many->1)!!!! */
|
||||||
$objsIds[$key] = $mapper->create($value);
|
$objsIds[$key] = $mapper->create($value);
|
||||||
}
|
}
|
||||||
} elseif (is_numeric($temp) || is_string($temp)) {
|
} elseif (is_scalar($temp)) {
|
||||||
$objsIds = $values;
|
$objsIds = $values;
|
||||||
continue; // I'm guesssing this is already a key!
|
|
||||||
} else {
|
} else {
|
||||||
throw new \Exception('Unexpected value for relational data mapping.');
|
throw new \Exception('Unexpected value for relational data mapping.');
|
||||||
}
|
}
|
||||||
|
|
@ -308,8 +306,8 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
/* is many->many */
|
/* is many->many */
|
||||||
$relQuery = new Builder($this->db);
|
$relQuery = new Builder($this->db);
|
||||||
$relQuery->prefix($this->db->getPrefix())
|
$relQuery->prefix($this->db->getPrefix())
|
||||||
->into(static::$hasMany[$pname]['table']);
|
->into(static::$hasMany[$pname]['table'])
|
||||||
$relQuery->insert(static::$hasMany[$pname]['src'], static::$hasMany[$pname]['dst']);
|
->insert(static::$hasMany[$pname]['src'], static::$hasMany[$pname]['dst']);
|
||||||
|
|
||||||
foreach ($objsIds as $key => $src) {
|
foreach ($objsIds as $key => $src) {
|
||||||
$relQuery->values($src, $objId);
|
$relQuery->values($src, $objId);
|
||||||
|
|
@ -418,7 +416,15 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
{
|
{
|
||||||
$class = get_class($this);
|
$class = get_class($this);
|
||||||
$class = str_replace('Mapper', '', $class);
|
$class = str_replace('Mapper', '', $class);
|
||||||
$obj = new $class();
|
|
||||||
|
if (count($result) === 0) {
|
||||||
|
$parts = explode('\\', $class);
|
||||||
|
$name = $parts[$c = (count($parts) - 1)];
|
||||||
|
$parts[$c] = 'Null' . $name;
|
||||||
|
$class = implode('\\', $parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
$obj = new $class();
|
||||||
|
|
||||||
return $this->populateAbstract($result, $obj);
|
return $this->populateAbstract($result, $obj);
|
||||||
}
|
}
|
||||||
|
|
@ -592,8 +598,8 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
{
|
{
|
||||||
$obj = [];
|
$obj = [];
|
||||||
if (is_array($primaryKey)) {
|
if (is_array($primaryKey)) {
|
||||||
foreach ($primaryKey as $key) {
|
foreach ($primaryKey as $key => $value) {
|
||||||
$obj[$key] = $this->populate($this->getRaw($key));
|
$obj[$value] = $this->populate($this->getRaw($value));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$obj = $this->populate($this->getRaw($primaryKey));
|
$obj = $this->populate($this->getRaw($primaryKey));
|
||||||
|
|
@ -685,7 +691,7 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
|
|
||||||
$sth = $this->db->con->prepare($query->toSql());
|
$sth = $this->db->con->prepare($query->toSql());
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
$result[$member] = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
$result[$member] = $sth->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user