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 /

Getting a Page From Database By ID


Reply


3 Posts   2619 Views

Avatar
jsaade

Community Member, 18 Posts

20 March 2011 at 3:49am

I have a Pagetype called CarHolder, I am trying to get one of the pages by its ID:
function getCar()
   {
      return DataObject::get_one('CarHolder',"ID=1");
   }

I am getting an error (User Error Cannot execute query).
Although I can Actually Run:
DataObject::get_one('CarHolder');
Then in the template if I just echo the $ID it is 1.
The exact error:

GET /AutoImmo/show-car?id=53

Line 525 in C:\wamp\www\AutoImmo\sapphire\core\model\MySQLDatabase.php
Source

516    }
517    
518    function databaseError($msg, $errorLevel = E_USER_ERROR) {
519       // try to extract and format query
520       if(preg_match('/Couldn\'t run query: ([^\|]*)\|\s*(.*)/', $msg, $matches)) {
521          $formatter = new SQLFormatter();
522          $msg = "Couldn't run query: \n" . $formatter->formatPlain($matches[1]) . "\n\n" . $matches[2];
523       }
524       
525       user_error($msg, $errorLevel);
526    }
527    
528    /**
529     * Return a boolean type-formatted string
530     *
531     * @param array $values Contains a tokenised list of info about this data type

Trace

* Couldn't run query: SELECT "SiteTree_Live"."ClassName", "SiteTree_Live"."Created", "SiteTree_Live"."LastEdited", "SiteTree_Live"."URLSegment", "SiteTree_Live"."Title", "SiteTree_Live"."MenuTitle", "SiteTree_Live"."Content", "SiteTree_Live"."MetaTitle", "SiteTree_Live"."MetaDescription", "SiteTree_Live"."MetaKeywords", "SiteTree_Live"."ExtraMeta", "SiteTree_Live"."ShowInMenus", "SiteTree_Live"."ShowInSearch", "SiteTree_Live"."HomepageForDomain", "SiteTree_Live"."ProvideComments", "SiteTree_Live"."Sort", "SiteTree_Live"."HasBrokenFile", "SiteTree_Live"."HasBrokenLink", "SiteTree_Live"."Status", "SiteTree_Live"."ReportClass", "SiteTree_Live"."CanViewType", "SiteTree_Live"."CanEditType", "SiteTree_Live"."ToDo", "SiteTree_Live"."Version", "SiteTree_Live"."Priority", "SiteTree_Live"."ParentID", "CarHolder_Live"."Car_Vitesse", "CarHolder_Live"."Car_Category", "CarHolder_Live"."Car_Version", "CarHolder_Live"."Car_Year", "CarHolder_Live"."Car_Mileage", "CarHolder_Live"."Car_Price", "CarHolder_Live"."Car_Description", "CarHolder_Live"."Car_Contact_Name", "CarHolder_Live"."Car_Contact_Email", "CarHolder_Live"."Car_Contact_Phone", "CarHolder_Live"."Car_Agent", "CarHolder_Live"."Car_DateIn", "CarHolder_Live"."Car_DateOut", "CarHolder_Live"."ModelID", "CarHolder_Live"."BrandID", "SiteTree_Live"."ID", CASE WHEN "SiteTree_Live"."ClassName" IS NOT NULL THEN "SiteTree_Live"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree_Live" LEFT JOIN "CarHolder_Live" ON "CarHolder_Live"."ID" = "SiteTree_Live"."ID" WHERE ("SiteTree_Live"."ClassName" IN ('CarHolder')) AND (ID=1) ORDER BY "Sort" LIMIT 1 Column 'ID' in where clause is ambiguous
Line 525 of MySQLDatabase.php
* MySQLDatabase->databaseError(Couldn't run query: SELECT "SiteTree_Live"."ClassName", "SiteTree_Live"."Created", "SiteTree_Live"."LastEdited", "SiteTree_Live"."URLSegment", "SiteTree_Live"."Title", "SiteTree_Live"."MenuTitle", "SiteTree_Live"."Content", "SiteTree_Live"."MetaTitle", "SiteTree_Live"."MetaDescription", "SiteTree_Live"."MetaKeywords", "SiteTree_Live"."ExtraMeta", "SiteTree_Live"."ShowInMenus", "SiteTree_Live"."ShowInSearch", "SiteTree_Live"."HomepageForDomain", "SiteTree_Live"."ProvideComments", "SiteTree_Live"."Sort", "SiteTree_Live"."HasBrokenFile", "SiteTree_Live"."HasBrokenLink", "SiteTree_Live"."Status", "SiteTree_Live"."ReportClass", "SiteTree_Live"."CanViewType", "SiteTree_Live"."CanEditType", "SiteTree_Live"."ToDo", "SiteTree_Live"."Version", "SiteTree_Live"."Priority", "SiteTree_Live"."ParentID", "CarHolder_Live"."Car_Vitesse", "CarHolder_Live"."Car_Category", "CarHolder_Live"."Car_Version", "CarHolder_Live"."Car_Year", "CarHolder_Live"."Car_Mileage", "CarHolder_Live"."Car_Price", "CarHolder_Live"."Car_Description", "CarHolder_Live"."Car_Contact_Name", "CarHolder_Live"."Car_Contact_Email", "CarHolder_Live"."Car_Contact_Phone", "CarHolder_Live"."Car_Agent", "CarHolder_Live"."Car_DateIn", "CarHolder_Live"."Car_DateOut", "CarHolder_Live"."ModelID", "CarHolder_Live"."BrandID", "SiteTree_Live"."ID", CASE WHEN "SiteTree_Live"."ClassName" IS NOT NULL THEN "SiteTree_Live"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree_Live" LEFT JOIN "CarHolder_Live" ON "CarHolder_Live"."ID" = "SiteTree_Live"."ID" WHERE ("SiteTree_Live"."ClassName" IN ('CarHolder')) AND (ID=1) ORDER BY "Sort" LIMIT 1 | Column 'ID' in where clause is ambiguous,256)
Line 123 of MySQLDatabase.php
* MySQLDatabase->query(SELECT "SiteTree_Live"."ClassName", "SiteTree_Live"."Created", "SiteTree_Live"."LastEdited", "SiteTree_Live"."URLSegment", "SiteTree_Live"."Title", "SiteTree_Live"."MenuTitle", "SiteTree_Live"."Content", "SiteTree_Live"."MetaTitle", "SiteTree_Live"."MetaDescription", "SiteTree_Live"."MetaKeywords", "SiteTree_Live"."ExtraMeta", "SiteTree_Live"."ShowInMenus", "SiteTree_Live"."ShowInSearch", "SiteTree_Live"."HomepageForDomain", "SiteTree_Live"."ProvideComments", "SiteTree_Live"."Sort", "SiteTree_Live"."HasBrokenFile", "SiteTree_Live"."HasBrokenLink", "SiteTree_Live"."Status", "SiteTree_Live"."ReportClass", "SiteTree_Live"."CanViewType", "SiteTree_Live"."CanEditType", "SiteTree_Live"."ToDo", "SiteTree_Live"."Version", "SiteTree_Live"."Priority", "SiteTree_Live"."ParentID", "CarHolder_Live"."Car_Vitesse", "CarHolder_Live"."Car_Category", "CarHolder_Live"."Car_Version", "CarHolder_Live"."Car_Year", "CarHolder_Live"."Car_Mileage", "CarHolder_Live"."Car_Price", "CarHolder_Live"."Car_Description", "CarHolder_Live"."Car_Contact_Name", "CarHolder_Live"."Car_Contact_Email", "CarHolder_Live"."Car_Contact_Phone", "CarHolder_Live"."Car_Agent", "CarHolder_Live"."Car_DateIn", "CarHolder_Live"."Car_DateOut", "CarHolder_Live"."ModelID", "CarHolder_Live"."BrandID", "SiteTree_Live"."ID", CASE WHEN "SiteTree_Live"."ClassName" IS NOT NULL THEN "SiteTree_Live"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree_Live" LEFT JOIN "CarHolder_Live" ON "CarHolder_Live"."ID" = "SiteTree_Live"."ID" WHERE ("SiteTree_Live"."ClassName" IN ('CarHolder')) AND (ID=1) ORDER BY "Sort" LIMIT 1,256)
Line 129 of DB.php
* DB::query(SELECT "SiteTree_Live"."ClassName", "SiteTree_Live"."Created", "SiteTree_Live"."LastEdited", "SiteTree_Live"."URLSegment", "SiteTree_Live"."Title", "SiteTree_Live"."MenuTitle", "SiteTree_Live"."Content", "SiteTree_Live"."MetaTitle", "SiteTree_Live"."MetaDescription", "SiteTree_Live"."MetaKeywords", "SiteTree_Live"."ExtraMeta", "SiteTree_Live"."ShowInMenus", "SiteTree_Live"."ShowInSearch", "SiteTree_Live"."HomepageForDomain", "SiteTree_Live"."ProvideComments", "SiteTree_Live"."Sort", "SiteTree_Live"."HasBrokenFile", "SiteTree_Live"."HasBrokenLink", "SiteTree_Live"."Status", "SiteTree_Live"."ReportClass", "SiteTree_Live"."CanViewType", "SiteTree_Live"."CanEditType", "SiteTree_Live"."ToDo", "SiteTree_Live"."Version", "SiteTree_Live"."Priority", "SiteTree_Live"."ParentID", "CarHolder_Live"."Car_Vitesse", "CarHolder_Live"."Car_Category", "CarHolder_Live"."Car_Version", "CarHolder_Live"."Car_Year", "CarHolder_Live"."Car_Mileage", "CarHolder_Live"."Car_Price", "CarHolder_Live"."Car_Description", "CarHolder_Live"."Car_Contact_Name", "CarHolder_Live"."Car_Contact_Email", "CarHolder_Live"."Car_Contact_Phone", "CarHolder_Live"."Car_Agent", "CarHolder_Live"."Car_DateIn", "CarHolder_Live"."Car_DateOut", "CarHolder_Live"."ModelID", "CarHolder_Live"."BrandID", "SiteTree_Live"."ID", CASE WHEN "SiteTree_Live"."ClassName" IS NOT NULL THEN "SiteTree_Live"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree_Live" LEFT JOIN "CarHolder_Live" ON "CarHolder_Live"."ID" = "SiteTree_Live"."ID" WHERE ("SiteTree_Live"."ClassName" IN ('CarHolder')) AND (ID=1) ORDER BY "Sort" LIMIT 1,256)
Line 400 of SQLQuery.php
* SQLQuery->execute()
Line 2884 of DataObject.php
* DataObject->instance_get_one(ID=1,)
Line 2808 of DataObject.php
* DataObject::get_one(CarHolder,ID=1)
Line 11 of CarInfo.php
* CarInfo_Controller->getCar()
Line 112 of ViewableData.php

Avatar
simon_w

Forum Moderator, 474 Posts

20 March 2011 at 8:33am

Even though you didn't include the actual error message (it's the thing above the call stack), I'm assuming it'll be ID in where clause is ambiguous. To get your code working, you can use:

return DataObject::get_one('CarHolder',"SiteTree"."ID"=1');


Though simply using

return DataObject::get_by_id('CarHolder', 1);


will also work, until someone changes the page type of the page with ID 1.

Avatar
jsaade

Community Member, 18 Posts

25 June 2011 at 3:46am

Thanks I figured it out but forgot to thank you :)