[eluser]Mirage[/eluser]
So I do have this right now:
Code:
class Plant extends DataMapper {
var $has_one = array();
var $has_many = array( 'image' );
Code:
class Image extends DataMapper {
var $has_one = array();
var $has_many = array();
I am assuming that I do not have to make a model for the plant_images relation.
Now in a synchronization loop for image resources I have the following:
Code:
foreach ($result as $key => $file)
{
$info=pathinfo($key);
if (!array_key_exists('extension',$info))
{
continue;
}
if (preg_match('/(_thumb|_small)$/', $info['filename']))
{
continue;
}
$image = new Image();
$image->where('path', $key)
->limit(1)
->get();
if (!$image->exists())
{
$image->name = $info['basename'];
$image->path = $key;
}
// We expect that the plant record exists
$plant = new plant();
$plant->where('bailey_id', substr($info['basename'],0,4))
->limit(1)
->get();
$image->trans_begin();
// The image must be saved, no matter what
log_message('debug', 'Saving Image');
$image->save();
if($image->trans_status===FALSE)
{
$image->trans_rollback();
log_message('error', 'Transaction failed. Image did not save. '. $image->error->string);
continue;
}
$image->trans_commit();
log_message('debug', 'Image saved.');
// If we do have a plant record, then we save it along with the image relation
if (!$plant->exists())
{
log_message('error', 'No related plant record. Skip saving relation');
continue;
}
// Save the relation
log_message('debug', 'Saving Plant and image relation');
$plant->save($image);
This will properly save/update the images. But there are two problems:
1. The plant model insists on the relation being called images_plants. Is there a way to customize this?
2. The call to $plant->save($image) will not create a relation to images_plants.