Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CActiveRecord allow attributes as getters and setters #1708

Closed
Ekstazi opened this issue Nov 15, 2012 · 8 comments
Closed

CActiveRecord allow attributes as getters and setters #1708

Ekstazi opened this issue Nov 15, 2012 · 8 comments

Comments

@Ekstazi
Copy link
Contributor

Ekstazi commented Nov 15, 2012

Hi! I try to select query rows with my custom user property(with getter and setter) defined in model. But i can't, because populateRecord doesn't know anything about setters.

@Ekstazi
Copy link
Contributor Author

Ekstazi commented Nov 15, 2012

example model:

class MyModel extends ActiveRecord
private $_prop;
public function setProp($val)
{
$this->_prop=$val;
}

public function getProp()
{
if(isset($this->_prop)
return $this->_prop;
// calculate my property
// ....
return $this->_prop;
}
// ...

and i try to select like:
MyModel::model()->findAll(array( 'select'=>'max(test) as prop,mymodelfield1,mymodelfield2', ))

@creocoder
Copy link
Contributor

👍 for this issue

@cebe
Copy link
Member

cebe commented Nov 16, 2012

Changed the issue title. This is related to #1245

@Ziggizag
Copy link

In my humble opinion, this is also linked to this issue:

#1899

@klimov-paul
Copy link
Member

It is better to resolve #627 (CActiveRecord not respecting attributeNames() overrides).
Then you can override "attributeNames()" and specify any set of attributes including the ones added by getters/setters.

Do not forget there should be an option to add virtual property, which is NOT an attribute.

@cebe
Copy link
Member

cebe commented May 27, 2013

Closing this in favor of #627.
As AR only considers the columns of the table as attributes it should be possible by overriding attributeNames().

@Ekstazi
Copy link
Contributor Author

Ekstazi commented Feb 15, 2014

The problem still present. So #627 didnt help,

@victorsmirnov
Copy link

@cebe can you please explain how to use attributeNames() to make populateRecord() method assign values using setter?

I've got simple use case:

    private $_email;

    public function getEmail()
    {
        return $this->_email;
    }

    public function setEmail($email)
    {
        if (empty($email)) {
            $this->_email = null;
            $this->status_email = Subscriber::STATUS_EMAIL_EMPTY;
        } else {
            $this->_email = $email;
            if ($this->status_email == Subscriber::STATUS_EMAIL_EMPTY) {
                $this->status_email = Subscriber::STATUS_EMAIL_ACTIVE;
            }
        }
    }

Is there anything I can do to make it working? Right now when I read record from the database the $_attributes property is updated and email property is added ($subscriber->_attributes['email'] is set). When I set email value using setter (or using setAttributes()) the $_email property is updated but the changes are not saved to the DB using save() method.

It looks like the only option for me now is to use beforeSave() method. But anyway I'm curious if I have any other options here.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants