本文整理汇总了PHP中CActiveRecord::getRelated方法的典型用法代码示例。如果您正苦于以下问题:PHP CActiveRecord::getRelated方法的具体用法?PHP CActiveRecord::getRelated怎么用?PHP CActiveRecord::getRelated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CActiveRecord
的用法示例。
在下文中一共展示了CActiveRecord::getRelated方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validateAttribute
/**
* @param CActiveRecord $object
* @param string $attribute
*/
protected function validateAttribute($object, $attribute)
{
/**
* Значение аттрибута
*/
$value = $object->{$attribute};
foreach ($object->getRelated('questions') as $question) {
/** @var $question QuizQuestion */
$pk = $question->getPrimaryKey();
if (!is_array($value) || !array_key_exists($pk, $value) || $this->isEmpty($value[$pk], true)) {
// если не пришли данные для этого вопроса, или пришли, но пустые
$this->addError($object, $attribute, $pk);
} else {
// собираем PK ответов на этот вопрос
$answers_pk = array();
foreach ($question->getRelated('answers') as $answer) {
/** @var $answer QuizAnswer */
$answers_pk[] = $answer->getPrimaryKey();
}
if ($question->type == QuizQuestion::TYPE_SINGLE && !in_array($value[$pk], $answers_pk)) {
unset($object->{$attribute}[$pk]);
// стираем текущие значения
$this->addError($object, $attribute, $pk);
}
if ($question->type == QuizQuestion::TYPE_MULTIPLE) {
// на вопрос можно несколько ответов
/**
* "Лишние" ответы, не присутсвующие в списке вопросов
*/
$wrong_answers = array_diff($value[$pk], $answers_pk);
if (!empty($wrong_answers)) {
unset($object->{$attribute}[$pk]);
// стираем текущие значения
$this->addError($object, $attribute, $pk);
}
}
/**
* Дополнительной проверки произвольного ответа не производится
* он не пуст и нас это устраивает
*/
}
}
}
示例2: convertModel
/**
*
* @param CActiveRecord $model
* @param mixed $attributes If is set to null all attributes of the model will be added. If a string is given, only this attribute will added. If an array of strings is given, all elements will be retrieved.
* @param mixed $relations If is set to true, all relations will be retrieved. If a string is given, only one relation will be added. For array see the class definition of relations.
*/
protected function convertModel($model, $attributes = null, $relations = null)
{
$relationArray = array();
if ($attributes === null) {
$attributes = array();
foreach ($model->attributes as $attr => $type) {
$attributes[] = $attr;
}
}
if ($relations === true || is_array($relations) || is_string($relations)) {
if ($relations === true) {
//include all relations
$relations = array();
foreach ($model->getMetaData()->relations as $name => $relation) {
$relations[] = $name;
}
}
if (is_string($relations)) {
$relations = array($relations);
}
foreach ($relations as $key => $relation) {
$relAttributes = null;
$relRelations = null;
/**
* @var array|mixed array or CDbCriteria object with additional parameters that customize the query
* conditions as specified in the relation declaration. See more at http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getRelated-detail
*/
$relParams = array();
/**
* @var boolean whether to reload the related objects from database. See more at
* http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getRelated-detail
*/
$relRefresh = false;
$relationName = $relation;
if (is_array($relation)) {
$relationName = $key;
if (!isset($relation['attributes']) && !isset($relation['relations']) && !isset($relation['refresh']) && !isset($relation['params'])) {
// for convenient configuration
$relAttributes = $relation;
} else {
$relAttributes = isset($relation['attributes']) ? $relation['attributes'] : null;
$relRelations = isset($relation['relations']) ? $relation['relations'] : null;
$relParams = isset($relation['params']) ? $relation['params'] : array();
$relRefresh = isset($relation['refresh']) ? $relation['refresh'] : false;
}
}
$relatedModels = $model->getRelated($relationName, $relRefresh, $relParams);
if (is_array($relatedModels)) {
foreach ($relatedModels as $relatedModel) {
$relationArray[$relationName][] = $this->convertModel($relatedModel, $relAttributes, $relRelations);
}
} else {
if ($relatedModels) {
$relationArray[$relationName] = $this->convertModel($relatedModels, $relAttributes, $relRelations);
}
}
}
}
if (is_string($attributes)) {
$attributes = array($attributes);
}
if ($attributes === null) {
$attributes = true;
}
foreach ($attributes as $attribute) {
$attributeArray[$attribute] = $model->{$attribute};
}
if ($this->attributeAliases) {
$tempArray = array();
foreach ($attributeArray as $attributeName => $value) {
if (isset($this->attributeAliases[$attributeName])) {
$tempArray[$this->attributeAliases[$attributeName]] = $value;
} else {
$tempArray[$attributeName] = $value;
}
}
$attributeArray = $tempArray;
}
if ($this->onAfterModelToArray) {
call_user_func_array($this->onAfterModelToArray, array($model, &$attributeArray, &$relationArray));
}
return array_merge($attributeArray, $relationArray);
}
示例3: convertModel
/**
*
* @param CActiveRecord $model
* @param mixed $attributes If is set to null all attributes of the model will be added. If a string is given, only this attribute will added. If an array of strings is given, all elements will be retrieved.
* @param mixed $relations If is set to true, all relations will be retrieved. If a string is given, only one relation will be added. For array see the class definition of relations.
*/
protected function convertModel($model, $attributes = null, $relations = null)
{
$relationArray = array();
if ($attributes === null) {
$attributes = array();
foreach ($model->attributes as $attr => $type) {
$attributes[] = $attr;
}
}
if ($relations === true || is_array($relations) || is_string($relations)) {
if ($relations === true) {
//include all relations
$relations = array();
foreach ($model->getMetaData()->relations as $name => $relation) {
$relations[] = $name;
}
}
if (is_string($relations)) {
$relations = array($relations);
}
foreach ($relations as $key => $relation) {
$relAttributes = null;
$relRelations = null;
$relationName = $relation;
if (is_array($relation)) {
$relationName = $key;
if (!isset($relation['attributes']) && !isset($relation['relations'])) {
// for convenient configuration
$relAttributes = $relation;
} else {
$relAttributes = isset($relation['attributes']) ? $relation['attributes'] : null;
$relRelations = isset($relation['relations']) ? $relation['relations'] : null;
}
}
$relatedModels = $model->getRelated($relationName);
if (is_array($relatedModels)) {
foreach ($relatedModels as $relatedModel) {
$relationArray[$relationName][] = $this->convertModel($relatedModel, $relAttributes, $relRelations);
}
} else {
if ($relatedModels) {
$relationArray[$relationName] = $this->convertModel($relatedModels, $relAttributes, $relRelations);
}
}
}
}
if (is_string($attributes)) {
$attributes = array($attributes);
}
if ($attributes === null) {
$attributes = true;
}
foreach ($attributes as $attribute) {
$attributeArray[$attribute] = $model->{$attribute};
}
if ($this->attributeAliases) {
$tempArray = array();
foreach ($attributeArray as $attributeName => $value) {
if (isset($this->attributeAliases[$attributeName])) {
$tempArray[$this->attributeAliases[$attributeName]] = $value;
} else {
$tempArray[$attributeName] = $value;
}
}
$attributeArray = $tempArray;
}
if ($this->onAfterModelToArray) {
call_user_func_array($this->onAfterModelToArray, array($model, &$attributeArray, &$relationArray));
}
return array_merge($attributeArray, $relationArray);
}