Seth
Seth

Reputation: 6260

CodeIgniter and DataMapper

I've read through a few different posts here and I can't figure out what I'm doing wrong.

My DB is setup like the following:

homes
- id
- address_id
- price
- etc...

address
- id
- home_id
- address1
- address2
- etc...

Then my models look like this, condensed.

home.php
<?php
class Home extends DataMapper {
    public var $has_one = array('address');
}

address.php
<?php
class Address extends DataMapper {
    public var $has_one = array('home');
}

Then my controller uses the following:

homes.php
class Homes extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('datamapper');
    }

    public function index() {
        $homes = new Homes();
        $homes->include_related('address');
        $homes->get_iterated();

        $this->output->enable_profiler(TRUE);

        _p($homes); // Self made function that wraps a print_r() in two <pre> tags.
}
}

If I comment out these two lines I get the standard CI return array.

$homes->include_related('address');
$homes->get_iterated();

If I don't then I get a server error. This is my first time using DataMapper and I'm almost certain I'm doing everything wrong, but have no idea where to start.

UPDATE:

I figured out my issue. I had to change the DB table address to addresses and in my address.php model I had to specify var $table = 'addresses';

That fixed everything.

Upvotes: 0

Views: 1581

Answers (2)

Spir
Spir

Reputation: 1719

Your relations is wrong. I assume you are setting a One to One relation ship. The doc say : http://datamapper.wanwizard.eu/pages/relationtypes.html

Because this is a One to One relationship, the relationship could have been stored in three ways:

  1. As shown, on the workers table.
  2. On the workplaces table, as worker_id
  3. On a dedicated workers_workplaces join table, with the columns id, worker_id, and workplace_id

But here you have added *address_id* to home and *home_id* in address. You have to choose between on. For example keep *home_id* in address and remove *address_id* in home.

Upvotes: 0

Spir
Spir

Reputation: 1719

Yes you can specify the table name in your model. Also your example was wrong :

$homes = new Homes();

Should be

$homes = new Home();

I usually redefine the table name within my model to make sure everything is fine.

Upvotes: 1

Related Questions