[SOLVED!] Problem with Array vs Object |
[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: /** The controller: Code: function test() 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!
[eluser]danmontgomery[/eluser]
Code: $object = (object)$array; http://php.net/manual/en/language.types....ggling.php
[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!
[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.
[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!
[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!
[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
[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 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! |
Welcome Guest, Not a member yet? Register Sign In |