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::get() and $filter - ISSUE.


Reply


7 Posts   1970 Views

Avatar
alexbalanoff

Community Member, 8 Posts

6 August 2011 at 2:09pm

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);
   }
}
?>

Avatar
simon_w

Forum Moderator, 474 Posts

6 August 2011 at 2:58pm

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.

Avatar
martimiz

Forum Moderator, 1132 Posts

6 August 2011 at 11:05pm

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'
);

Avatar
alexbalanoff

Community Member, 8 Posts

7 August 2011 at 9:24am

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.

Avatar
MarcusDalgren

Community Member, 288 Posts

7 August 2011 at 10:00am

Edited: 07/08/2011 10:01am

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.

Avatar
alexbalanoff

Community Member, 8 Posts

7 August 2011 at 11:27am

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'

Avatar
alexbalanoff

Community Member, 8 Posts

7 August 2011 at 11:30am

I've solved it !

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

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