Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Update multiple dataobject records

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

Page: 1
Go to End
Author Topic: 1047 Views
  • noisyjerm
    Avatar
    Community Member
    1 Post

    Update multiple dataobject records Link to this post

    I am building a site that includes a page where visitors can upload an image. There will be a moderation page where an authenticated user (administrator) will be able to see the uploaded images and set an accepted / declined flag for each image.

    What I would like to be able to do is update all the records shown on the page in one go, as opposed to iterating through the dataobjects and calling write() on each one. The code I am using follows. All seems to work fine except $mediaItems->write();. Is there a better way to achieve this or is it acceptable to call write() within the for loop?

    MediaDataObject.php

    class MediaDataObject extends DataObject {
    static $db = array(
    'MediaURL' => 'Text',
    'CreatorId' => 'Int',
    'IsApproved' => 'Int'
    );
    }

    MediaPage.ss

    <form action="media/doModerate" method="POST">
    <table>
    <% control mediaList %>
    <tr>
    <td><img src="$MediaURL" /></td>
    <td>$Created.Nice</td>
    <td>
    <input type="radio" name="video[$ID]" value="0" <% if IsApproved==0 %>checked='checked'<% end_if %> />
    <input type="radio" name="video[$ID]" value="1" <% if IsApproved==1 %>checked='checked'<% end_if %> />
    <input type="radio" name="video[$ID]" value="2" <% if IsApproved==2 %>checked='checked'<% end_if %> />
    </td>
    </tr>
    <% end_control %>
    </table>

    MediaPage.php

    class MediaPage_Controller extends Page_Controller {
    function init(){
    parent::init();
    }

    function mediaList() {
    $allMedialDataObjects = DataObject::get('MediaDataObject');
    return $allMedialDataObjects;
    }

    function doModerate(SS_HTTPRequest $request) {
    $vars = $request->postVars();
    $approvals = $vars['video'];
    $mediaItems = DataObject::get('MediaDataObject');   
    foreach ($mediaItems as $mediaItem) {
    $mediaItem->IsApproved = $approvals[$mediaItem->ID] ;
    //$mediaItem->write();
    }
    $mediaItems->write();
    }   
    }

  • baba-papa
    Avatar
    Community Member
    279 Posts

    Re: Update multiple dataobject records Link to this post

    mediaItems is a DataObjectSet and the method "write()" belongs to DataObject. You have to iterate maybe with foreach and write every single one:

    foreach $mediaItems as $mediaItem {
    $mediaItem->write();
    }

    1047 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.