Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Many images


Reply


23 Posts   9551 Views

Avatar
Garrett

Community Member, 245 Posts

12 July 2008 at 9:34am

Hi,

I am also setting up a gallery, and have copied your code.

The ImageAttachement tab is showing up. However, in my popup for adding an image, I get the following error:

"FATAL ERROR: ComplexTableField::DetailForm() Cannot automatically determine 'has-one'-relationship to parent, please use setParentClass() to set it manually
At line 306 in C:\wwwroot\wamp\www\silverstripe-v2.2.2\sapphire\forms\ComplexTableField.php"

What am I doing wrong?

Thanks,
Garrett

Avatar
UncleCheese

Forum Moderator, 4096 Posts

12 July 2008 at 10:08am

This is usually an easy fix. Make sure that every has_many relationship you have corresponds to an object that has a has_one relationship with the other object, and do a /db/build.

Avatar
Garrett

Community Member, 245 Posts

15 July 2008 at 1:30am

Hi,

Thanks very for your reply..... However, I did copy this code exactly, so I still am not sure what I am doing wrong. In my ImageAttachment (extends DataObject) class, I have:

static $has_one = array(
'ImageThumbnail' => 'Image',
'Image' => 'Image'
);

And in my ProjectPage (extends Page) class, the pagetype which will contain the unlimited images, I have:

static $has_many = array(
'ImageAttachments' => 'ImageAttachment'
);

As I said, the ImageAttachment tab IS showing up, but in the POPUP for adding an image attachment, I am getting the "Cannot automatically determine 'has-one'-relationship to parent, please use setParentClass() to set it manually" error.

Your continued help would be greatly appreciated!

Thanks again,
Garrett

Avatar
UncleCheese

Forum Moderator, 4096 Posts

15 July 2008 at 6:44am

Again, for every has_many, you need a reciprocating has_one. Your ImageAttachment class needs a has_one with ProjectPage.

And I'm not so sure about extending the DataObject class for your images. Probably better to extend image, and use the generateXXX() functions.

Avatar
Garrett

Community Member, 245 Posts

18 July 2008 at 3:17am

Hi-- and thanks again for your help. When you say, "Your ImageAttachment class needs a has_one with ProjectPage," I am not sure how to translate that into code, as I haven't seen any examples of this exact architecture.

Here is the $has_one in my ImageAttachment class:

static $has_one = array(
   'ImageMain' => 'Image',
   'ImageThumbnail' => 'Image'
);

How do I refer to the ProjectPage? What do I add here?

'ProjectPage' => '?????'

//Garrett

Avatar
UncleCheese

Forum Moderator, 4096 Posts

18 July 2008 at 3:25am

Edited: 18/07/2008 3:25am

static $has_one = array(
'ImageMain' => 'Image',
'ImageThumbnail' => 'Image'
'ProjectPage' => 'ProjectPage'
);

/db/build?flush=1

Granted, you could really give it any name you want, since you're probably never going to use it as a property. But this will help the DB builder to see the relationship and you won't have to worry about an ambiguous parent/child relationship on your CTF.

Avatar
Garrett

Community Member, 245 Posts

18 July 2008 at 5:00am

Ok, that made the error go away-- thanks! Now ONE more problem: the Images do not appear to be saving to the database. In other words, in the popup, I upload the image, click save, and then when I close the popup nothing has been added to the list.

Here is my code once again:

ProjectPage
------------

class ProjectPage extends Page {

   static $db = array(
       'ProjectChallenge' => 'Text'
      ,'ProjectSolution' => 'Text'
      ,'ProjectResults' => 'Text'
      ,'ImageMainTitle' => 'Text'
   );

   static $has_one = array(
       'ImageMain' => 'Image',
       'ThumbnailMain' => 'Image'
   );

   static $has_many = array(
      'ImageAttachments' => 'ImageAttachment'
   );

   function getCMSFields() {
      $fields = parent::getCMSFields();
      
   $fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectChallenge', 'The Challenge'), 'Content');
   $fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectSolution', 'The Solution'), 'Content');
       $fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectResults', 'The Results'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new ImageField('ImageMain', 'Main Image'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new ImageField('ThumbnailMain', 'Main Thumbnail'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new TextField('ImageMainTitle', 'Main Image Title'), 'Content');
      
      $fields->removeFieldFromTab("Root.Content.Main","Content");
      
      $imagetable = new ComplexTableField(
         $this,
         'ImageAttachments', // relation name
         'ImageAttachment', // object class
         ImageAttachment::$field_names, // fields to show in table
         ImageAttachment::getCMSFields_forPopup(), // form that pops up for edit
         "ImageID = {$this->ID}", // a filter to only display item associated with this page
         "Name ASC" // Sort by name
      );
      $fields->addFieldToTab('Root.Content.ImageAttachments', $imagetable);
   
      return $fields;
   }
}

------------------
ImageAttachment
------------------
<?php

class ImageAttachment extends DataObject {
   static $db = array(
      'Title' => 'Text'
   );
   
   static $has_one = array(
      'Image' => 'Image',
      'ProjectPage' => 'ProjectPage'
   );
   
   static $field_names = array(
      'Image' => 'Image',
      'Title' => 'Title'
   );
   
   function getCMSFields_forPopup() {
      
      $fields = new FieldSet();
      
      $fields->push(new TextField('Title', 'Title'));
      $fields->push(new ImageField('Image', 'Image'));
   
      return $fields;
   }
}

?>

Avatar
UncleCheese

Forum Moderator, 4096 Posts

18 July 2008 at 5:33am

This seems a little suspect..

"ImageID = {$this->ID}", // a filter to only display item associated with this page

I think you want ProjectPageID = {$this->ID}