Welcome Guest, Not a member yet? Register   Sign In
"Cannot redeclare class {...}" for user Model class in CodeIgniter 2.0.0-Dev (tip in hg)
#1

[eluser]Frank V[/eluser]
I'm attempting to follow a tutorial to learn how to use CodeIgniter. (http://www.devshed.com/c/a/PHP/Building-...Framework/) For my reasons, I've decided to learn from 2.0.0-Dev (which I realize the tutorials are written for ~1.7.x). (Note: I'm using Changeset: 476bf5866ee4)

Nevertheless, I figure I'll learn the product better if I use the newest and have to figure out the differences between 1.7.x and 2.0.0-Dev.

Ok, that said -- I've run in to a problem that I do not understand. Quick info about me, I'm not an extremely experienced PHP coder. I'm coming from Django and Python. That said, I do know most of the basics about PHP (I think).

I'm utilizing the latest version of WAMP (PHP 5.3, MySQL 5.1) & I'm developing on Windows 7. (I added xdebug to php)

The exact error I'm getting is as follows:


Code:
(!) Fatal error: Cannot redeclare class tOSU_Users in C:\wamp\www\codeigniter\application\models\tosu_users.php on line 32
Call Stack
#    Time    Memory    Function    Location
1    0.0004    380320    {main}( )    ..\index.php:0
2    0.0012    439216    require_once( 'C:\wamp\www\codeigniter\system\core\CodeIgniter.php' )    ..\index.php:163
3    0.0109    1121592    tOSU_Users->tOSU_Users( )    ..\CodeIgniter.php:281
4    0.0154    1366104    CI_Loader->model( )    ..\tosu_users.php:9


The crux of the error is: "Cannot re-declare class tOSU_Users in C:\wamp\www\codeigniter\application\models\tosu_users.php on line 32". (line 32 is the last line and is blank) The tutorial had me naming the model class as 'users' and I changed it to tosu_users to ensure there isn't a new class (in 2.0.0-dev that is called users.)

Here is my full source code for the model (tosu_users.php):

Code:
<?php
    class tOSU_Users extends CI_Model {
        function tOSU_Users() {
            //call constructor
            parent::CI_Model();
            
            //load the DB class and connect.
            $this->load->database();
        }
        
        function getAllUsers() {
            $query = $this->db->get('Users');
            if( $query->num_rows() > 0 ) {
                //return result set as associative array
                return $query->result_array();
            }
            return null;
        }
        
        function getUsersWhere( $field, $param ) {
            $this->db->where($field, $param);
            $query = $this->db->get('Users');
            
            //return result set as an associtive array
            return $query->result_array();
        }
        
        // get total number of users
        function getNumUsers() {
            return $this->db->count_all('Users');
        }
    }
?>

Controller (tosu_users.php):

Code:
<?php
    
    class tOSU_Users extends Controller {
        function tOSU_Users() {
            // call parent...
            parent::Controller();
            
            //load the 'Users' model.
            $this->load->model('tOSU_Users');
        }
        
        function index() {
            //Store data for being displayed on view file
            $data['users'] = $this->Users->getUsers();
            $data['numusers'] = $this->Users->getNumUsers();
            $data['title'] = "Displaying user data";
            $data['header'] = "User List";
            
            // load the view
            $this->load->view('users_view', $data);
        }
    }
    
?>

View (users_view.php):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    <h1>&lt;?php echo $header; ?&gt;</h1>
    <ul>
        &lt;?php foreach($users as $user): ?&gt;
        <li>
            <p>
                &lt;?php echo 'Full name: ' . $user['firstname'].' '.$user['lastname'] . ' Email: '.$user['email'];?&gt;</p></li>
        &lt;?php endforeach; ?&gt;
    </ul>
    
    <p>&lt;?php echo 'Total number of users: $numusers'; ?&gt;</p>
&lt;/body&gt;
&lt;/html&gt;

Any help or thoughts would be appreciated, thank you.
#2

[eluser]mi6crazyheart[/eluser]
Make a small change in u'r controller file. Try it & tell me...
Code:
&lt;?php
    
    class tOSU_Users extends Controller {
        function tOSU_Users() {
            // call parent...
            parent::Controller();
            
            //load the 'Users' model.
            $this->load->model('tOSU_Users');
        }
        
        function index() {
            //Store data for being displayed on view file
            $data['users'] = $this->tOSU_Users->getUsers(); // doubt at this place as no model function by "getUsers" present in side of u'r model. So change it accordingly
            $data['numusers'] = $this->tOSU_Users->getNumUsers();
            $data['title'] = "Displaying user data";
            $data['header'] = "User List";
            
            // load the view
            $this->load->view('users_view', $data);
        }
    }
    
?&gt;
#3

[eluser]Frank V[/eluser]
Thank you for your response.

I made the adjustment as you suggested but I'm still getting the same error. Here is the revised controller...

Code:
&lt;?php
    
    class tOSU_Users extends Controller {
        function tOSU_Users() {
            // call parent...
            parent::Controller();
            
            //load the 'Users' model.
            $this->load->model('tOSU_Users');
        }
        
        function index() {
            //Store data for being displayed on view file
            $data['users'] = $this->Users->getAllUsers(); //Updated
            $data['numusers'] = $this->Users->getNumUsers();
            $data['title'] = "Displaying user data";
            $data['header'] = "User List";
            
            // load the view
            $this->load->view('users_view', $data);
        }
    }
    
?&gt;
#4

[eluser]mi6crazyheart[/eluser]
Hey, u might've forget to mark one more thing in my suggested controller & that is...
When u r accessing any model function u'r doing this
Code:
$this->Users->getAllUsers();
But, as u'r model file name is "tOSU_Users.php", the above code piece will be like this:
Code:
$this->tOSU_Users->getAllUsers();

So, change this thing also inside of u'r controller & execute u'r script...
#5

[eluser]Frank V[/eluser]
Good call -- I missed that. I made the change but it didn't change anything with regards to my error. Nevertheless, thanks for pointing that out.
#6

[eluser]kaejiavo[/eluser]
Hi,
your model class and controller class have the same name. i think this is not working. try renaming one of them.

Marco
#7

[eluser]Frank V[/eluser]
Mawi27, thanks. That did it.

All the tutorials seemed to use the same name for the model and controller. I had been working under the assumption that that was a requirement of the framework.

Anyway, thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB