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.

Data Model Questions

[Solved] Add Image to DataObjectSet?


Reply

5 Posts   1016 Views

Avatar
_Vince

25 September 2010 at 1:12am Community Member, 161 Posts

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.

Avatar
swaiba

25 September 2010 at 1:17am Forum Moderator, 1796 Posts

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

Avatar
_Vince

25 September 2010 at 1:23am Community Member, 161 Posts

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"?

Avatar
swaiba

25 September 2010 at 1:35am (Last edited: 25 September 2010 1:36am), Forum Moderator, 1796 Posts

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

Avatar
_Vince

25 September 2010 at 1:43am Community Member, 161 Posts

Yes! :D 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 %>