/** * Find column * @param string $column column name * @param mixed $condition * @param array $params * @return array column values */ public function findColumn($column, $condition = '', $params = array()) { $criteria = $this->owner->getCommandBuilder()->createCriteria($condition, $params); $criteria->select = $column; $this->owner->applyScopes($criteria); $command = $this->owner->getCommandBuilder()->createFindCommand($this->owner->getTableSchema(), $criteria); if (empty($criteria->with)) { return $command->queryColumn(); } else { $finder = new CActiveFinder($this->owner, $criteria->with); $results = $finder->query($criteria, true); $data = array(); foreach ($results as $result) { $data[] = $result[$column]; } return $data; } }
public function exists($condition = '', $params = array()) { $builder = $this->getCommandBuilder(); $criteria = $builder->createCriteria($condition, $params); $table = $this->getTableSchema(); $criteria->select = '1'; $criteria->limit = 1; $this->applyScopes($criteria); if (empty($criteria->with)) { return $builder->createFindCommand($table, $criteria)->queryRow() !== false; } else { $criteria->select = '*'; $finder = new CActiveFinder($this, $criteria->with); return $finder->count($criteria) > 0; } }
public function countByAttributes($attributes, $condition = '', $params = array()) { $prefix = $this->getTableAlias(true) . '.'; $builder = $this->getCommandBuilder(); $criteria = $builder->createColumnCriteria($this->getTableSchema(), $attributes, $condition, $params, $prefix); $this->applyScopes($criteria); if (empty($criteria->with)) { return $builder->createCountCommand($this->getTableSchema(), $criteria)->queryScalar(); } else { $finder = new CActiveFinder($this, $criteria->with); return $finder->count($criteria); } }
/** * Returns the related record(s). * This method will return the related record(s) of the current record. * If the relation is HAS_ONE or BELONGS_TO, it will return a single object * or null if the object does not exist. * If the relation is HAS_MANY or MANY_MANY, it will return an array of objects * or an empty array. * @param string the relation name (see {@link relations}) * @param boolean whether to reload the related objects from database. Defaults to false. * @param array additional parameters that customize the query conditions as specified in the relation declaration. * This parameter has been available since version 1.0.5. * @return mixed the related object(s). * @throws CDbException if the relation is not specified in {@link relations}. * @since 1.0.2 */ public function getRelated($name, $refresh = false, $params = array()) { if (!$refresh && $params === array() && (isset($this->_related[$name]) || array_key_exists($name, $this->_related))) { return $this->_related[$name]; } $md = $this->getMetaData(); if (!isset($md->relations[$name])) { throw new CDbException(Yii::t('yii', '{class} does not have relation "{name}".', array('{class}' => get_class($this), '{name}' => $name))); } Yii::trace('lazy loading ' . get_class($this) . '.' . $name, 'system.db.ar.CActiveRecord'); $relation = $md->relations[$name]; if ($this->getIsNewRecord() && ($relation instanceof CHasOneRelation || $relation instanceof CHasManyRelation)) { return $relation instanceof CHasOneRelation ? null : array(); } if ($params !== array()) { $exists = isset($this->_related[$name]) || array_key_exists($name, $this->_related); if ($exists) { $save = $this->_related[$name]; } unset($this->_related[$name]); $r = array($name => $params); } else { $r = $name; } $finder = new CActiveFinder($this, $r); $finder->lazyFind($this); if (!isset($this->_related[$name])) { if ($relation instanceof CHasManyRelation) { $this->_related[$name] = array(); } else { if ($relation instanceof CStatRelation) { $this->_related[$name] = $relation->defaultValue; } else { $this->_related[$name] = null; } } } if ($params !== array()) { $results = $this->_related[$name]; if ($exists) { $this->_related[$name] = $save; } else { unset($this->_related[$name]); } return $results; } else { return $this->_related[$name]; } }
/** * @param integer $row the row number (zero-based). * @param array $data result of CDbDataReader.read() or an item from the CDataProvider.data array * @param CActiveFinder $finder * @param boolean $isActiveDataProvider * @param boolean $isStreaming true when CDataReader is used instead of CDataProvider.data * @return mixed a model or array */ protected function prepareRow($row, $data, $finder, $isActiveDataProvider) { if ($isActiveDataProvider) { $data = $finder === null ? $this->dataProvider->model->populateRecord($data) : $finder->populateRecord($data); } $this->dataProvider->setData(array($row => $data)); return $data; }
/** * Checks whether there is row satisfying the specified condition. * See {@link find()} for detailed explanation about $condition and $params. * @param mixed $condition query condition or criteria. * @param array $params parameters to be bound to an SQL statement. * @return boolean whether there is row satisfying the specified condition. */ public function exists($condition = '', $params = array()) { Yii::trace(get_class($this) . '.exists()', 'system.db.ar.CActiveRecord'); $builder = $this->getCommandBuilder(); $criteria = $builder->createCriteria($condition, $params); $table = $this->getTableSchema(); $criteria->select = '1'; $criteria->limit = 1; $this->applyScopes($criteria); if (empty($criteria->with)) { return $builder->createFindCommand($table, $criteria, $this->getTableAlias(false, false))->queryRow() !== false; } else { $criteria->select = '*'; $finder = new CActiveFinder($this, $criteria->with); return $finder->count($criteria) > 0; } }