summaryrefslogtreecommitdiffstats
path: root/library/Director/Data/Db/DbObject.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Director/Data/Db/DbObject.php')
-rw-r--r--library/Director/Data/Db/DbObject.php73
1 files changed, 66 insertions, 7 deletions
diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php
index 6ecae8b..114b61b 100644
--- a/library/Director/Data/Db/DbObject.php
+++ b/library/Director/Data/Db/DbObject.php
@@ -80,6 +80,9 @@ abstract class DbObject
protected $binaryProperties = [];
+ /* key/value!! */
+ protected $booleans = [];
+
/**
* Filled with object instances when prefetchAll is used
*/
@@ -346,6 +349,16 @@ abstract class DbObject
return $this->$func($value);
}
+ if ($this->getUuidColumn() === $key) {
+ if (strlen($value) > 16) {
+ $value = Uuid::fromString($value)->getBytes();
+ }
+ }
+
+ if ($this->propertyIsBoolean($key)) {
+ $value = DbDataFormatter::normalizeBoolean($value);
+ }
+
if (! $this->hasProperty($key)) {
throw new InvalidArgumentException(sprintf(
'Trying to set invalid key "%s"',
@@ -372,7 +385,10 @@ abstract class DbObject
return $this;
}
if ($key === 'id' || substr($key, -3) === '_id') {
- if ((int) $value === (int) $this->properties[$key]) {
+ if ($value !== null
+ && $this->properties[$key] !== null
+ && (int) $value === (int) $this->properties[$key]
+ ) {
return $this;
}
}
@@ -553,7 +569,7 @@ abstract class DbObject
/**
* Unique key name
*
- * @return string
+ * @return string|array
*/
public function getKeyName()
{
@@ -706,8 +722,7 @@ abstract class DbObject
*/
protected function loadFromDb()
{
- $select = $this->db->select()->from($this->table)->where($this->createWhere());
- $properties = $this->db->fetchRow($select);
+ $properties = $this->db->fetchRow($this->prepareObjectQuery());
if (empty($properties)) {
if (is_array($this->getKeyName())) {
@@ -728,6 +743,11 @@ abstract class DbObject
return $this->setDbProperties($properties);
}
+ public function prepareObjectQuery()
+ {
+ return $this->db->select()->from($this->table)->where($this->createWhere());
+ }
+
/**
* @param object|array $row
* @param Db $db
@@ -878,6 +898,11 @@ abstract class DbObject
return in_array($column, $this->binaryProperties) || $this->getUuidColumn() === $column;
}
+ public function propertyIsBoolean($property)
+ {
+ return array_key_exists($property, $this->booleans);
+ }
+
/**
* Store object to database
*
@@ -959,7 +984,7 @@ abstract class DbObject
$this->table,
$this->getLogId(),
$e->getMessage(),
- var_export($this->getProperties(), 1) // TODO: Remove properties
+ var_export($this->getProperties(), true) // TODO: Remove properties
));
}
@@ -1027,7 +1052,7 @@ abstract class DbObject
if ($this->hasUuidColumn() && $this->properties[$this->uuidColumn] !== null) {
return $this->db->quoteInto(
sprintf('%s = ?', $this->getUuidColumn()),
- $this->connection->quoteBinary($this->getUniqueId()->getBytes())
+ $this->connection->quoteBinary($this->getOriginalProperty($this->uuidColumn))
);
}
if ($id = $this->getAutoincId()) {
@@ -1302,6 +1327,40 @@ abstract class DbObject
}
/**
+ * @param $id
+ * @param DbConnection $connection
+ * @return static
+ */
+ public static function loadOptional($id, DbConnection $connection): ?DbObject
+ {
+ if ($prefetched = static::getPrefetched($id)) {
+ return $prefetched;
+ }
+ /** @var DbObject $obj */
+ $obj = new static();
+
+ if (self::$dbObjectStore !== null && $obj->hasUuidColumn()) {
+ $table = $obj->getTableName();
+ assert($connection instanceof Db);
+ $uuid = UuidLookup::findUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch());
+ if ($uuid) {
+ return self::$dbObjectStore->load($table, $uuid);
+ }
+
+ return null;
+ }
+
+ $obj->setConnection($connection)->setKey($id);
+ $properties = $connection->getDbAdapter()->fetchRow($obj->prepareObjectQuery());
+ if (empty($properties)) {
+ return null;
+ }
+
+ $obj->setDbProperties($properties);
+ return $obj;
+ }
+
+ /**
* @param DbConnection $connection
* @param \Zend_Db_Select $query
* @param string|null $keyColumn
@@ -1436,7 +1495,7 @@ abstract class DbObject
));
}
- public static function loadWithUniqueId(UuidInterface $uuid, DbConnection $connection)
+ public static function loadWithUniqueId(UuidInterface $uuid, DbConnection $connection): ?DbObject
{
$db = $connection->getDbAdapter();
$obj = new static;