I've a Paris
based model with its relative 'posts' table:
class Post extends Model {
public static $_table = 'posts';
public static $_id_column = 'id';
}
When I make a simple query to grab all posts:
$posts = Model::factory('Post')->find_many();
var_dump($posts);
this is what I get in response:
array(13) {
[0]=>
object(Post)#34 (1) {
["orm"]=>
object(ORM)#21 (19) {
["_table_name":protected]=>
string(5) "posts"
["_table_alias":protected]=>
NULL
["_values":protected]=>
array(0) {
}
["_result_columns":protected]=>
array(1) {
[0]=>
string(1) "*"
}
["_using_default_result_columns":protected]=>
bool(true)
["_join_sources":protected]=>
array(0) {
}
["_distinct":protected]=>
bool(false)
["_is_raw_query":protected]=>
bool(false)
["_raw_query":protected]=>
string(0) ""
["_raw_parameters":protected]=>
array(0) {
}
["_where_conditions":protected]=>
array(0) {
}
["_limit":protected]=>
NULL
["_offset":protected]=>
NULL
["_order_by":protected]=>
array(0) {
}
["_group_by":protected]=>
array(0) {
}
["_data":protected]=>
array(4) {
["id"]=>
string(1) "1"
["title"]=>
string(10) "Primo post"
["content"]=>
string(11) "prova prova"
["published"]=>
string(1) "0"
}
["_dirty_fields":protected]=>
array(0) {
}
["_is_new":protected]=>
bool(false)
["_instance_id_column":protected]=>
string(2) "id"
}
}
etc...
Now the question is: How do I access the datas? Because I cannot access that. Am I doing something wrong?
You can do this in the following way:
foreach($posts as $post) {
echo $post->title;
}
As you can see I am first looping over the records and then printing the title of each one. Everthing in the protected data property of your result can be accessed in this way:
array(4) {
["id"]=>
string(1) "1"
["title"]=>
string(10) "Primo post"
["content"]=>
string(11) "prova prova"
["published"]=>
string(1) "0"
}
So you could get the content by calling:
echo $post->content;
As an aside the guy who wrote Paris and Idiorm, Jamie, used to work with me so let me know if you have any other questions.
You can encode it to JSON in the following way:
foreach($posts as $post) {
echo json_encode($post->as_array());
}