1

Данные из массива не загружаются в модель.

Модель:

private $socialType = [
    'facebook' => 1,
    'twitter' => 2,
    'google+' => 3
];

public static function tableName()
{
    return 'social_user';
}

public function rules()
{
    return [
        [['name', 'email', 'id'], 'required'],
        [['name', 'email', 'id'], 'string', 'max' => 255],
        //[['create_at', 'type', 'social_id'], 'safe']
    ];
}

public function attributeLabels()
{
    return [
        'social_user_id' => 'Social User ID',
        'name' => 'Name',
        'email' => 'Email',
        'social_id' => 'Social ID',
        'create_at' => 'Create At',
        'type' => 'Type',
    ];
}

Контроллер:

public function authSuccess($client) {
    $userAttributes = new SocialUser();
    $u = $client->getUserAttributes();
    if($userAttributes->load($client->getUserAttributes())) {

    }
}

$u:

array (size=3) 'name' => string 'name' (length=16) 'email' => string 'email@email.ru' (length=17) 'id' => string '313312414' (length=16)

1 ответ 1

2

Попробуйте сделать $userAttributes->load($client->getUserAttributes(),'') Указав в качестве имени формы пустую строку.

вот код как все это работает :

public function load($data, $formName = null)
{
    $scope = $formName === null ? $this->formName() : $formName;
    if ($scope === '' && !empty($data)) {
        $this->setAttributes($data);
        return true;
    } elseif (isset($data[$scope])) {
        $this->setAttributes($data[$scope]);
        return true;
    } else {
        return false;
    }
}

В Вашем случае должно все осуществиться в первой проверке $scope === '' и выполнится $this->setAttributes($data)

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.