Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Error with ManyManyDataObjectManager and Images

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1129 Views
  • pingu
    Avatar
    Community Member
    75 Posts

    Error with ManyManyDataObjectManager and Images Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Error with ManyManyDataObjectManager and Images Link to this post

    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);
    }

  • pingu
    Avatar
    Community Member
    75 Posts

    Re: Error with ManyManyDataObjectManager and Images Link to this post

    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!

  • UncleCheese
    Avatar
    4085 Posts

    Re: Error with ManyManyDataObjectManager and Images Link to this post

    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.

    1129 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.