Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject can't access has many data via map class

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

Page: 1
Go to End
Author Topic: 369 Views
  • Chimera
    Avatar
    Community Member
    10 Posts

    DataObject can't access has many data via map class Link to this post

    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 ?

  • Chimera
    Avatar
    Community Member
    10 Posts

    Re: DataObject can't access has many data via map class Link to this post

    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.

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: DataObject can't access has many data via map class Link to this post

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

  • Chimera
    Avatar
    Community Member
    10 Posts

    Re: DataObject can't access has many data via map class Link to this post

    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)

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: DataObject can't access has many data via map class Link to this post

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

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

    $getAttribute(title).Value

  • Chimera
    Avatar
    Community Member
    10 Posts

    Re: DataObject can't access has many data via map class Link to this post

    Thats worked, thanks man. Cheeers

    369 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.