Welcome Guest, Not a member yet? Register   Sign In
Datamapper ORM in-table foreign key not working
#1

[eluser]DaMaul[/eluser]
For some reason Datamapper is not recognising my in-table foreign key relationship and is attempting to use a joining table instead when I try and access my related object.

It's probably the simplest example of such a relationship you could get. Here's what my code looks like:

Code:
class Venue extends DataMapper
{
    public $has_one = ['town'];
}

class Town extends DataMapper
{
    public $has_many =['venue'];
}

// Controller
class Pubs extends CI_Controller
{
    public function single($id) {
        $v = new Venue($id);
        $v->town->get();
        echo $v->town->name;
    }
}

Here's the mysql CREATE code for my venues table:
Code:
CREATE TABLE `venues` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL DEFAULT NULL,
`town_id` INT(10) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `town_venue` (`town_id`),
CONSTRAINT `FK1_towns` FOREIGN KEY (`town_id`) REFERENCES `towns` (`id`)
)

And here for the towns table:
Code:
CREATE TABLE `towns` (
`id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`country` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)


When I run the method in the controller, I get a codeigniter DB error stating "Table 'mydatabase.towns_venues' doesn't exist".

Obviously datamapper hasn't realised that it can use an in-table foreign key and is looking for a joining table. Can anyone suggest why this is happening? I'm pulling my hair out trying to figure out what's wrong with my setup
#2

[eluser]DaMaul[/eluser]
After a load of messinng around, I tried stepping through the code, and it seems like my problem is happening because the code is not correctly initialising the list of fields within the model from the database.

The problem seems to be in CodeIgniter's "field_data()" function, which is returning an empty array instead of returning an array of the fields....
#3

[eluser]DaMaul[/eluser]
Ok after a couple of hours of head scratching I think I've solved the problem. I'll post my findings here in case anyone else encounters the same.

It seems that the latest version of datamapper relies on the codeigniter's CI_DB_mysql_result::field_data() method, which seems to be totally broken in version 2.2.0 (the latest official release, but it's fixed in the newest dev version).

So as far as I can see, the latest version of datamapper doesn't work properly with the latest official release of codeigniter!




Theme © iAndrew 2016 - Forum software by © MyBB