Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Can't set virtual attribute for ActiveRecord #33

Closed
phrakon opened this issue Aug 8, 2018 · 2 comments
Closed

Can't set virtual attribute for ActiveRecord #33

phrakon opened this issue Aug 8, 2018 · 2 comments
Labels
Milestone

Comments

@phrakon
Copy link

phrakon commented Aug 8, 2018

My ActiveRecord class has the attribute

class Profile extends ActiveRecord
{
   /**
     * @var bool
     */
    public $agree;

public function rules()
    {
        return [
         
            [['agree'], 'required', 'on' => 'insert'],
         
        ];
    }
}

But i have got an error "Failed to set unsafe attribute 'agree' in 'app\models\user\Profile'."

I found out we have to set scenario here if scenario provided by behavior

$relationModel->setAttributes($data);

Anyway it helps me to work/ Isn't it the right case?

@phrakon
Copy link
Author

phrakon commented Aug 9, 2018

Update. There is no sense depends on type of the attribute. Scenario should be applied before assigning attributes (after object will be instatiated to be sure in the future) because setAttributes checks safe ability of attributes depends on current scenario. You do it before validate. Its too late.

@juban juban added the bug label Aug 9, 2018
@juban juban modified the milestones: 1.4.1, 1.5.1 Aug 9, 2018
juban pushed a commit that referenced this issue Aug 9, 2018
Added the ability to set a relation scenario at runtime using the setRelationScenario method
Added related tests
@juban
Copy link
Contributor

juban commented Aug 9, 2018

Hi @phrakon,

Yep, you’re right.
Bug fix is on its way. The master branch contains a first fix but there’s still one issue. I will hopefully publish the full fix tomorrow.

Thanks.

juban pushed a commit that referenced this issue Aug 10, 2018
@juban juban closed this as completed Aug 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants