/** * * @return integer * @throws Kohana_Exception */ public function count() { if (Arr::get($this->relation1, 'store') !== false) { if (isset($this->relation1['field'])) { $field = $this->relation1['field']; $arr = $this->model1->{$field}; return count($arr); } else { $id = "{$this->model1->get_model_name()}:{$this->model1->pk()}:{$this->field1}"; $self_collection = self::COLLECTION; $code = "function(){return db.{$self_collection}.find({'_id':'{$id}'}).limit(1)[0].ids.length}"; $return_value = 0; try { $return_value = intval($this->model1->db()->execute($code)); } catch (Exception $ex) { } return $return_value; } } elseif (isset($this->relation2) && Arr::get($this->relation2, 'store') !== false) { if (isset($this->relation2['field'])) { return $this->model2->get_collection()->count(array($this->relation2['field'] => $this->model1->pk())); } else { return $this->model2->db()->selectCollection(self::COLLECTION)->count(array("ids" => $this->model1->pk(), 'model' => $this->relation2['model'], 'field' => $this->field2)); } } else { throw new Kohana_Exception("can`t get ids for this relation"); } }