/** * @param \Restyii\Model\ActiveRecord $model the model instance * @param string $attribute the attribute name * * @return string */ public function formatAttribute($model, $attribute) { $format = $model->getAttributeFormat($attribute); if ($format == 'choice' && method_exists($model, $attribute . 'Labels')) { $methodName = $attribute . 'Labels'; return $this->formatChoice($model->{$attribute}, $model->{$methodName}()); } $methodName = 'format' . $format; if (method_exists($this, $methodName)) { return $this->{$methodName}($model->{$attribute}); } else { return $this->formatText($model->{$attribute}); } }
/** * 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 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; }
/** * 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) { $types = $model->attributeTypes(); $descriptions = $model->attributeDescriptions(); $attributes = array(); foreach ($model->getVisibleAttributeNames() as $attribute) { $validators = array(); foreach ($model->getValidators($attribute) as $validator) { if ($validator->enableClientValidation && ($js = $validator->clientValidateAttribute($model, $attribute)) !== null) { $validators[] = $js; } } $attributes[$attribute] = array('label' => $model->getAttributeLabel($attribute), 'type' => isset($types[$attribute]) ? $types[$attribute] : null, 'description' => isset($descriptions[$attribute]) ? $descriptions[$attribute] : null, 'writable' => $model->isAttributeSafe($attribute), 'required' => $model->isAttributeRequired($attribute), 'validators' => $validators); } return $attributes; }
/** * Creates a model definition * @param string $name the name of the model * @param ActiveRecord $model the model * * @return array the definition config */ public function createModelDefinition($name, ActiveRecord $model) { $config = array('type' => 'object', 'properties' => array()); foreach ($model->getVisibleAttributeNames() as $attribute) { $config['properties'][$attribute] = array('location' => 'json', 'type' => $model->getAttributePrimitive($attribute)); } return $config; }
/** * Generates the cache key for an active record * @param ActiveRecord $input the data to generate the key for * @param array $qualifiers the qualifiers that differentiate the item * * @return string the cache key */ protected function createActiveRecordKey(ActiveRecord $input, $qualifiers = array()) { $prepend = $input->controllerID() . ':'; $append = $input->getDbCriteria()->toArray(); return $this->calculateKey($prepend, array($append, $qualifiers)); }
/** * Delete a particular model * * @param ActiveRecord $model the model to delete * * @return bool true if the delete was successful, otherwise false. */ protected function delete(ActiveRecord $model) { return $model->delete(); }
/** * Creates the suggestion criteria for a given model * * @param ActiveRecord $model the model to suggest for * @param array $params the action parameters * * @return \CDbCriteria the criteria to merge */ protected function createSuggestCriteria(ActiveRecord $model, $params) { $criteria = new \CDbCriteria(); $table = $model->getTableAlias(false, false); $conditions = array(); foreach ($this->getAttributes() as $attribute) { $conditions[] = $table . '.' . $attribute . ' LIKE :term'; } $criteria->params[':term'] = $params['term'] . '%'; $criteria->addCondition(implode(' OR ', $conditions)); $criteria->limit = $params['limit']; return $criteria; }