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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Need Help: Couldn't run query: SELECT DISTINCT DATE_FORMAT(`Date`,'%Y_%M_%m')


Go to End


3 Posts   1510 Views

Avatar
jamesmburnett

Community Member, 3 Posts

12 September 2016 at 6:24pm

Edited: 12/09/2016 6:32pm

Hi, I am pretty new to Silverstripe and I am getting this error on my Latest News page.

'[User Error] Uncaught SS_DatabaseException: Couldn't run query: SELECT DISTINCT DATE_FORMAT(`Date`,'%Y_%M_%m') AS "DateString" FROM ArticlePage_Stage ORDER BY Date ASC Table 'db159598_dhub.ArticlePage_Stage' doesn't exist'

My Article Holder page is set up like this ArticleHolder.php

    public function ArchiveDates() {
		$list = ArrayList::create();
		$stage = Versioned::current_stage();		
		$query = new SQLQuery(array ());
		$query->selectField("DATE_FORMAT(`Date`,'%Y_%M_%m')","DateString")
			  ->setFrom("ArticlePage_{$stage}")
			  ->setOrderBy("Date", "ASC")
			  ->setDistinct(true);
		$result = $query->execute();
		
		if($result) {
			while($record = $result->nextRecord()) {
				list($year, $monthName, $monthNumber) = explode('_', $record['DateString']);
				$list->push(ArrayData::create(array(
					'Year' => $year,
					'MonthName' => $monthName,
					'MonthNumber' => $monthNumber,
					'Link' => $this->Link("date/$year/$monthNumber"),
					'ArticleCount' => ArticlePage::get()->where("
							DATE_FORMAT(`Date`,'%Y%m') = '{$year}{$monthNumber}'
							AND ParentID = {$this->ID}
						")->count()
				)));
			}
		}
		return $list;
	}

and my ArticlePage.php is set up like this

public function getCMSFields() {
    			$fields = parent::getCMSFields();
			    $fields->addFieldToTab('Root.Main', DateField::create('Date','Date of article')
          				->setConfig('showcalendar', true)
     					 ,'Content');  
			    $fields->addFieldToTab('Root.Main', TextareaField::create('Teaser'),'Content');
			    $fields->addFieldToTab('Root.Main', TextField::create('Author','Author of article'),'Content');

			    $fields->addFieldToTab('Root.Attachments', $photo = UploadField::create('Photo'));
        		$fields->addFieldToTab('Root.Attachments', $brochure = UploadField::create('Brochure','(PDF only)'));

        		$photo->getValidator()->setAllowedExtensions(array('png','gif','jpg','jpeg'));
        		$photo->setFolderName('articles-photos');
        		$brochure->getValidator()->setAllowedExtensions(array('pdf'));
        		$brochure->setFolderName('articles-brochure');

        		$fields->addFieldToTab('Root.Categories', CheckboxSetField::create(
		            'Categories',
		            'Selected categories',
		            $this->Parent()->Categories()->map('ID','Title')
	      		));

    			return $fields;
  			}

Im not really sure how to fix this as I am a bit of a noob at silverstripe. Any help would be great

Avatar
Vlad Belfort

Community Member, 55 Posts

14 September 2016 at 9:58pm

It looks like you're trying to get the current version in ArticleHolder.php

The $stage = Versioned::current_stage(); bit is returning "Stage" which is why your query is looking for a table called "ArticlePage_Stage" which doesn't exist...

I guess for the version of a page try something like :

$stage = Versioned::get_version('SiteTree', $this->ID, 1);
replace the 1 with any version number you want to get

Avatar
doglitbug

Community Member, 1 Post

21 June 2017 at 4:39pm

Edited: 21/06/2017 4:48pm

Replace the given section of code in ArticleHolder.php:

		$stage = Versioned::current_stage();				

		$query = new SQLQuery(array ());
		$query->selectField("DATE_FORMAT(`Date`,'%Y_%M_%m')","DateString")
			  ->setFrom("ArticlePage_{$stage}")
			  ->setOrderBy("Date", "ASC")
			  ->setDistinct(true);

with

		$query = new SQLQuery(array ());
		$query->selectField("DATE_FORMAT(`Date`,'%Y_%M_%m')","DateString")
			  ->setFrom("ArticlePage")
			  ->setOrderBy("Date", "ASC")
			  ->setDistinct(true);

This should enable you to continue with lesson 20