简体   繁体   中英

CodeIgniter DataMapper ORM unable to instantiate model object

I am creating a model object like this: $object1 = new Object1(); There is a table in the database named object1s. The Object1 class is an empty class that extends DataMapper. That works without problems. I am able to save data, and retrieve data from the database (I can see it using a separate database client).

I tried to same thing with another object (and a different table), for example, $object2 = new Object2(); This time I get the following error message:

Fatal error: Call to a member function line() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/c/application/libraries/Datamapper.php on line 6049

Line 6049 looks like this:

return $this->lang->line($key);

When I instantiated Object1, $this->lang was set to an instance of DM_Lang at this point, but when instantiating the second object $this->lang is set to "en". The only difference between the two are the name and database table.

What can I do to fix this? Why are they different? Where is $this->lang being set (I've already stepped through with a debugger and can't find it)?

The database is MySQL. I'm running this for development purposes on MacOSX Lion. The web server is apache. I'm using DataMapper ORM v1.8.2.


There was a comment requesting the exact code in question. This is the complete exact code I tested with (I copied and pasted from each file).

Object1 is called Movie, models/movie.php:

<?php

class Movie extends DataMapper {

}

Object 2 is called Image, models/image.php:

<?php

class Image extends DataMapper {

}

The controller looks like this, controllers/test.php:

<?php

class Test extends CI_Controller {

    public function newdata()
    {
        // The following line runs as expected
        $movie = new Movie();
        // The following line fails with the Fatal error mentioned above
        $image = new Image();

    }

}

I followed the installation instructions that came with DataMapper v1.8.2. My index.php file includes the following before loading the CodeIgniter bootstrap file.

/* --------------------------------------------------------------------
 * LOAD THE DATAMAPPER BOOTSTRAP FILE
 * --------------------------------------------------------------------
 */
        require_once APPPATH.'third_party/datamapper/bootstrap.php';

It works quite smoothly with the Movie object. I am able to write data to it and verify it is there using a separate SQL query tool.

I just noticed that the "$image = ..." line runs with no errors if the "$movie = ..." line is commented out. I will, however, need both of them for my app to function properly.

I am using CI_VERSION=2.1.0.

Thank you for your help.

The database schema had been used prior to switching to CodeIgniter. The first table had a name conflict (it used "lang" for a field name). The second table had no conflicts, and was failing only because it was the next DataMapper subclass instantiation coming after the model object with the problem. My attention was on the table/model that was failing, when in reality it was the one that succeeded that had the problem.

The fix was to check ALL model objects for conflicts, not just the one that is failing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM