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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

DataObject can't access has many data via map class


Go to End


6 Posts   1925 Views

Avatar
Chimera

Community Member, 10 Posts

16 May 2013 at 1:46am

Edited: 16/05/2013 1:47am

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

Community Member, 10 Posts

16 May 2013 at 8:28pm

Edited: 16/05/2013 8:30pm

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

Community Member, 344 Posts

17 May 2013 at 12:37am

Edited: 17/05/2013 1:04am

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

Community Member, 10 Posts

17 May 2013 at 1:39am

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

Community Member, 344 Posts

17 May 2013 at 1:46am

Edited: 17/05/2013 1:49am

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

Community Member, 10 Posts

17 May 2013 at 1:51am

Thats worked, thanks man. Cheeers :)