Welcome Guest, Not a member yet? Register   Sign In
Error when executing a method in a model.
#1

[eluser]Avril[/eluser]
Hello,

I've made a model that will insert some data in my DB, model is called by using a controller.

I get the standard "cannot redeclare class" error message when executing the model.
Quote:Fatal error: Cannot redeclare class Expenses in /Users/avrilverhaeghen/Sites/myBudget/application/models/expenses.php on line 44

This is my model:
Code:
<?php

class Expenses extends Model {

    function new_expense($data){

        $query = $this->db->get_where('bank_account_link', array($data['account_id']));

        foreach($query->result() as $row){

            $data['bank_account_number'] = $row['bank_account_number'];

        }

        if(isset($data['bank_account_number'])){

            $insert = $this->db->insert('expenses', $data);

            if($insert == TRUE){

                return TRUE;

            }else{

                return FALSE;

            }

        }else{

            return FALSE;

        }

    }

}

And the method in the controller:
Code:
function new_expense(){

        $data = array(

            'account_id' => $this->authenticator->get_uid(),
            'type' => $this->input->post('expense_type'),
            'amount' => $this->input->post('expense_amount')

        );

        $this->load->model('expenses');
        $insert_new_expense = $this->expenses->new_expense($data);

        if($insert_new_expense == TRUE){

            echo "Insert OK";

        }else{

            echo "Insert NOT OK, please contact the Administrator";

        }

    }

I used the echo to check if it's working, they will be changed after it's working.

I don't where I went wrong when calling the model, I used the same technique in the same project and there it's working.

Thanks!
#2

[eluser]danmontgomery[/eluser]
So what's on line 44 of the model file?
#3

[eluser]Avril[/eluser]
Well, it's very last }.
I know it's strange ..
#4

[eluser]Avril[/eluser]
Never mind, I've found, it was because my controller AND model file names where the same ... d'OH!
#5

[eluser]InsiteFX[/eluser]
Your Controller function has the same name as the function you are calling in your model!
Try changing the name to a different one.

Code:
// new_expense here!
function new_expense(){

        $data = array(

            'account_id' => $this->authenticator->get_uid(),
            'type' => $this->input->post('expense_type'),
            'amount' => $this->input->post('expense_amount')

        );

        $this->load->model('expenses');
        // new_expense here!
        $insert_new_expense = $this->expenses->new_expense($data);

        if($insert_new_expense == TRUE){

            echo "Insert OK";

        }else{

            echo "Insert NOT OK, please contact the Administrator";

        }

    }

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB