Welcome Guest, Not a member yet? Register   Sign In
[Q] specify model class for $query->results() or $query->row()
#1

[eluser]makoto kuwata[/eluser]
Hi all,
I have a question about model class.

Is it possible to specify model class for $query->result() or $query->row()?

Normally, these methods returns stdClass object:

Code:
$query = $this->db->getwhere('posts', array('id'=>123));
$obj = $query->row();
var_export($obj);   //=> $obj is an instance of stdClass

But I want to specify model class, like the following:

Code:
$query = $this->db->getwhere('posts', array('id'=>123));
$this->load->model('Post');
$obj = $query->row(Post);
var_export($obj);   //=> $obj is an instance of Post class

Is it possible? Or any good idea?

--
regards,
makoto kuwata
#2

[eluser]Yash[/eluser]
What is your aim?
#3

[eluser]makoto kuwata[/eluser]
[quote author="Yash" date="1217860492"]What is your aim?[/quote]

Just want to use my own class instead of stdClass
because I defined some convenient methods in my class.
Is it so ridiculous?
I think it is very popular in other ORMapper such as Hibernate or ActiveRecord
to specify class name of model object.

I read source code of CI and found that it is difficult to specify class name of model object.
#4

[eluser]Hannes Nevalainen[/eluser]
The AactiveRecord database layer in CI isn't an ORMapper. You will have to build your own on top of AR if want an ORMapper. Or take a look att IgnitedRecord which is a simple ORM-layer for CI.

Happy Coding.
#5

[eluser]xwero[/eluser]
all objects are based on the stdClass, it's the class that php uses. The framework doesn't oversees how you build your applications it doesn't automatically binds results to models because the result is returned by the model method it's in or used internally in the model method.

The ORM classes have internally a list of tables and fields and that is how they bind their objects.




Theme © iAndrew 2016 - Forum software by © MyBB