This is a question more concerning my ignorance of PHP5.3 features, than specific of FuelPHP. I hope you will bear with me on this one.
I can see the crud model has a find() method that does a database lookup of a matching record. The fetched data is converted to an array, and then returned to the caller. Somehow the caller gets a crud object back and not the array of data from the database.
How does that work? I am guessing a magic function does the hard work, but I just can't see how.
Mmmmm ... is not a pure array but, for my fast search, I think that returned by the as_array() method in /core/classes/database/result.php
Check line 104
Perhaps I should explain better what is confusing me.
At the end of the crud model find() method, is this return statement:
return static::post_find($result);
$result is an array at this point, post_find just returns $result, so I would expect find() to return an array. Instead it returns an object of the type that the class using the crud model is (e.g. returns object ModelUser if called up using ModelUser::find(...)
This return statement is what I would write to do that:
return new static($result);
In a standalone library, my version works and returns the new object, while the FulePHP version above returns an array, just as I would expect. However, when run in the FuelPHP framework, both of the above examples return an object. I just don't understand how.
I'm sure I am missing something totally obvious. I'm including the __constructor() in my standalone library, but I guess I'm missing something out...?
-- JJ
Okay - I think I have got it.
Firstly I was mistaking "as_array" meaning "a row of retrieved data as an associative array of name/value pairs". It actually means "an array of query results, with each element being a single query row".
Secondly I could see the database object spitting this result out, but could not work out how that object type is the relevant model object and not a generic database result object. Tracing through the code I realise that the database query object is told what kind of object its results are to be put into. It is the database results object that handles all that, many many layers deep in its inner workings.
So what I was missing, is that the database object was spitting out model objects, and I did not expect that.
It's all very clever, and beautiful
-- JJ