Skip to content

Commit

Permalink
#285 - added the DEnum::saveIfNew() method and calling it from the Ac…
Browse files Browse the repository at this point in the history
…tiveRecord providers to save new DEnums for the first time
  • Loading branch information
alphadevx committed Aug 17, 2017
1 parent 4bd701d commit 470d2f1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
9 changes: 6 additions & 3 deletions Alpha/Model/ActiveRecordProviderMySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,8 @@ public function makeTable()
$sqlQuery = rtrim($sqlQuery, ',');
$sqlQuery .= ') CHARACTER SET utf8,';
} elseif ($prop instanceof DEnum) {
$tmp = new DEnum(get_class($this->record).'::'.$propName);
$denum = new DEnum(get_class($this->record).'::'.$propName);
$denum->saveIfNew();
$sqlQuery .= "$propName INT(11) ZEROFILL,";
} elseif ($prop instanceof Relation) {
$sqlQuery .= "$propName INT(11) ZEROFILL UNSIGNED,";
Expand Down Expand Up @@ -1352,7 +1353,8 @@ public function makeHistoryTable()
$sqlQuery = rtrim($sqlQuery, ',');
$sqlQuery .= ') CHARACTER SET utf8,';
} elseif ($prop instanceof DEnum) {
$tmp = new DEnum(get_class($this->record).'::'.$propName);
$denum = new DEnum(get_class($this->record).'::'.$propName);
$denum->saveIfNew();
$sqlQuery .= "$propName INT(11) ZEROFILL,";
} elseif ($prop instanceof Relation) {
$sqlQuery .= "$propName INT(11) ZEROFILL UNSIGNED,";
Expand Down Expand Up @@ -1479,7 +1481,8 @@ public function addProperty($propName)
$sqlQuery = rtrim($sqlQuery, ',');
$sqlQuery .= ') CHARACTER SET utf8';
} elseif ($prop instanceof DEnum) {
$tmp = new DEnum(get_class($this->record).'::'.$propName);
$denum = new DEnum(get_class($this->record).'::'.$propName);
$denum->saveIfNew();
$sqlQuery .= "$propName INT(11) ZEROFILL";
} elseif ($prop instanceof Relation) {
$sqlQuery .= "$propName INT(11) ZEROFILL UNSIGNED";
Expand Down
9 changes: 6 additions & 3 deletions Alpha/Model/ActiveRecordProviderSQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,8 @@ public function makeTable()
} elseif ($prop instanceof Enum) {
$sqlQuery .= "$propName TEXT,";
} elseif ($prop instanceof DEnum) {
$tmp = new DEnum(get_class($this->record).'::'.$propName);
$denum = new DEnum(get_class($this->record).'::'.$propName);
$denum->saveIfNew();
$sqlQuery .= "$propName INTEGER(11),";
} elseif ($prop instanceof Relation) {
$sqlQuery .= "$propName INTEGER(11),";
Expand Down Expand Up @@ -1406,7 +1407,8 @@ public function makeHistoryTable()
} elseif ($prop instanceof Enum) {
$sqlQuery .= "$propName TEXT,";
} elseif ($prop instanceof DEnum) {
$tmp = new DEnum(get_class($this->record).'::'.$propName);
$denum = new DEnum(get_class($this->record).'::'.$propName);
$denum->saveIfNew();
$sqlQuery .= "$propName INTEGER(11),";
} elseif ($prop instanceof Relation) {
$sqlQuery .= "$propName INTEGER(11),";
Expand Down Expand Up @@ -1529,7 +1531,8 @@ public function addProperty($propName)
} elseif ($prop instanceof Enum) {
$sqlQuery .= "$propName TEXT";
} elseif ($prop instanceof DEnum) {
$tmp = new DEnum(get_class($this->record).'::'.$propName);
$denum = new DEnum(get_class($this->record).'::'.$propName);
$denum->saveIfNew();
$sqlQuery .= "$propName INTEGER(11)";
} elseif ($prop instanceof Relation) {
$sqlQuery .= "$propName INTEGER(11)";
Expand Down
22 changes: 18 additions & 4 deletions Alpha/Model/Type/DEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,23 @@ public function __construct($name = null)

$this->name = new SmallText($name);

if (isset($name) && $this->checkTableExists()) {
if (isset($name)) {
$this->saveIfNew();
}
}

/**
* Saves a new DEnum record with the same name as this instance if one does not already exist
*
* @return void
*
* @since 3.0
*/
public function saveIfNew()
{
if (isset($this->name) && $this->checkTableExists()) {
try {
$this->loadByAttribute('name', $name);
$this->loadByAttribute('name', $this->name);
} catch (RecordNotFoundException $e) {
// DEnum does not exist so create it
$this->save();
Expand Down Expand Up @@ -189,9 +203,9 @@ public function getOptions($alphaSort = false)
$count = 0;
$this->options = array();

$tmp = new DEnumItem();
$item = new DEnumItem();

foreach ($tmp->loadItems($options->getID()) as $DEnumItem) {
foreach ($item->loadItems($options->getID()) as $DEnumItem) {
$this->options[$DEnumItem->getID()] = $DEnumItem->getValue();
++$count;
}
Expand Down

0 comments on commit 470d2f1

Please sign in to comment.