コード例 #1
0
ファイル: joomla2.php プロジェクト: pdelbar/onethree
 /**
  * Update a single instance
  *
  * @param One_Model $model
  */
 public function update(One_Model $model)
 {
     $scheme = One_Repository::getScheme($model->getSchemeName());
     $db = $this->db($scheme);
     // determine table to insert into
     $table = $this->getTable($scheme);
     //create clauses
     $modified = $model->getModified();
     $modifiedRelations = $model->getDeltaRelations();
     $data = new stdClass();
     foreach ($scheme->get('attributes') as $attName => $at) {
         if (isset($modified[$attName])) {
             $colName = $at->getColumn();
             $data->{$colName} = $modified[$attName];
             //				$data->$attName = $modified[ $attName ];
         }
     }
     // Check for relationships (FK values), cannot use attribute but must use column or link name
     // JL 06JAN2008 - Three possible situations, two are needed:
     // * ManyToOne
     //		* The FK is a field in the model's record
     //		* We need to set this field BEFORE saving the record
     // * ManyToMany
     //		* Relations are in a separate table
     //		* We should set them AFTER saving the record (especially when inserting a new record)
     // * OneToMany
     // 		* Not needed for now - When editing, we'll usually edit the child and select it's parent
     foreach ($scheme->getLinks() as $link) {
         if ($link->getAdapterType() == "manytoone") {
             $fk = $link->fk();
             if (isset($modified[$fk])) {
                 $data->{$fk} = $modified[$fk];
             }
         }
     }
     $idAttr = $scheme->getIdentityAttribute();
     //		$id = $idAttr->getName();
     $id = $idAttr->getColumn();
     $value = $model->{$id};
     $value = $idAttr->toString($value);
     if ($idAttr->getType() instanceof One_SchemeAttribute_Type_String) {
         $value = preg_replace('/^\\"(.*)\\"$/i', '$1', $value);
     }
     $data->{$id} = $value;
     $nrChanged = 0;
     foreach ($data as $key => $val) {
         if ($key != $id) {
             $nrChanged++;
         }
     }
     if ($nrChanged > 0) {
         $db->updateObject($table, $data, $id);
     }
     if (trim($db->errorMsg()) != '') {
         throw new One_Exception($db->errorMsg());
     }
     // Handle ManyToMany relations
     foreach ($scheme->getLinks() as $link) {
         /*var_dump($link);
           exit();*/
         if ($link->getAdapterType() == "manytomany") {
             if (isset($modifiedRelations[$link->getName()])) {
                 $model->saveRelated($link);
             }
         }
     }
     return null;
 }