/** * Gets the configurations for the given model's attributes * @param ActiveRecord $model the active record to get attributes for * * @return array the attribute config */ protected function getAttributeConfigs(ActiveRecord $model) { $attributes = array(); $pk = $model->getTableSchema()->primaryKey; foreach ($model->getVisibleAttributeNames() as $attribute) { $validators = array(); foreach ($model->getValidators($attribute) as $validator) { if ($validator->enableClientValidation && ($js = $validator->clientValidateAttribute($model, $attribute)) !== null) { $validators[] = $js; } } if (is_array($pk)) { $isPk = in_array($attribute, $pk); } else { $isPk = $attribute == $pk; } $attributes[$attribute] = array('label' => $model->getAttributeLabel($attribute), 'description' => $model->getAttributeDescription($attribute), 'primitive' => $model->getAttributePrimitive($attribute), 'type' => $model->getAttributeType($attribute), 'format' => $model->getAttributeFormat($attribute), 'input' => $model->getAttributeInput($attribute), 'validators' => $validators, 'isWritable' => $model->isAttributeSafe($attribute), 'isRequired' => $model->isAttributeRequired($attribute), 'isPrimaryKey' => (bool) $isPk); } return $attributes; }
/** * Creates the relation criteria * * @param ActiveRecord $owner the owner model * @param ActiveRecord $relatedModel the related model * * @return \CDbCriteria the criteria */ public function createRelationCriteria(ActiveRecord $owner, ActiveRecord $relatedModel) { $criteria = new \CDbCriteria(); $relation = $this->getRelation(); $criteria->alias = empty($relation->alias) ? $relation->name : $relation->alias; if ($relation instanceof \CManyManyRelation) { $junctionTable = $relation->getJunctionTableName(); $junctionKeys = $relation->getJunctionForeignKeys(); $criteria->join = $relation->joinType . ' ' . $junctionTable . ' ON ' . $junctionTable . '.' . $junctionKeys[0] . ' = :relationOwnerModelId AND ' . $junctionTable . '.' . $junctionKeys[1] . ' = ' . $criteria->alias . '.' . $relatedModel->getTableSchema()->primaryKey . ' ' . $relation->join; } else { if ($relation instanceof \CHasManyRelation) { $criteria->join = $relation->join; $criteria->condition = $criteria->alias . '.' . $relation->foreignKey . ' = :relationOwnerModelId'; } else { $criteria->join = $relation->join; } } $criteria->select = $relation->select; if (empty($criteria->condition)) { $criteria->condition = $relation->condition; } else { if (!empty($relation->condition)) { $criteria->condition = '(' . $criteria->condition . ') AND (' . $relation->condition . ')'; } } $criteria->having = $relation->having; $criteria->with = $relation->with; $criteria->group = $relation->group; $criteria->order = $relation->order; $criteria->limit = $relation->limit; $criteria->params['relationOwnerModelId'] = $owner->primaryKey; return $criteria; }
/** * Gets the model primary key from the `$_GET` parameters. * Returns null if the required parameters do not exist. * * @param ActiveRecord $finder the model to get the pk values for * @param array $context the context to load from, defaults to $_GET * * @return mixed|null the pk, or null if the required params are not present */ public function getPkFromContext(ActiveRecord $finder, $context = null) { if ($context === null) { $context = $_GET; } $pkName = $finder->getTableSchema()->primaryKey; if (is_array($pkName)) { $pk = array(); foreach ($pkName as $name) { if (!isset($context[$name])) { return null; } $pk[$name] = $context[$name]; } } else { if (!isset($context[$pkName])) { return null; } $pk = $context[$pkName]; } return $pk; }