Welcome Guest, Not a member yet? Register   Sign In
Infuriatingly Simple Datamapper Relationship problem
#1

[eluser]kungpoo[/eluser]
I've been tearing my hair out tonight, something that I know has worked for me in the past just doesn't seem to work anymore. To illustrate I have done a fresh install of CI and DMZ on both CI 1.7.2 and 2.0.

Inserted the following into a DB (as simple as possible) -

Code:
CREATE TABLE `artists` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `artists_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `artist_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Created the models-
Code:
<?php
class Artist extends DataMapper
{
    
    var $has_many = array('user');
    
    function __construct($id=NULL)
    {
        parent::__construct($id);
    }
}
Code:
<?php
class User extends DataMapper
{
    
    var $has_many = array('artist');
    
    function __construct($id=NULL)
    {
        parent::__construct($id);
    }
}

Then did the controller -

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        $this->output->enable_profiler(TRUE);
        //$this->load->model('Artist');
        //$this->load->model('User'); - neither seem to change things
    }
    
    function index()
    {
        $a = new Artist();
        $a->name = 'artist';
        
        $u = new User();
        $u->name = 'user';
        
        $u->save($a);
        //$a->save($u); - doesn't help to create it either
    }
}

But all I get is the following queries being run, the relationship is never created. If the objects are saved separately they are fine but obviously there is no relationship then.

Code:
SELECT * FROM `artists` LIMIT 1
SELECT * FROM `users` LIMIT 1
INSERT INTO `users` (`name`) VALUES ('user')

I've tried it with simple one to one and one to many relationships too but nothing seems to be working for me. Am I missing something glaringly obvious?

Thanks!
#2

[eluser]Jelmer[/eluser]
I don't really know DMZ, only played around with it a little bit and ended up using another ORM.

But in general I would expect that you'd have to tell the system it's not just a "has many" (one-to-many) but a "has and belongs to many" (many-to-many) relationship.
Also, I'd expect you have to save the first object before attaching the second. And maybe save the second object before establishing the relation between the two.
#3

[eluser]kungpoo[/eluser]
Thanks for trying to help Jelmer. As far as I can tell that is all the syntax that the models require.

Saving the models first individually and then retreiving them to create a join works but seems quite a roundabout way of doing it?

In the DMZ docs http://www.overzealous.com/dmz/pages/save.html there is a section that explains what I am trying to do but I cannot seem to replicate it although I am sure I've had it working in the past.

Code:
//Save a New object and its Relations in a single call

//It is important to note that you can save both an object's data and relationships with a single save call. For example, you could save a new object and its relationships all in one go like this:

// Create new User
$u = new User();

// Enter values into required fields
$u->username = "foo";
$u->password = "bar";
$u->email = "[email protected]";

// Get country object for Australia
$c = new Country();
$c->where('name', 'Australia')->get();

// Save new user and also save a relationship to the country
$u->save($c);

Thanks again
#4

[eluser]kungpoo[/eluser]
Looking at it I think it might be that I can't create 2 objects and save the relationship on-the-fly, which is more or less what you were getting at. Thankfully it works with one being created and then joined to the selected one. I suppose that must have been what I was doing when I had a similar setup working before Smile




Theme © iAndrew 2016 - Forum software by © MyBB