/** * Save an existing Model object into the database. * Will create, set and execute a dataset object as appropriately internally. * * @param boolean $useset Set to true to have this model use an INSERT_UPDATE statement instead of just UPDATE. * @return bool */ protected function _saveExisting($useset = false) { // If this model doesn't have any changes, don't save it! if(!$this->changed()) return false; $i = self::GetIndexes(); $s = self::GetSchema(); $n = $this->_getTableName(); // No primary schema defined or it's a string, (single value)... just don't make the in_array bail out. $pri = isset($i['primary']) ? $i['primary'] : []; if($pri && !is_array($pri)) $pri = [$pri]; if($pri && !is_array($pri)) $pri = [$pri]; // This is the dataset object that will be integral in this function. $dat = new Core\Datamodel\Dataset(); $dat->table($n); foreach($this->_columns as $c){ /** @var \Core\Datamodel\Columns\SchemaColumn $c */ if($c->type == Model::ATT_TYPE_ID || $c->type == Model::ATT_TYPE_UUID){ $dat->setID($c->field, $c->value); } elseif($c->changed()){ $dat->update($c->field, $c->getUpdateValue()); } if (in_array($c->field, $pri)) { // Any field keyed as a primary field sets the WHERE clause. $dat->where($c->field, $c->getUpdateValue()); } } //var_dump($dat); die(); // No data.. nothing to change I guess! // This is a failsafe should (for some reason), the changed() method doesn't return the correct value. if(!sizeof($dat->_sets)){ return false; } //var_dump($dat); die('mep'); // DEBUG $dat->execute($this->interface); // IDs don't change in updates, else they wouldn't be the id. return true; }
/** * Internal function to parse and handle the dataset in the <upgrade> and <install> tasks. * This is used for installations and upgrades. * * Unlike the other parse functions, this handles a single node at a time. * * @param $node DOMElement * @param $verbose bool * * @throws InstallerException */ private function _parseDatasetNode(DOMElement $node, $verbose = false){ $action = $node->getAttribute('action'); $table = $node->getAttribute('table'); $haswhere = false; $sets = array(); $renames = array(); $ds = new Core\Datamodel\Dataset(); $ds->table($table); foreach($node->getElementsByTagName('datasetset') as $el){ $sets[$el->getAttribute('key')] = $el->nodeValue; } foreach($node->getElementsByTagName('datasetrenamecolumn') as $el){ // <datasetrenamecolumn oldname="ID" newname="id"/> $renames[$el->getAttribute('oldname')] = $el->getAttribute('newname'); } foreach($node->getElementsByTagName('datasetwhere') as $el){ $haswhere = true; $ds->where(trim($el->nodeValue)); } switch($action){ case 'alter': if(sizeof($sets)) throw new InstallerException('Invalid mix of arguments on ' . $action . ' dataset request, datasetset is not supported!'); if($haswhere) throw new InstallerException('Invalid mix of arguments on ' . $action . ' dataset request, datasetwhere is not supported!'); foreach($renames as $k => $v){ // ALTER TABLE `controllers` CHANGE `ID` `id` INT( 11 ) NOT NULL AUTO_INCREMENT $ds->renameColumn($k, $v); } break; case 'update': foreach($sets as $k => $v){ $ds->update($k, $v); } break; case 'insert': foreach($sets as $k => $v){ $ds->insert($k, $v); } break; case 'delete': if(sizeof($sets)) throw new InstallerException('Invalid mix of arguments on ' . $action . ' dataset request'); if(!$haswhere) throw new InstallerException('Cowardly refusing to delete with no where statement'); $ds->delete(); break; default: throw new InstallerException('Invalid action type, '. $action); } // and GO! if($verbose){ CLI::PrintActionStart('Executing dataset ' . $action . ' command on ' . $table); } $ds->execute(); if($ds->num_rows){ CLI::PrintActionStatus(true); return array($action . ' on table ' . $table . ' affected ' . $ds->num_rows . ' records.'); } else{ CLI::PrintActionStatus(false); return false; } }