示例#1
0
 /**
  * Save an object.
  * @param ChunsuObject $saveme The object to save.
  * @param DataSource $source The data source to save the object to.
  * @return bool TRUE is successful, FALSE otherwise.
  */
 function save(&$saveme, &$source)
 {
     parent::save($saveme, $source);
     if ($saveme->isChanged()) {
         if ($saveme->config->get('create-on-save') && $saveme->is_new) {
             $saveme->commit();
             // flatten changes into core
             $gen = new SQLGenerator($saveme->getCore());
             $savequeries = $gen->replace($this->config);
         } else {
             $params = $saveme->changeset;
             $pk = $this->config->getPrimaryKey();
             $pkalias = $pk->get('alias');
             $params[$pkalias] = $saveme->get($pkalias);
             foreach ($this->config->getForeignKeys() as $fk) {
                 $fkalias = $fk->get('alias');
                 $params[$fkalias] = $saveme->get($fkalias);
             }
             $gen = new SQLGenerator($params);
             $savequeries = $gen->update($this->config);
         }
         foreach ($savequeries as $sq) {
             $cursor =& $source->query($sq);
             if (!$cursor->getNext()) {
                 LogError("save query failed! saving " . print_r($saveme, TRUE));
                 return FALSE;
             }
         }
         $saveme->is_new = FALSE;
         if ($pkval = $cursor->get('generated-id')) {
             $pkfield =& $this->config->get('primary-key');
             $pka =& $pkfield->get('alias');
             // for "replace into", treat this save as a load
             $saveme->is_loaded = TRUE;
             $opkval = $saveme->get($pka);
             if ($opkval == 'new') {
                 $opkval = NULL;
             }
             if (!is_null($opkval) && $pkval != $opkval) {
                 LogError("Primary key mismatch: {$pkval} != {$opkval} saving " . $saveme->trace());
                 LogFatal("Primary key mismatch: {$pkval} != {$opkval}");
             }
             // reset the primary key with the value assigned by the
             // data source
             $saveme->set($pkval, $pka);
             $saveme->commit();
             if (IsLogEnabled('DEBUG')) {
                 LogDebug("set {$pka} to {$pkval}: " . $saveme->trace());
             }
         }
         if ($rows = $cursor->get('affected-rows') > 1) {
             LogWarning($rows . " records written saving " . print_r($saveme, TRUE));
         }
     }
     return TRUE;
 }
示例#2
0
 /**
  * Save this object's data in the data source.
  */
 function save()
 {
     $this->connectDataSource();
     $this->storage_method->save($this, $this->datasource);
     $this->commit();
 }