Welcome Guest, Not a member yet? Register   Sign In
Data Transfer Object in CodeIgniter
#1

[eluser]Agustín Villalba[/eluser]
Hi there! I post you to know if there is any way to use objects such as data transfer object in CodeIgniter. I have a model which retrieves a row from a table of DB and needs to create an object with that row and return it to the controller. Is there any way to implement dto pattern in CodeIgniter?? Thank you very much!
#2

[eluser]rogierb[/eluser]
It sounds you are describing basic CI functionality. Take a look at the user guide expecially the database part regarding result sets.
#3

[eluser]Agustín Villalba[/eluser]
Oh! Perhaps I didn't explain correctly my problem! I mean that I have a class (call it Resource) so I need to retrieve some row from DB with a model and each field of that row is passed to the __construct method of the Resource object (which have its own methods).
I give you some example code:

Resource class:

Code:
class Resource {

function __construct($param1,$param2,$param3)
{
//some code
}

function someMethod()
{
echo $this->$param1;
}

}

In my model
Code:
...
$result = $this->db->get();
$res = $result->result_array();
$myresource = new Resource($res[0],$res[1],$res[2]);
...
Is there any way to do this in CodeIgniter??
#4

[eluser]Phil Sturgeon[/eluser]
Yes, like this:

Code:
$result = $this->db->get();
$res = $result->result_array();
$myresource = new Resource($res[0],$res[1],$res[2]);
#5

[eluser]Agustín Villalba[/eluser]
Ok! Thank you Phil! So I was right! But... where do I have to create my Resource_Class.php file?? In model folder or where?? Thank you so much!!
#6

[eluser]Phil Sturgeon[/eluser]
My point was that if you want to do something that doesnt fit into a CodeIgniter library or model, do not be scared to use native PHP. You can include a PHP file and use $CI =& get_instance() and it will work fine.

Models cannot have constructors, and Libraries need constructors as an array.
#7

[eluser]Agustín Villalba[/eluser]
Ok! So I have 3 questions to you:
1. Why models cannot have contstructors?

2. I don't understand "Libraries need constructors as an array", what do you mean?

3. Where do I have to place my PHP file "Resource_Class.php"??

Thank you for your help, I'm a newbie in CodeIgniter!
#8

[eluser]Phil Sturgeon[/eluser]
1. That's how they were written.

2. Pass it an array... its in the User Guide.

3. Anywhere you damn well please.

Code:
include('customcode/Resource.php');




Theme © iAndrew 2016 - Forum software by © MyBB