Exemplo n.º 1
0
 /**
  * Retrieve the given relation.
  * 
  * @param string $attribute
  * @return Relation
  */
 public function relation($attribute)
 {
     if (!$this->hasRelation($attribute)) {
         return null;
     }
     $attribute = $this->prepareAttribute($attribute);
     $relation = $this->relations[$attribute];
     if (!$relation instanceof Relation) {
         $type = array_shift($relation);
         $arguments = array_merge(array($this), $relation);
         $arguments['name'] = $attribute;
         $relation = Relation::factory($type, $arguments);
         $this->relations[$attribute] = $relation;
     }
     return $relation;
 }
Exemplo n.º 2
0
 /**
  * Count the number of related model instances.
  * 
  * Counts loaded instances if they are present, queries storage otherwise.
  * 
  * @return int
  */
 public function count()
 {
     if ($this->loaded()) {
         return parent::count();
     }
     if (empty($this->filter())) {
         return $this->storage()->count($this->table, $this->associationFilter());
     }
     $filter = $this->filter($this->relatedIds());
     return $this->storage()->count($this->target->table(), $filter);
 }