Welcome Guest, Not a member yet? Register   Sign In
[SOLVED!] Problem with Array vs Object
#1

[eluser]Screaming Eagle[/eluser]
Hello Everyone!

First and foremost I want to compliment the community here. I've learned an incredible amount from reading these forums over the last few years.

I've searched for 2 days now trying to find an answer to a problem I've having and now luck. I'm hoping I can get some guidance.

I am building a large site and trying to add some caching using the Zend_Cache module. I have the cache being created OK and I can check for it and retrieve it. The problem is that when I get the data returned from the cache it is an array and not an object in CodeIgniters eyes. Is there a way I can change it back? I'd much rather use an object.

Here is some quick code I'm using to figure this out.

The model:
Code:
/**
     * Retrieves a list of stories from a particular category from the database.
     *
     * @param intger $cat_num - Number of category to get stories for
     * @param integer $num_stories - Number of stories to retrieve, to retrieve all stories pass a zero.
     * @return array
     */
    function getStoryList($cat_num, $num_stories)
    {
        $this->load->library('zend');
        $this->zend->load('Zend/Cache');
        
        $frontendOptions = array(
               'lifetime' => 10,
              'automatic_serialization' => true
                );

        $backendOptions = array(
                'cache_dir' => '/home/hnedatan/public_html/z_cache/'
        );

        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
        $cache_id = "StoryList_".$cat_num."_".$num_stories;
        
        if(!($story_list = $cache->load($cache_id)))
        {
        $sql = "SELECT t1.*, t2.* FROM stories as t1 INNER JOIN story_cats as t2 ON t1.ManagementID = t2.ManagementID WHERE category=".$cat_num." ORDER BY priority DESC, updated DESC";
        If ($num_stories > 0){$sql .= " LIMIT ".$num_stories;}
        $story_list = $this->db->query($sql);
        $cache->save($story_list,$cache_id);
        }
        return $story_list;
    }

The controller:
Code:
function test()
    {
        $this->load->model('Db_helper');
        $test_result = $this->Db_helper->getStoryList(3000,5);
        echo $test_result->num_rows();
    }

This is a VERY simple example of course. When the data is not in the cache, I get the expected return (in this case a '5' because that's how many records were returned). However, when I pull the data from the cache I get nothing at all. No error, no number, nothing.

I suspect this has to do with the data type being different if I pull the data from the cache but I can't seem to prove that. I've done a print_r on the query results and I am getting the same info either way.

Can anybody PLEASE help?

Thanks!
#2

[eluser]danmontgomery[/eluser]
Code:
$object = (object)$array;

http://php.net/manual/en/language.types....ggling.php
#3

[eluser]Screaming Eagle[/eluser]
[quote author="noctrum" date="1265241074"]
Code:
$object = (object)$array;

http://php.net/manual/en/language.types....ggling.php[/quote]

Thank you for the advice. I believe this has gotten me closer. I am now seeing an object. Any idea why CI isn't able to properly count the rows on the cached item with the $variable->num_rows() command?

Thanks!
#4

[eluser]danmontgomery[/eluser]
Because num_rows() is just an alias for mysql_num_rows() (or whatever equivalent function for the database you're using) , which is dependent on an sql query being run.

I'm not familiar with the zend_cache module, but if you're getting an array back you can just get the value of count($array); before you convert it to an object.
#5

[eluser]Screaming Eagle[/eluser]
Just to clarify:

The previous advice helped some. I now get an object of the same type whether pulling from the database live or from cache. They do look slightly different though and the num_rows() command doesn't seem to work on an entry pulled from the cache.

Here's a print_r of an object pulled from the db live:

CI_DB_mysql_result Object ( [conn_id] => Resource id #32 [result_id] => Resource id #52 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 5 [row_data] => )


Here's a print_r of an object pulled from cache:

CI_DB_mysql_result Object ( [conn_id] => 0 [result_id] => 0 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 5 [row_data] => )

Any thoughts? They both appear to be an object of type "mysql_result" so I think the functions should work the same. Thanks so much for your help so far!
#6

[eluser]danmontgomery[/eluser]
Or just access num_rows directly...
#7

[eluser]Screaming Eagle[/eluser]
[quote author="noctrum" date="1265243512"]Or just access num_rows directly...[/quote]

Sorry if this is a dumb question, but how? The object is returning to my controller correctly. Once inside the controller how do I access the 'num_rows' directly? Thanks again!
#8

[eluser]danmontgomery[/eluser]
Code:
$object->result_object['num_rows']

Or if you're converting result_object to an object

Code:
$object->result_object->num_rows
#9

[eluser]Screaming Eagle[/eluser]
[quote author="noctrum" date="1265243882"]
Code:
$object->result_object['num_rows']

Or if you're converting result_object to an object

Code:
$object->result_object->num_rows
[/quote]

Hey! I think you got me there! I had to make a slight change though:

$test_result->num_rows;

That seems to have done the trick! Thanks so much for your help!




Theme © iAndrew 2016 - Forum software by © MyBB