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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Error with ManyManyDataObjectManager and Images


Reply


4 Posts   1179 Views

Avatar
pingu

Community Member, 75 Posts

28 June 2009 at 5:29pm

Hi,

I'm having an sql error with ManyManyDataObject Manager where the dataobject contains an image.

If I use a ManyManyComplexTable field, the admin works fine. However, on changing that to a DataObjectManager, I get an sql error. My DataObject is defined as follows:

class Sponsor extends DataObject {

static $db = array(
'Name' => 'Text',
'URL' => 'Text'
);

   public static $has_one = array(
      'Logo' => 'Image'
   );
   
   public static $belongs_many_many = array(
      'EventDetails' => 'EventDetail'
   );
...
}

the error I get is:

[User Error] Couldn't run query: SELECT `Sponsor`.*, `Sponsor`.ID, if(`Sponsor`.ClassName,`Sponsor`.ClassName,'Sponsor') AS RecordClassName, Logo, IF(`EventDetailID` IS NULL, '0', '1') AS Checked FROM `Sponsor` LEFT JOIN `EventDetail_Sponsors` ON (`Sponsor`.`ID` = `SponsorID` AND `EventDetailID` = '15') GROUP BY `Sponsor`.ID ORDER BY SortOrder ASC LIMIT 0, 10 Unknown column 'Logo' in 'field list'

If in EventDetail.php I change the dataobject constructor from

$tablefield = new ManyManyDataObjectManager(
$this,
'Sponsors',
'Sponsor',
array(
       'Name' => 'Name',
       'URL' => 'URL',
       'Logo' => 'Logo'
),
'getCMSFields_forPopup'
   );

to

$tablefield = new ManyManyDataObjectManager(
$this,
'Sponsors',
'Sponsor',
array(
       'Name' => 'Name',
       'URL' => 'URL',
       'LogoID'=> 'Logo'
),
'getCMSFields_forPopup'
   );

(i.e., change "Logo" to "LogoID") Then I no longer get the error - but I can't see my image thumbnail in the table.

Any ideas on how I can resolve this?

Avatar
UncleCheese

Forum Moderator, 4094 Posts

28 June 2009 at 7:21pm

If you're going to display a foreign object on the table I believe you need to use a custom getter.

function getLogo()
{
return $this->Logo()->CroppedImage(50,50);
}

Avatar
pingu

Community Member, 75 Posts

28 June 2009 at 8:53pm

Hi Unclecheese -

Could you give me some more detail on how to use the getter method?
I've added it to my Sponsor.php class but I'm not sure how to reference it in the EventDetail page dataobject constructor?

Thanks!

Avatar
UncleCheese

Forum Moderator, 4094 Posts

29 June 2009 at 4:38am

In this array:

array(
'Name' => 'Name',
'URL' => 'URL',
'Logo' => 'Logo'
),

You're saying that the field "Name" should have the header "Name" and "URL" should have the header "URL," etc. Most of the time, SS just gets the fields named Name and URL off the Sponsor object. However, it will always check first to see if the method get{Fieldname} is defined on the object. If so, it will run that function to get the value. This affords you some custom display control, and also helps in getting foreign data, for instance:

public function getFullName()
{
return $this->FirstName . " " . $this->LastName;
}

could be called using:

array('FullName' => 'Full name of client');

or, say you have a student with a has_one Teacher..

public function getTeacherName()
{
return $this->Teacher()->Name;
}

array('Name' => 'Student Name', 'TeacherName' => 'Teacher Name');

In your case, you're handling foreign data with the Logo object, so you need to define getLogo() in your Sponsor.php model as I coded for you above. You should then be able to call it using "Logo" => "Logo" and it will catch the get{Fieldname} method before it tries to render the non-existent "Logo" field on your Sponsor object.