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

DataObject can't access has many data via map class


Reply

6 Posts   512 Views

Avatar
Chimera

16 May 2013 at 1:46am (Last edited: 16 May 2013 1:47am), Community Member, 10 Posts

Hi there,

Iam new in SilverStripe and i have problem. I have DataObject called Property with relation defined like this :

static $has_many = array(
'Attributes' => 'PropertyAttributes'
);

public function PropertyAttributes() {
$attributes = $this->Attributes();
$map = new SS_Map($attributes, "name", "value");
return $map;
}

One property have many attributes stored in DB in name value pairs. I want acces them in template with their name. When i try acces attributes in template $PropertyAttributes.title i am getting this warning

[Warning] call_user_func_array() expects parameter 1 to be a valid callback, class 'SS_Map' does not have a method 'XML_val'

How can i access these attributes in template on each property with their name ?

Avatar
Chimera

16 May 2013 at 8:28pm (Last edited: 16 May 2013 8:30pm), Community Member, 10 Posts

Nobody cant help me ? I just want to acces Property related data with their names instead $name $value pairs which are accessible only in loop, thats why i used map() which rebuild object to needed form.

Avatar
Devlin

17 May 2013 at 12:37am (Last edited: 17 May 2013 1:04am), Community Member, 215 Posts

Map returns an array from a DataList, but you cannot use arrays in templates. Templates expect ViewableData.

Maybe I don't understand you correctly, but maybe this helps.

class MyDataObject extends DataObject{
   static $has_many = array(
      'Attributes' => 'PropertyAttributes'
   );

   function getProperty($name) {
      return $this->Attributes()
            ->filter(array('Name' => Convert::raw2sql($name)))
            ->first();
   }
}

Template:

<% with $Property('foo') %>
$Value
<% end_with %>

// or
$Property('foo').Value

// vanilla style
<% loop $Attributes %>
<% if $Name = 'Foo' %>
$Value
<% end_if %>
<% end_with %>

Avatar
Chimera

17 May 2013 at 1:39am Community Member, 10 Posts

Thanks, it looks great but when i use it as you described iam still getting this error

[Warning] Missing argument 1 for Property::getAttribute(), called in D:\WorkSpace\silverstripe\framework\view\ViewableData.php on line 106 and defined

my code

function getAttribute($name) {
if ($name) {
return $this->Attributes()
->filter(array('name' => Convert::raw2sql($name)))
->first();
}
return null;
}

<% with $Attribute(title) %>
$Value
<% end_with %>

also tried ...

$Attribute(title)

Avatar
Devlin

17 May 2013 at 1:46am (Last edited: 17 May 2013 1:49am), Community Member, 215 Posts

Yeah, my fault. Fancy getter/setter methods apparently don't work with arguments. Try:

<% with $getAttribute(title) %>
$Value
<% end_with %>

$getAttribute(title).Value

Avatar
Chimera

17 May 2013 at 1:51am Community Member, 10 Posts

Thats worked, thanks man. Cheeers :)