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

Adding Images to the testimonials page?


Go to End
Reply


11 Posts   3425 Views

Avatar
Mgc

Community Member, 23 Posts

25 August 2009 at 9:45am

Hi all,

I am in a bit of a tight spot here. I need to figure out how to be able to add images to the DOM. I took some of the sample code from the Testimonials tutorial and modified it a bit.

What I want to do is have images attached to the record entry and then be able to display them on the my site. I have manged to do this and it works but every time i rebuild the database i get this error. This a bad thing because every time I make changes to my code silverstripe get stuck on that error and wont recognize the changes made.

Error:

[Warning] DataObject::__construct passed The value 'Photo'. It's supposed to be passed an array, taken straight from the database. Perhaps you should use DataObject::get_one instead?
GET /bingham/dev/build?flush=1

Line 197 in C:\wamp\www\bingham\sapphire\core\model\DataObject.php
Source

188             'RecordClassName' => get_class($this)
189          );
190       }
191
192       if(!is_array($record)) {
193          if(is_object($record)) $passed = "an object of type '$record->class'";
194          else $passed = "The value '$record'";
195
196          user_error("DataObject::__construct passed $passed. It's supposed to be passed an array,
197          taken straight from the database. Perhaps you should use DataObject::get_one instead?", E_USER_WARNING);
198          $record = null;
199       }
200
201       $this->record = $this->original = $record;
202
203       // Keep track of the modification date of all the data sourced to make this page

Code: Product.php

<?php

class Product extends DataObject
{
   static $db = array (
      'Date' => 'Date',
      'Author' => 'Text',
      'Quote' => 'HTMLText',
      'Photo' => 'Image'
   );
   
   static $has_one = array (
      'ProductPage' => 'ProductPage',
      'Photo' => 'Image'

   );
      
   public function getCMSFields_forPopup()
   {
      return new FieldSet(
         new CalendarDateField('Date'),
         new TextField('Author'),
         new TextareaField('Quote'),
         new ImageField('Photo')
      );
   }
}

?>

ProductPage.php

<?php

class ProductPage extends Page
{
   static $has_many = array (
      'Products' => 'Product',
      'Photo' => 'Image'
   );
   
   public function getCMSFields()
   {
      $f = parent::getCMSFields();
      $f->addFieldToTab("Root.Content.Products", new DataObjectManager(
         $this,
         'Products',
         'Product',
         array('Date' => 'Date','Author'=>'Author','Quote' => 'Quote','Photo' => 'Photo'),
         'getCMSFields_forPopup'
      ));
      return $f;
   }

}

class ProductPage_Controller extends Page_Controller {
   
}
?>

If any one has any suggestions please let me know. I would appreciate all the help i can get thanks in advance.

Avatar
Mgc

Community Member, 23 Posts

25 August 2009 at 9:51am

Also forgot to mention I get the following error when trying to upload an image

[User Error] Couldn't run query: update `Product` SET `PhotoID` = 23, `LastEdited` = now() where ID = 5 Unknown column 'PhotoID' in 'field list'
POST /bingham/images/index/Product/5/Photo/EditImageForm

Line 400 in C:\wamp\www\bingham\sapphire\core\model\MySQLDatabase.php
Source

391    }
392    
393    function databaseError($msg, $errorLevel = E_USER_ERROR) {
394       // try to extract and format query
395       if(preg_match('/Couldn\'t run query: ([^\|]*)\|\s*(.*)/', $msg, $matches)) {
396          $formatter = new SQLFormatter();
397          $msg = "Couldn't run query: \n" . $formatter->formatPlain($matches[1]) . "\n\n" . $matches[2];
398       }
399       
400       user_error($msg, $errorLevel);
401    }
402 }
403
404 /**
405 * A result-set from a MySQL database.
406 * @package sapphire

Trace

* Couldn't run query: update `Product` SET `PhotoID` = 23, `LastEdited` = now() where ID = 5 Unknown column 'PhotoID' in 'field list'
Line 400 of MySQLDatabase.php
* MySQLDatabase->databaseError(Couldn't run query: update `Product` SET `PhotoID` = 23, `LastEdited` = now() where ID = 5 | Unknown column 'PhotoID' in 'field list',256)
Line 102 of MySQLDatabase.php
* MySQLDatabase->query(update `Product` SET `PhotoID` = 23, `LastEdited` = now() where ID = 5)
Line 400 of Database.php
* Database->manipulate(Array)
Line 133 of DB.php
* DB::manipulate(Array)
Line 890 of DataObject.php
* DataObject->write()
Line 757 of Image.php
* Image_Uploader->save(Array,Form,HTTPRequest)
Line 241 of Form.php
* Form->httpSubmission(HTTPRequest)
Line 129 of RequestHandler.php
* RequestHandler->handleRequest(HTTPRequest)
Line 143 of RequestHandler.php
* RequestHandler->handleRequest(HTTPRequest)
Line 122 of Controller.php
* Controller->handleRequest(HTTPRequest)
Line 277 of Director.php
* Director::handleRequest(HTTPRequest,Session)
Line 121 of Director.php
* Director::direct(/images/index/Product/5/Photo/EditImageForm)
Line 118 of main.php

Avatar
UncleCheese

Forum Moderator, 4096 Posts

25 August 2009 at 11:36am

Have you run a /dev/build? There's no reason I can see that Product would not have the column PhotoID.

Avatar
Mgc

Community Member, 23 Posts

26 August 2009 at 1:20am

Yes I have. but every time i do i get this error:

[Warning] DataObject::__construct passed The value 'Photo'. It's supposed to be passed an array, taken straight from the database. Perhaps you should use DataObject::get_one instead?
GET /bingham/dev/build?flush=1

Line 197 in C:\wamp\www\bingham\sapphire\core\model\DataObject.php

Avatar
GeorgeCrawford

Community Member, 8 Posts

12 February 2010 at 9:01am

Did you get this sorted?

I'm having the same problem.

Thanks in advance.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

12 February 2010 at 9:37am

The problem is that he's got the field Photo in both his $db and $has_one arrays. All fields need to be unique.

Avatar
GeorgeCrawford

Community Member, 8 Posts

12 February 2010 at 9:48am

Hi Uncle,

I'm trying to make a custom field for the image at the top of the page template.

This is the code I have in the page.php.

class Page extends SiteTree {
   
   public static $db = array(
   'TagLine' => 'HTMLText',
   'Statement' => 'Text',
   'HeaderImage' => 'Image'
   );
   
   public static $has_one = array(
   );
   
   function getCMSFields() {
   $fields = parent::getCMSFields();
   
   $fields -> addFieldToTab('Root.Content.Main', new TextField('TagLine'), 'Content');
   $fields -> addFieldToTab('Root.Content.Main', new TextField('Statement'), 'Content');
   $fields -> addFieldToTab('Root.Content.Main', new ImageField('HeaderImage'), 'TagLine');
   
   return $fields;
   }
}

I only need to change the "HeaderImage" on the the page template and not any others.

Can you let me know where I'm going wrong.

Thanks,
George

Avatar
UncleCheese

Forum Moderator, 4096 Posts

12 February 2010 at 9:54am

I'm sorry, what is the problem?

Go to Top