Beispiel #1
0
 public function countRelated(One_Relation_Adapter $link, One_Model $model, array $options = array())
 {
     $linkName = $link->getName();
     // identify the target scheme
     $source = One_Repository::getScheme($model->getSchemeName());
     $target = One_Repository::getScheme($link->getTarget());
     $backlinks = $target->getLinks();
     $backlink = $backlinks[$link->getLinkId()];
     if (!$backlink) {
         throw new One_Exception('The role "' . $roleName . '" does not exist for this model');
     }
     $at = $source->getIdentityAttribute()->getName();
     $column = $this->remoteFK($link, $source, $backlink);
     // bind the data using the data
     $localValue = $model->{$at};
     // create query and execute
     $q = One_Repository::selectQuery($link->getTarget());
     $q->setOptions($options);
     if (isset($link->meta['hybrid'])) {
         $var = $link->meta['hybrid'] . '_scheme';
         $q->where($var, 'eq', $source->getName());
     }
     $q->where($column, 'eq', $localValue);
     return $q->getCount();
 }
Beispiel #2
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);
 }
Beispiel #3
0
 public function countRelated(One_Relation_Adapter $link, One_Model $model, array $options = array())
 {
     $linkName = $link->getName();
     // identify the target scheme
     $source = One_Repository::getScheme($model->getSchemeName());
     // PD 22OCT08
     if ($link->getTarget() == '*') {
         $col = $link->getName() . '_scheme';
         $target = One_Repository::getScheme($model->{$col});
     } else {
         $target = One_Repository::getScheme($link->getTarget());
     }
     // determine the target's identity column
     $column = $this->localFK($link, $target);
     // bind the data using the data
     $localValue = $model[$column];
     // create query and execute
     return One_Repository::selectOne($target->getName(), $localValue) ? 1 : 0;
 }
Beispiel #4
0
 /**
  * delete a single instance
  *
  * @param One_Model $model
  */
 public function delete(One_Model $model)
 {
     $scheme = One_Repository::getScheme($model->getSchemeName());
     $db = $this->db($scheme);
     // determine table to insert into
     $table = $this->getTable($scheme);
     $sql = 'DELETE FROM ' . $table;
     $idAttr = $scheme->getIdentityAttribute();
     $id = $idAttr->getName();
     $value = $model->{$id};
     $value = $idAttr->toString(mysql_real_escape_string($value, $db));
     $sql .= ' WHERE `' . $id . '` = ' . $value;
     // execute query
     if (!mysql_query($sql, $db)) {
         throw new One_Exception(mysql_error() . $sql);
     }
 }
Beispiel #5
0
 /**
  * delete a single instance
  *
  * @param One_Model $model
  * @return void
  */
 public function delete(One_Model $model)
 {
     $scheme = One_Repository::getScheme($model->getSchemeName());
     $db = $this->db($scheme);
     // determine table to insert into
     $table = $this->getTable($scheme);
     $sql = 'DELETE FROM ' . $table . ' ';
     $idAttr = $scheme->getIdentityAttribute();
     //		$id = $idAttr->getName();
     $id = $idAttr->getColumn();
     $value = $model->{$id};
     $value = $idAttr->toString($value);
     $where = 'WHERE `' . $id . '` = ' . $value;
     $db->setQuery($sql . $where);
     $db->query();
     return null;
 }
Beispiel #6
0
 public function countRelated(One_Relation_Adapter $link, One_Model $model, array $options = array())
 {
     $nRelated = 0;
     $linkName = $link->getName();
     // identify the target scheme
     $source = One_Repository::getScheme($model->getSchemeName());
     $target = One_Repository::getScheme($link->getTarget());
     $backlinks = $target->getLinks();
     $backlink = $backlinks[$link->getLinkId()];
     if (!$backlink) {
         throw new One_Exception("There is no link with id " . $link->getLinkId() . " in scheme " . $link->getTarget());
     }
     $sourceId = $source->getIdentityAttribute()->getName();
     $localValue = $model[$sourceId];
     $targetId = $target->getIdentityAttribute()->getName();
     // TR20100615 if all stores are equal, do normal join, if not, fetch data in several steps
     // @TODO This should actually be handled in One_Query
     if ($source->getConnection()->getName() == $link->meta['connection'] && $link->meta['connection'] == $target->getConnection()->getName()) {
         $lQ = One_Repository::selectQuery($source);
         $lQ->setSelect(array($linkName . ':*'));
         $lQ->where($sourceId, 'eq', $localValue);
         if (array_key_exists('query', $options) && is_array($options['query']) && count($options['query']) > 0) {
             foreach ($options['query'] as $qKey => $qOption) {
                 if (count($qOption) == 3) {
                     $options['query'][$qKey][0] = $linkName . ':' . $options['query'][$qKey][0];
                 }
             }
         }
         $lQ->setOptions($options);
         $nRelated = $lQ->getCount();
     } else {
         $fkLocal = $link->meta['fk:local'];
         $fkRemote = $link->meta['fk:remote'];
         $omtms = One_Repository::getScheme('onemanytomany');
         $omtms->setConnection(One_Repository::getConnection($link->meta['connection']));
         $omtms->setResources($link->meta);
         $localAtt = new One_Scheme_Attribute($fkLocal, $source->getIdentityAttribute()->getType()->getName());
         $remoteAtt = new One_Scheme_Attribute($fkRemote, $target->getIdentityAttribute()->getType()->getName());
         $omtms->addAttribute($localAtt);
         $omtms->addAttribute($remoteAtt);
         $joinQ = One_Repository::selectQuery($omtms);
         $joinQ->setSelect(array($fkRemote));
         $joinQ->where($fkLocal, 'eq', $localValue);
         $rawRelatedIDs = $joinQ->execute(false);
         $related = array();
         if (count($rawRelatedIDs) > 0) {
             $relatedIDs = array();
             foreach ($rawRelatedIDs as $row) {
                 $relatedIDs[] = $row->{$fkRemote};
             }
             $lQ = One_Repository::selectQuery($target);
             $lQ->where($targetId, 'in', $relatedIDs);
             $lQ->setOptions($options);
             $nRelated = $lQ->getCount();
         }
     }
     return $nRelated;
 }