Example #1
0
 /**
  * Add a model to the map
  *
  * @param One_Model $model
  */
 public static function add(One_Model $model)
 {
     $scheme = $model->getSchemeName();
     $identity_name = $model->getIdentityName();
     $identity = $model->{$identity_name};
     if (!$identity) {
         throw new One_Exception("The model has no identity yet and cannot be stored in the identity map.");
     }
     $key = "model.{$scheme}.{$identity}";
     if (!self::$registry) {
         self::$registry = new One_Registry();
     }
     self::$registry->set($key, $model);
 }
Example #2
0
 /**
  * Delete a relationship from the model
  *
  * @param One_Model $model
  * @param One_Relation_Adapter $link
  */
 public function deleteRelations(One_Model $model, One_Relation_Adapter $link)
 {
     $deleted = $model->getDeletedRelations();
     if (isset($deleted[$link->getName()])) {
         // @todo - this probably isn't the correct way to get to the db object we need?
         // the db object should be based on the info in the $link, not the $model ...
         $scheme = One_Repository::getScheme($model->getSchemeName());
         $db = $this->db($scheme);
         $table = $link->meta['table'];
         $localKey = $link->fk('local');
         $remoteKey = $link->fk('remote');
         $localId = $model->getIdentityName();
         $localValue = $model->{$localId};
         // Insert the new (modified) relations in the given model
         $values = array();
         foreach ($deleted[$link->getName()] as $remoteValue) {
             if (is_array($remoteValue)) {
                 foreach ($remoteValue as $rVal) {
                     $values[] = '`' . $remoteKey . '` = "' . mysql_real_escape_string($rVal, $db) . '"';
                 }
             } else {
                 $values[] = '`' . $remoteKey . '` = "' . mysql_real_escape_string($remoteValue, $db) . '"';
             }
         }
         // only run the insert query if we actually received new values
         if (count($values)) {
             $sql = 'DELETE FROM `' . $table . '` ' . 'WHERE `' . $localKey . '` = "' . mysql_real_escape_string($localValue, $db) . '"' . 'AND ( ' . implode(' OR ', $values) . ' )';
             mysql_query($sql, $db);
         }
     }
 }
Example #3
0
 public function getAlias(One_Model $model)
 {
     $scheme = $model->getScheme();
     $aliasOpts = $scheme->get('behaviorOptions.linkalias');
     if (null !== $behaviorOpts && isset($behaviorOpts['attribute'])) {
         $aliasField = $behaviorOpts['attribute'];
         $alias = $model->{$aliasField};
     } elseif (isset($model->slug)) {
         $alias = $model->slug;
     } else {
         $idAttr = $model->getIdentityName();
         $alias = $model->{$idAttr};
     }
     return $alias;
 }