Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorbhembree
     
    Hi All,

    I've been stumbling along trying to use Akelos. I think most of my problem is that I'm not quite sure of the basic flow of things. Pardon me if these questions are too basic but my MVC experience is from back in the Smalltalk days and I don't have any RoR experience. What follows is my uderstanding of how things work. If someone could please correct me where I'm wrong and fill in some of the blanks, that would be a big help.

    From the Booklink tutorial, when the controller (lets say foo_controller.php) is called, it instantiates and instance of the model in an attribute of the controller named $this->Foo (this brings up the first question: What is the difference between $this-foo and $this->Foo?). Now the model definition resides in the directory models and will have a name like foo.php. The next question is, How do we define private and public instance variables and methods like construct(), init(), destroy(), modify(), etc. in the model definition? How does init() get called when a new instance of the model is created by the controller? Akelos tries to infer the instance variables from the database schema. But what if I want to add and initialize additional instance variables?

    So as a simple example, say I have a model class called Foo. It has a private instance variable called $timeOfBirth that gets set by a call to the function time() when the models gets instantiated by the controller. It also has a public instance variable $timeAlive that gets set by a call to a public function timeSinceBirth that simply subtracts the $timeOfBirth from a new call to time(). I would like it so that every time I made a call to the template show.tpl, it would simply display something like <h1>I've been alive for {Foo.timeAlive}</h1>. How would I do this in Akelos?


    Thanks for your help.

    Barry
    •  
      CommentAuthorbermi
     

    What is the difference between $this->foo and $this->Foo?

    No difference at all they both point to the same object.

    if you want to call a parent method, you can do it like

    parent::init();
    

    How does init() get called when a new instance of the model is created by the controller? Akelos tries to infer the instance variables from the database schema. But what if I want to add and initialize additional instance variables?

    So as a simple example, say I have a model class called Foo. It has a private instance variable called $timeOfBirth that gets set by a call to the function time() when the models gets instantiated by the controller. It also has a public instance variable $timeAlive that gets set by a call to a public function timeSinceBirth that simply subtracts the $timeOfBirth from a new call to time(). I would like it so that every time I made a call to the template show.tpl, it would simply display something like <h1>I've been alive for {Foo.timeAlive}</h1>. How would I do this in Akelos?

    Use the default created_at attribute which is set automatically for you when storing an Active Record object.

    This would be the simple way.

    On your view:

    <h1>I've been alive since <%= distance_of_time_in_words_to_now Foo.created_at %></h1>
    

    A more complicated way that shows the basis of constructor overloading as you asked for would be

    on your model:

    private $_time_alive;
    
    function __construct()
    {
        $attributes = (array)func_get_args();
        parent::init($attributes);
        $this->time_alive = $this->timeSinceBirth();
    }
    private timeSinceBirth()
    {
        return $this->isNewRecord() ? 0 : time() - Ak::getTimestamp($this->get('created_at'));
    }
    

    on your view:

    <h1>I've been alive for {Foo.time_alive}</h1>
    

    Hope you find it useful.

    • CommentAuthorbhembree
     
    Thanks Bermi, that helps alot. What are you doing up so late? Just one more question. How is object persistence handled in Akelos? Does this object get created and destroyed with every call to the controller or is there some way to keep it persistent without using the database?
    •  
      CommentAuthorbermi
     

    Barry,

    Normally PHP uses share-nothing approach, and that is what makes PHP application deployment and scaling so easy. The simplest way to persist models is using the database. You might persist objects and rendered views in memory using memcached or use an application server like mongrel in PHP.

    Using appserver server is the path I'm working on to allow persistance, but it stills on its infancy.