Welcome Guest, Not a member yet? Register   Sign In
Datamapper relationship will read, but not save
#1

[eluser]Dracos[/eluser]
This is a rather complex relationship that got coded up a long time ago, but only now is coming into use.

The system is using CI 2.1 and DM 1.8.2. Here are the model snippets:

Code:
class contact extends DataMapperExt {

var $has_one = array(
  'image' => array(
   'class' => 'file',
   'other_field' => 'image_contact',
   'join_self_as' => 'contact',
   'join_other_as' => 'file',
   'join_table' => 'contacts_images',
  )
);
}

class File extends DataMapperExt {
var $has_many = array(
  'image_contact' => array(
   'class' => 'contact',
   'other_field' => 'image',
   'join_self_as' => 'file',
   'join_other_as' => 'contact',
   'join_table' => 'contacts_images',
  )
);
}

So far, the file model is only being used for images; contact is only one model (of about 8) to have such a relationship with file, and the first to have it implemented. The tables involved are "files" (of course), "contacts" (of course), and for this relationship "contacts_images". All the other similar $has_many relationships in file use a "[foo]s_images" table.

Using this:

Code:
$c = new contact($some_id);
$c->image->get();

I can get the image (nee file) correctly, if contact::has_one['image']['other_field'] = 'image_contact'. I know this works because I manually created the row in contacts_images. I know 'other_field' breaks it because I commented it out; commenting out contact::has_one['image']['join_other_as'] made no difference.

However, this:

Code:
$c = new contact($some_id);
$img = new file($image_id);
$c->save_image($img);

Results in nothing ever happening in the contacts_images table... no insert, no update. Contact has a custom save(), in which I've placed some logging... I see it being called with $related = "image".

Where did this go wrong? I remember there was a reason why class "File" isn't named "file", but I don't remember what that was. Invoking "new file()" seems to work just fine.




Theme © iAndrew 2016 - Forum software by © MyBB