Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [Solved] Add Image to DataObjectSet?

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

Page: 1
Go to End
Author Topic: 957 Views
  • _Vince
    Avatar
    Community Member
    157 Posts

    [Solved] Add Image to DataObjectSet? Link to this post

    Hi, I have an SQLQuery which loads a DataObjectSet.

    I would like to add an image to the DataObjectSet.

    How do I go about it?

    All I can do at the moment is add properties such as the file ID and so on, but I would like to be able to say

    $MyImage.setWidth(100)

    on the template

    Any suggestions are more than welcome, I've been trying to do this all day.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: [Solved] Add Image to DataObjectSet? Link to this post

    Hi,

    easier to see with the code, but my guess would be to...

    foreach ($dosMyDataObjectSet as $doDataObject)
    {
    $doDataObject->MyImage = DataObject::get_by_id('Image',$doDataObject->ImageID);
    }

  • _Vince
    Avatar
    Community Member
    157 Posts

    Re: [Solved] Add Image to DataObjectSet? Link to this post

    the code so far goes like this

    class Products extends DataObject{
       static $db = array(
          "CategoryID" => "Int",
          "SubCategory" => "Varchar(80)",
          "ProductName" => "Varchar(30)",
          "Blurb" => "Text",
          "Link" => "Varchar(100)"
       );
       
       static $has_one = array(
          "ProductImage" => "Image"
       );

    and the query on the Page class goes

          $sqlQuery = new SQLQuery();
          $sqlQuery->select = array(
           "p.*", "f.Filename as Filename", "f.Content as FContent", "f.Title as FTitle",
             "@anum := if(@CategoryID = CategoryID, @anum + 1, 1) as row_num",
             "@CategoryID := CategoryID as dummy"
          );
          
          $sqlQuery->from = array("(SELECT @anum:=0) r,
             (SELECT @CategoryID:='') c, Products p left join File f on f.ID = p.ProductImageID");

    $result = $sqlQuery->execute();

          $Products = new DataObjectSet();

          foreach($result as $row) {
    $Products->push(new ArrayData($row));

          }

    Not sure about this "Image" in your suggestion. Is that like "File"?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: [Solved] Add Image to DataObjectSet? Link to this post

    I see, or rather I don't see, what your sql is doing. But I think what I've given will help... this may help explain it

    controller...

    function MyProducts()
    {
    return DataObject::get('Products ');
    }

    template...

    <% control MyProducts %>
    $ID
    <% control ProductImage %>
    <% control CroppedImage(173,106) %>
    <img src="$URL" alt="$Title" />
    <% end_control %>
    <% end_control %>
    <% end_control %>

    and yes Image is an extension of File

  • _Vince
    Avatar
    Community Member
    157 Posts

    Re: [Solved] Add Image to DataObjectSet? Link to this post

    Yes! Thank you so much, that definitely worked! *whew* I've been stuck on that for the past 14 hours.

    The SQL is adding a row number to each record within a product category.

    I want to be able to have a heading everytime a product category changes, and with that SQL I can set the template to say something like

    <% if rec_num=1%>
    <h2>$ProductCategory</h2>
    <% end_if %>

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