Welcome Guest, Not a member yet? Register   Sign In
OOP in Codeigniter with Active Records
#1

[eluser]scherman[/eluser]
Hi, first of all, i don't speak english very well, so, i hope that you can understand me.
I am developing a project in Codeigniter, and i love the OOP, so i am trying to merge the OOP with Codeigniter. And i want to use the Active Record's methods.

So, i have this code:

Doctor_model.php
Code:
class Doctor_model extends CI_Model
{
    public $id;
    public $firstname;
    public $lastname;

    public function __construct()
    {
        $this->load->database();
    }

    public function save()
    {
        $datos = array(
                'firstname' => $this->firstname,
                'lastname' => $this->lastname
            );
        
        return $this->db->insert('doctors', $datos);
    }
}

And in my controller i have this code:
Code:
class Doctors extends CI_Controller
{
     function index()
     {
        $doctor = $this->load->model('doctor_model');
        $doctor->firstname = $this->input->post('firstname');
        $doctor->lastname = $this->input->post('lastname');
        echo 'saving ' . $doctor->firstname;

        if($doctor->save())
        {
            echo 'saved';
        }
        else
        {
            echo 'fail';
        }
     }
}

but when i run my controller's index method, it returns me this error:
Quote:Fatal error: Call to undefined method stdClass:Confusedave() in [here the file and line]

Do somebody know what is my error? Do somebody know a better way for work in codeigniter with the OOP and use in the classes the Active Record's methods?

Thank you so much, and i hope that you can help me.

I accept criticism and advice from you!

Clarification: I've traduced the fields and the names of my code, because it was in spanish, so, maybe it has an error of the field's name.
#2

[eluser]toopay[/eluser]
In CodeIgniter, OOP is slightly different, since CI was already added alot helper/method. Here you can do that in CI
Code:
/* [NOT TESTED] */

// In your model
    public function save()
    {
        $datos = array(
                'firstname' => $this->firstname,
                'lastname' => $this->lastname
            );
        $this->db->insert('doctors', $datos);
        return $this;
    }
// In Controller
    function index()
    {
        $this->load->model('doctor_model');
        $this->doctor_model->firstname = $this->input->post('firstname');
        $this->doctor_model->lastname = $this->input->post('lastname');
        echo 'saving ' . $this->doctor_model->firstname;

        if($this->doctor_model->save())
        {
            echo $this->doctor_model->firstname . ' successfully saved';
        }
     }
#3

[eluser]scherman[/eluser]
[quote author="toopay" date="1313123260"]In CodeIgniter, OOP is slightly different, since CI was already added alot helper/method. Here you can do that in CI
Code:
/* [NOT TESTED] */

// In your model
    public function save()
    {
        $datos = array(
                'firstname' => $this->firstname,
                'lastname' => $this->lastname
            );
        $this->db->insert('doctors', $datos);
        return $this;
    }
// In Controller
    function index()
    {
        $this->load->model('doctor_model');
        $this->doctor_model->firstname = $this->input->post('firstname');
        $this->doctor_model->lastname = $this->input->post('lastname');
        echo 'saving ' . $this->doctor_model->firstname;

        if($this->doctor_model->save())
        {
            echo $this->doctor_model->firstname . ' successfully saved';
        }
     }
[/quote]

Thank you toopay, but on this way, i can't work with more than one object, i can't create more instances, do i?
When i do this:
Code:
echo $doctor->firstname;
it works, the problem is when i call the method "save".

I want when i call the method "save", it insert the values of it's propertys that it have in it's instance.

Something like this:
Code:
$doctor1 = $this->load->model('doctor_model'); /*or something like $doctor1 = new doctor();*/
$doctor2 = $this->load->model('doctor_model');
$doctor1->firstname = 'firstname1';
$doctor2->firstname = 'firstname2';
$doctor1->lastname = 'lastname1';
$doctor2->lastname = 'lastname2';
$doctor1->save();
$doctor2->save();

i've used two objects for explain that the save method insert the values of each one.

do somebody know can i do that?
#4

[eluser]toopay[/eluser]
If you would like your model assigned to a different object name you can specify it via the second parameter of the loading function (it can be whatsoever you want to named it):
Code:
$this->load->model('doctor_model', 'doctor1');
$this->doctor1->firstname = $this->input->post('firstname');
$this->doctor1->lastname = $this->input->post('lastname');
echo 'saving ' . $this->doctor1->firstname;
$this->doctor1->save();
// And so on
#5

[eluser]scherman[/eluser]
[quote author="toopay" date="1313125537"]If you would like your model assigned to a different object name you can specify it via the second parameter of the loading function (it can be whatsoever you want to named it):
Code:
$this->load->model('doctor_model', 'doctor1');
$this->doctor1->function();
$this->doctor1->firstname = $this->input->post('firstname');
$this->doctor1->lastname = $this->input->post('lastname');
echo 'saving ' . $this->doctor1->firstname;
$this->doctor1->save();
// And so on
[/quote]

Now it's better! jaja, i like it. Thank you again.
I have one more question.

I don't understand for what is this line:
Code:
$this->doctor1->function();

is it bad this?:
Code:
$this->load->model('doctor_model', 'doctor1');
$this->doctor1->firstname = $this->input->post('firstname');
$this->doctor1->lastname = $this->input->post('lastname');
echo 'saving ' . $this->doctor1->firstname;
$this->doctor1->save();
// And so on




Theme © iAndrew 2016 - Forum software by © MyBB