[решено] REST API expand

Всё что касается построения API
Ответить
Аватара пользователя
yiijeka
Сообщения: 3103
Зарегистрирован: 2012.01.28, 09:14
Откуда: Беларусь
Контактная информация:

[решено] REST API expand

Сообщение yiijeka »

Как заставить expand заработать?

Документация: https://github.com/yiisoft/yii2/blob/ma ... ponse-data

Делаю http://localhost/users?fields=id&expand=pipings
не возвращается

Код: Выделить всё

"pipings": {
           ...
        }
в модели связь установлена:

Код: Выделить всё

  public function getPipings()
    {
        return $this->hasMany(Piping::className(), ['sector_id' => 'id']);
    }
Если задать руками

Код: Выделить всё

 public function fields()
    {
        return [
            // field name is the same as the attribute name
            'id',
            'pipings' => function () {
                    return $this->getPipings();
                },
        ];
    } 
то тогда http://localhost/users возвращает зависимый объект, но тут не указывается expand.

Есть идеи?
Последний раз редактировалось yiijeka 2014.03.19, 14:48, всего редактировалось 1 раз.
Аватара пользователя
yiijeka
Сообщения: 3103
Зарегистрирован: 2012.01.28, 09:14
Откуда: Беларусь
Контактная информация:

Re: REST API expand

Сообщение yiijeka »

Ах нужно было разрешить получения связи в

Код: Выделить всё

class .. extends \yii\db\ActiveRecord
....
 public function extraFields()
    {
        return [
            // field name is the same as the attribute name
            'pipings'
        ];
    }
The method [[yii\base\Model::extraFields()]] is very similar to [[yii\base\Model::fields()]]. The difference between these methods is that the latter declares the fields that should be returned by default, while the former declares the fields that should only be returned when the user specifies them in the expand query parameter.
Ответить