Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject::get() and $filter - ISSUE.

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

Page: 1
Go to End
Author Topic: 1665 Views
  • alexbalanoff
    Avatar
    Community Member
    8 Posts

    DataObject::get() and $filter - ISSUE. Link to this post

    This should be relatively easy, BUT for the life of me, I cannot solve it.

    I have created a DataObject - called Advert which is under Classifieds template.
    And I have created 3 separate pages of Classifieds, ie. Classifieds Page 1, Classifieds Page 2, Classifieds Page 3 -

    in the CMS each Classifieds page has a tab with a DOM (manager) with different Adverts (DataOjects).

    I can't get the $filter variable to output only Adverts associated with its PARENT page - instead I just get ALL of the adverts display on each one of the Classifieds pages.

    here's the code...

    <?php
    class ClassifiedsPage extends Page
    {
       static $has_many = array(
          'Classifieds' => 'Advert'
       );

       public function getCMSFields()
    {
    $fields = parent::getCMSFields();

    $manager = new DataObjectManager(
    $this,
    'Classifieds',
    'Advert'
    );
    $fields->addFieldToTab("Root.Content.Classifieds", $manager);

    return $fields;
    }
    }

    class ClassifiedsPage_Controller extends Page_Controller
    {
       function AllClassifieds()
       {
          $classifieds = DataObject::get_one("ClassifiedsPage");
        return ($classifieds) ? DataObject::get(
           $obj = "Advert",
           $filter = "",
           $sort = "Created DESC",
           $join = "",
           $limit = ""
       
          ) : false;
          //$classifieds = DataObject::get($obj, $filter, $sort, $join, $limit);
       }
    }
    ?>

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: DataObject::get() and $filter - ISSUE. Link to this post

    You can just use $Classifieds (or <% control Classifieds %>) to get at all the Adverts associated with the page.

    Otherwise, your filter will be something like '"ParentID" = ' . $this->ID, where Parent is the name of the has_one Advert has with ClassifiedsPage (the ID appended to it is important).

    Also, as your method's in the page type's controller, you don't need to do $classifieds = DataObject::get_one("ClassifiedsPage");, as $this works almost the same as a ClassifiedsPage, and $this->dataRecord is the current ClassifiedsPage.

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: DataObject::get() and $filter - ISSUE. Link to this post

    Just to be sure - did you define the reciprocal $has_one relation in your Advert class? (don't forget to /dev/build/?flush=1)

    static $has_one = array(
       'MyClassifiedsPage' => 'ClassifiedsPage'
    );

  • alexbalanoff
    Avatar
    Community Member
    8 Posts

    Re: DataObject::get() and $filter - ISSUE. Link to this post

    I've just tried the following BUT still having an issue - I get 'Sorry, there was a problem with handling your request.'

    I've added the following to the $filter var (which is causing this) - Whereas if I leave the $filter variable empty, then I get all of the Adverts displaying, NOT just relating to that page.

    class ClassifiedsPage_Controller extends Page_Controller
    {
       function AllClassifieds()
       {
          DataObject::get_one("ClassifiedsPage");
        return ($classifieds) ? DataObject::get(
        $obj = "Advert",
           $filter = '"ParentID" = ' . $this->ID,
           $sort = "Created DESC",
           $join = "",
           $limit = ""
       
          ) : false;
       }
    }

    Any more ideas ??
    OH just making sure - The single Advert class is a DataObject ... not a page.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: DataObject::get() and $filter - ISSUE. Link to this post

    If you get 'Sorry, there was a problem with handling your request.' then put your site in dev mode so we can see what the error is. Just put Director::set_environment_type("dev"); in your _config.php. Also the AllClassifieds function will never return $classifieds since you never put anything in the variable it will always run the DataObject::get function. I'm not sure if it's a typo in your last post since the post before says $classifieds = DataObject::get_one("ClassifiedsPage").

    Like martimiz said check the relationship first. If you haven't defined a has_one in the Advert class then there isn't actually a relationship, has_many does not do anything to the database. It needs the has_one to work at all. If you have that setup correctly and run a /dev/build/?flush=1 you should be able to get all your Adverts belonging to this page by calling $this->Classifieds() in the controller or model or by just writing <% control Classifieds %> in your template just like simon_w said.

  • alexbalanoff
    Avatar
    Community Member
    8 Posts

    Re: DataObject::get() and $filter - ISSUE. Link to this post

    The has_one relationships - Yes, they are there, and they work - since if I dont specify a filter I get every advert display regardless of what classifieds page they fall under.

    This is the error I'm getting:

    [User Error] Couldn't run query: SELECT "Advert"."ClassName", "Advert"."Created", "Advert"."LastEdited", "Advert"."Title", "Advert"."Email", "Advert"."Mobile", "Advert"."Link", "Advert"."Description", "Advert"."Featured", "Advert"."ClassifiedsPageID", "Advert"."PhotoID", "Advert"."ID", CASE WHEN "Advert"."ClassName" IS NOT NULL THEN "Advert"."ClassName" ELSE 'Advert' END AS "RecordClassName" FROM "Advert" WHERE ("ParentID" = 6) ORDER BY Created DESC Unknown column 'ParentID' in 'where clause'

  • alexbalanoff
    Avatar
    Community Member
    8 Posts

    Re: DataObject::get() and $filter - ISSUE. Link to this post

    I've solved it !

    The ParentID in this case is ClassifiedsPageID, so the following works;
    Thanks for everyones help!

    $filter = '"ClassifiedsPageID" = ' . $this->ID,

    1665 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.