Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorpweil
     
    I'm still quite new to akelos, and it's been fairly slow going. I could use some help in adjusting my approach to programming a web app.

    Viewing and testing MySQL results: in Akelos how do I know what the name of the array is when I run a find()? Normally I deal with query results as elements or values in an array, e.g, $results[0], $results[1][2], etc. And I do a lot of result testing by running print_r($array).

    Displaying results: if I need to treat certain array elements in a particular way for display, how do I refer to them? Do I need to refer to them as objects, and if so, what would this look like?

    I'm sure that I will have more questions like these, but obtaining answers to these will help me a great deal. thank you.
    • CommentAuthorKaste
     

    a find() usually returns an array or a list of ActiveRecords which are objects. only _find('first'...) retuns a single object.

    so basically you iterate of the returned array in a foreach and access your 'column-values' like you would access any object-property.

    $People = $this->find('all');
    foreach ($People as $Person){
        echo $Person->name;
    }
    

    what otherwise is a 'row' results in an instantiated ActiveRecord, one could say.

    • CommentAuthorKaste
     

    a find() usually returns an array or a list of ActiveRecords which are objects. only _find('first'...) retuns a single object.

    so basically you iterate of the returned array in a foreach and access your 'column-values' like you would access any object-property.

    $People = $this->find('all');
    foreach ($People as $Person){
        echo $Person->name;
    }
    

    what otherwise is a 'row' results in an instantiated ActiveRecord, one could say.

    • CommentAuthorKaste
     

    damn, our forum is broken.

    • CommentAuthorpweil
     
    Thanks Kaste. I think I'm slowly getting it. But being as stubborn as I am, I'm still clamoring for a way to do the equivalent of print_r so I can quickly display/inspect an object in a readable way, without having to do foreach loops for multiple columns. I'm thinking of something like the debug method in Rails (which I thought was sort of a poor mans' substitute for print_r). Is there such a thing? (I think the forum is basically working, but the email functions are not)
    • CommentAuthorKaste
     

    you could

    echo $Person;
    print_r((string)$Person);
    
    • CommentAuthorpweil
     
    Hmm. This seems to give me just the column names; I was looking for the values within the objects (I hope I'm saying this the correct way). Here is my find method

    $this->resources = $this->Resource->find('all', array('include'=>'host, contact, category, location'));

    This generates a long query:

    SELECT __owner.id AS __owner_id, __owner.title AS __owner_title, __owner.description AS __owner_description, ETC. ETC ..

    Before working with Akelos, I would do something like this (using PEAR MDB2):

    $sql = [query as above]
    $results = $mdb2->queryAll($sql);

    Then I could run print_r($results) and see everything in the array -- column names and values -- that is returned in a readable way. It's a very valuable way to debug queries. I'm sure we've all done this many times.

    How might one do this in Akelos?
    • CommentAuthorKaste
     

    Hm.

    An action like this, if you have built the booklink-tutorial

    class BookController...
    function dumpy()
    {
        $Book = $this->Book->find('first');
        $this->renderText((string)$Book);
    }
    

    now

    http://localhost/booklink/book/dumpy

    prints a nicely formatted info.

    Details for Book with id 1
    Id
        1
    Title
        first book
    Description
        my first book 
    Author
        1
    Published on
        2007-11-15
    
    • CommentAuthorpweil
     
    That's much better Kaste. I don't understand what renderText does, but thank you. Now.... if we can do this sort of dump for _all_ records returned, and not just the first record, one after another, that would be great...Maybe you need a loop, I don't know. Sorry to be so demanding -- I really appreciate this.
    • CommentAuthorKaste
     

    renderText just renders the given string/text.

    BookController...
    function dump_more()
    {
        $Books = $this->Book->find('all');
        $this->dump($Books);
    }
    
    ApplicationController...
    function dump($objects)
    {
        $this->renderText(join('',$objects));
    }
    

    http://localhost/booklink/book/dump_more

    you can actually change the ->toString() method to alter the output if you really like to. usually you would do this on your shared_model.

    • CommentAuthorpweil
     
    Brilliant! thank you, Kaste!