Exemple #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();
 }
Exemple #2
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;
 }
Exemple #3
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;
 }