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 /

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

[Solved] Add Image to DataObjectSet?


Go to End
Reply


5 Posts   1281 Views

Avatar
_Vince

Community Member, 163 Posts

25 September 2010 at 1:12am

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

Forum Moderator, 1840 Posts

25 September 2010 at 1:17am

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

Community Member, 163 Posts

25 September 2010 at 1:23am

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

Forum Moderator, 1840 Posts

25 September 2010 at 1:35am

Edited: 25/09/2010 1:36am

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

Community Member, 163 Posts

25 September 2010 at 1:43am

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 %>