/**
  * Given active record class name returns new model instance.
  *
  * @param string $className active record class name.
  * @return MongoRecord active record model instance.
  *
  * @since 1.1.14
  */
 protected function getModel($className)
 {
     return MongoRecord::model($className);
 }
Exemplo n.º 2
0
 /**
  * Связи по ObjectId соединяются только через строки!
  *
  * @param string $name
  * @return MongoRecord|MongoRecord[]|bool
  * @throws \CDbException
  */
 public function getRelated($name)
 {
     $params = null;
     foreach ($this->relations() as $relationName => $relation) {
         if ($name === $relationName) {
             $params = $relation;
             break;
         }
     }
     if ($params === null) {
         return null;
     }
     list($relationType, $relationCollection, $foreignKey) = $params;
     $criteria = new MongoDbCriteria();
     $modelObject = MongoRecord::model($relationCollection);
     $result = null;
     switch ($relationType) {
         case self::BELONGS_TO:
             if (!isset($this->{$foreignKey})) {
                 throw new CDbException("Неправильно задана связь {$name} c ключем: {$foreignKey}=>_id");
             }
             $criteria->addCondition('_id', '=', (string) $this->{$foreignKey});
             $criteria->setLimit(1);
             $result = $modelObject->find($criteria, false);
             break;
         case self::HAS_ONE:
             if (!property_exists($modelObject, $foreignKey)) {
                 throw new CDbException("Неправильно задана связь {$name} c ключем: _id=>{$foreignKey}");
             }
             $criteria->addCondition($foreignKey, '=', (string) $this->id);
             $criteria->setLimit(1);
             $result = $modelObject->find($criteria, false);
             break;
         case self::HAS_MANY:
             if (!property_exists($modelObject, $foreignKey)) {
                 throw new CDbException("Неправильно задана связь {$name} c ключем: _id=>{$foreignKey}");
             }
             $criteria->addCondition($foreignKey, '=', $this->id);
             $result = $modelObject->findAll($criteria);
             break;
         case self::HAS_RELATION_WITH:
             $cursor = $modelObject->getCollection()->find([$foreignKey => (string) $this->id], [$foreignKey]);
             $cursor->limit(1);
             $result = $cursor->count() === 1;
             break;
         default:
             throw new CDbException("Wrong relation type for relation: {$name}");
     }
     $this->relations[$name] = $result;
     return $result;
 }