Set/read an object attribute that is not in the database

Silverstripe Version:3.1

How can I add an attribute/property (does not exit in the DB) to an object and be able to access it in a template

I have the following declaration for my product model

class Product extends SlugModel {
private static $db = array(
        'Name' => 'Varchar(200)',
        'Code' => 'Varchar(200)',
);
}

I have another script looping through the product and setting a “Large” attribute

public function ProdList() {

$products = $this->Products()->column('ID');
$large_product_ids = array(1,2,3);

foreach($products as $p){
if (in_array($p->ID, $large_product_ids)){
    $p->update(array('Large'=>'Y'));
} else {
    $p->update(array('Large'=>'N'));
}
}
return $products;
}

I have tried to access the Large attribute in the ss file, but I get a blank value. How can I access the Large value??

<% loop $ProdList %>
$Name $Large
<% end_loop %>

I do not want to add the Large attribute to the Product model.

I think what I’d do is create a new method on the Products class that will return what you want. That way it will be accessible in any scope (templates, PHP, etc.)

public function getLarge() {
  $large_product_ids = array(1,2,3);
  if (in_array($this->ID, $large_product_ids)) {
    return true;
  }
  return false;
}

With that in place, anywhere you are looping a Products collection, you can access the Large property automagically.

In some PHP:

$allProducts = Products::get();
foreach ($allProducts as $product) {
  if ($product->Large === true) {
   //It's a large one
  }
}

or in a template:

<% loop $Products %>
  <% if $Large %>
    <p>Big</p>
  <% else %>
   <p>Not so big</p>
  <% end_if %>
<% end_loop %>