Jump to:

23472 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] ImageID won't record anymore

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 2065 Views
  • Ryan M.
    Avatar
    Community Member
    309 Posts

    [SOLVED] ImageID won't record anymore Link to this post

    I have a photo uploader using SWFUpload to insert images into a Photo DataObject. See code below:

    public function addPhotos($data,$form)
       {
          if(isset($data['uploaded_files']) && is_array($data['uploaded_files']) && isset($data['AlbumID'])) {
             foreach($data['uploaded_files'] as $id) {
                $p = new Photo();
                $p->AlbumID = $data['AlbumID'];
                $p->MemberPageID = $this->Member()->MemberPageID;
                $p->ImageID = $id;
                $p->write();
             }
             Director::redirectBack();
             return;
          }
       }

    The problem is that the ImageID does not get written to its own column in the Photo database table. The File class gets created just fine, the file appears in the assets/Uploads folder. I even echoed $id to find out if it was being passed at all, and yes it is.

    I'm stumped.

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: [SOLVED] ImageID won't record anymore Link to this post

    Alright,

    Are the rest of the photo items getting written correctly? Do you have the Photo DataObject to see the code?
    I've had this issue when the name of the field was wrong or I had a conflicting function checking the database and then using $p-setField('Name','Value') might help. Or you could try using the setComponent function to set the has_one (it is a has_one isn't it?).

    Barry

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: [SOLVED] ImageID won't record anymore Link to this post

    Yes, the rest of the items get written correctly.

    Here is the DataObject code:

    <?php

    class Photo extends DataObject
    {
       static $db = array (
          'Title' => 'Text',
          'Caption' => 'Text',
          'Votes' => 'Int'
       );
       
       static $has_one = array (
          'MemberPage' => 'MemberPage',
          'Image' => 'Image',
          'Album' => 'PhotoAlbum',
          'EventPage' => 'EventPage'
       );
       
       static $has_many = array (
          'Comments' => 'Comment'
       );

       public function DeleteLink()
       {
          return Director::link("Account/deletephoto/$this->ID");
       }
       
       public function Link()
       {
          return $this->MemberPage()->Link("showphoto/$this->ID");
       }
       
       public function EventLink()
       {
          if($this->EventPage()) {
             return $this->EventPage()->Link("showphoto/$this->ID");
          }
       }
       
       public function getCMSFields()
       {
          return new FieldSet(
             new TextField('Title'),
             new TextareaField('Caption'),
             new ImageField('Image'),
             new DataObjectManager($this)
          );
       }
    }

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: [SOLVED] ImageID won't record anymore Link to this post

    I'd have to say I am stumped too, all looks fine, did you try...

    $p->setComponent('Image',DataObject::get_by_id('Image',$id));

    or

    $p->setField('Image',$id);
    (or $p->setField('ImageID',$id);)

    also did you debug show / log the data object before the write and then compare it to one that you 'get' with the right value?

    Out of interest is your photo uploader part of the DOM or is it possible to use it in ModelAdmin? I am very interested in a nice way for a user (public side) and admin (cms side) to upload a bunch of photos... http://silverstripe.org/data-model-questions/show/289206#post289206... if possible could you post an outline of your method please?

    Barry

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: [SOLVED] ImageID won't record anymore Link to this post

    I use the DOM for this, so no I don't interface directly with ModelAdmin.

    $p->setField('ImageID',$id) didn't work.

    I added Debug::show() and put the site in dev mode to capture errors. This is what I got:

    [Warning] Missing argument 2 for Account::addPhotos(), called in /var/www/vhosts/domain.com/httpdocs/sapphire/core/control/Controller.php on line 162 and defined
    POST /Account/addPhotos

    Line 457 in /var/www/vhosts/domain.com/httpdocs/mysite/code/Account.php
    Source

    448             new LiteralField("uploadWarn", "<div class=\"uploadWarn\">Your photos must be resized to 1200x1200 pixels or smaller and take up no more than 5mb of disk space each.</div>"),
    449             new FormAction('addPhotos','Upload')
    450          )
    451       );
    452       if(isset($this->urlParams['ID']))
    453          $fields->push(new HiddenField('AlbumID','',$this->CurrentAlbum()->ID));
    454       return $f;
    455    }
    456    
    457    public function addPhotos($data,$form)
    458    {
    459       if(isset($data['uploaded_files']) && is_array($data['uploaded_files']) && isset($data['AlbumID'])) {
    460          Debug::show();
    461          foreach($data['uploaded_files'] as $id) {
    462             $p = new Photo();
    463             $p->AlbumID = $data['AlbumID'];

    Trace

    * Account->addPhotos(HTTPRequest)
    Line 162 of Controller.php
    * Controller->handleAction(HTTPRequest)
    Line 129 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(/Account/addPhotos)
    Line 118 of main.php

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: [SOLVED] ImageID won't record anymore Link to this post

    change...

    public function addPhotos($data,$form)


    to...

    public function addPhotos($data)


    ?

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: [SOLVED] ImageID won't record anymore Link to this post

    Tried that already, didn't fix it.

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: [SOLVED] ImageID won't record anymore Link to this post

    you got the same error?

    2065 Views
Page: 1 2
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.