Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorbhembree
     
    Does Akelos have something similar to the attr_accessor method in Rails? I would like to declare some attributes for a class that are not part of the database schema and have the controllers and views know about them.
    • CommentAuthorJeff
     

    Please refer to here

    Simply put:

    $Person->get("name");
    $Person->set("name", value);
    

    which will call getName and setName in the Model.

    • CommentAuthorbhembree
     
    Jeff,

    Thanks for your reply. Where do you declare the virtual attributes? Do you do it with an init() function in the model? Do things like ActiveRecordHelpers then know how to find them or do you have to write your own helpers?

    I too am working through the same Rails tutorial you mentioned. I thought I would put it up on the Tutorials Wiki for Akelos when I get it all worked out.
    • CommentAuthorJeff
     

    You can simply declare the virtual attributes in the class and declaring the variable in the init() method should also work just fine. Other ActiveRecord methods work just fine with them if that's what you mean by ActiveRecordHelpers.

    class User extends ActiveRecord
    {
        var $password = null;
    
        function setPassword($value) { /* ... */ }
        function validate() {
            $this->validatesLengthOf('password', array('minimum'=>5));
        }
    }
    

    I should also mention that I used the automatic getters and setters configuration since my web app didn't have many relational models. I doubt if that really affects how akelos accesses the attributes (besides using the mass assign/retrieval methods).

    Also, since it isn't an actual database field. using the view's input helper wouldn't work properly like other database fields:

    <!-- does not work. returns nothing -->
    <%= input 'user', 'password' %>
    

    Instead, you'll have to specify the exact kind of input field:

    <!-- generates a password input field -->
    <%= password_field 'user', 'password' %>
    

    As a side note, Bermi has implemented a User model in his project that avoided this issue entirely by using the beforeCreate() and beforeUpdate() methods that encrypt the password before it is saved to the database.

    Hopefully I addressed your question. :)

    • CommentAuthorbhembree
     
    Thanks Jeff. I think that is all I need to know.
    • CommentAuthorbhembree
     
    Ok, What am I missing?

    If I declare the variable in the model like:

    var $password = null;

    and in the controller I do something like

    $this->User->setAttributes($this->params['user']);

    where params['user'] is an array with a user_name and password

    The user_name gets set but not the password. The previos responses seemed to imply that I could do mass sets with the setAttributes method. Also, if i do a getAttributes(), then only the attributes that are in the database get retrieved. ???
    • CommentAuthorJeff
     

    Did you add the define statement into your config.php file?

    define('AK_ACTIVE_RECORD_ENABLE_AUTOMATIC_SETTERS_AND_GETTERS', true);
    

    This will make the enable the custom attributes to be set by getters and setters.

    • CommentAuthorbhembree
     
    Yes I did. By itself it doesn't seem to make any difference. However if define methods for setPassword() and getPassword() with something like setAttribute(), then I get an infinite loop recursion problem. If I define them with something like $password = or return $password, then nothing other than an error on the return $password.
    • CommentAuthorKaste
     

    setAttribute(...) will call setYourSetter(...)

    So

    function setPassword($value)
    {
        $this->password = $value;
    }
    
    function getPassword()
    {
        return $this->password;
    }
    

    should do the trick. Now you can do $this->set('password',$value)

    look here for a different example.

    • CommentAuthorbhembree
     
    Kaste,

    I can do $this->set('password', $value) just fine. What I can't do is $this->setAttributes($atts), where $atts is an array that has model attributes that are not part of the database record. For instance $atts = array([user_name => in_record' [password] => not_in_record). Is this command possible in Akelos?

    Barry
    • CommentAuthorKaste
     

    yes.

    $attributes = array(
           'user_name' => 'bhembree', 
           'password' => 'tries hard on virtual attributes'
    );
    
    //then all of the following should work:
    $User =& new Model($attributes);
    $this->create($attributes);  //uses setAttributes internally
    //therefore:
    $this->setAttributes($attributes);
    

    can you write a test?

    • CommentAuthorbhembree
     
    Very funny example Kaste.

    I see what my problem was. I was setting the attributes with $this->User->setAttributes(array). I was checking to see if the virtual attribute was getting set with print_r($this->User->getAttributes()). This was just showing the database record attributes. When I used echo $this->User->password, then I saw the value of the virtual attribute. Also if I say $this->User->getAttribute('password') , then I also get the value of the virtual attribute. I'm still not quite sure why getAttributes() doesn't work. It must have to do with the call getAvailableAttributes() in the getAttributes() method.
    • CommentAuthorKaste
     

    I'm still not quite sure why getAttributes() doesn't work. It must have to do with the call getAvailableAttributes() in the getAttributes() method.

    +1,

    and: I don't know if this is an issue we should solve.