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.

General Questions

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

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

Error When Creating page with DataObject


Go to End
Reply

9 Posts   1195 Views

Avatar
MagicUK

20 July 2011 at 1:18am Community Member, 60 Posts

Hi.

Small problem i was hoping you guys could help.

I am creating a page that lists the speakers of a specific event. I created a dataobject to hold the speakers here:

mysite/code/Speakers.php

<?php
class Speakers extends DataObject {

   static $db = array(
'FirstName' => 'Text',
'LastName' => 'Text',
'Position' => 'Text',
'Company' => 'Text',

);
public static $has_one = array(
   
      'SpeakerImage' => 'Image',      
      
   );

function getCMSFields_forPopup() {
$fields = new FieldSet();

$fields->push( new TextField( 'FirstName', 'First Name' ) );
$fields->push( new TextField( 'LastName' ) );
$fields->push( new TextField( 'Position' ) );
$fields->push( new TextField( 'Company' ) );
$fields->push( new ImageField('SpeakerImage') );

return $fields;
}
   

}
class Speakers_Controller extends ContentController {

   
   public static $allowed_actions = array (
   );

   public function init() {
      parent::init();

   }
}

And I want to manage the Speakers for the event here:

themes/templates/SpeakersPage.php

<?php
class SpeakersPage extends SiteTree {

   public static $db = array(
   'RightContent' => 'HTMLText',
   
   );

   public static $has_one = array(
      'MySpeakers' => 'Speakers',
   );
   
    function getCMSFields() {
$fields = parent::getCMSFields();

$tablefield = new HasManyComplexTableField(
$this,
'MySpeakers',
'Speakers',
array(
'FirstName' => 'First Name',
'LastName' => 'Family Name',
'Position' => 'Position',
'Company' => 'Company',
'SpeakerImage' => 'Speaker Image',
),
'getCMSFields_forPopup'
);

$tablefield->setParentClass('Speakers');
$fields->addFieldToTab( 'Root.Content.Speakers', $tablefield );

$fields->addFieldToTab("Root.Content.Main", new HtmlEditorField('RightContent', 'Right Content'));

return $fields;
}
   

}
class SpeakersPage_Controller extends ContentController {

   
   public static $allowed_actions = array (
   );

   public function init() {
      parent::init();

      Requirements::themedCSS('reset');
      Requirements::themedCSS('960');
      Requirements::themedCSS('layout');
      Requirements::themedCSS('typography');
      Requirements::themedCSS('form');
      Requirements::javascript("js/jquery-1.6.1.min.js");
      Requirements::javascript("js/jquery.nivo.slider.pack.js");
      Requirements::javascript("js/slideshow.js");
   }
}

I get the following error when Creating the page in Dev mode:

<br />
<b>Notice</b>: Debug::log_error_if_necessary() and Debug::log_errors_to() are deprecated. Please use SS_Log instead.
         See the class documentation in SS_Log.php for more information. in <b>/var/www/vhosts/oilcouncil.com/httpdocs/event/sapphire/dev/Debug.php</b> on line <b>486</b><br />
ERROR [User Warning]: Can't find a has_one relationship from 'Speakers' to 'SpeakersPage'
IN POST /event/admin/getitem?ID=17&ajax=1
Line 56 in /var/www/vhosts/oilcouncil.com/httpdocs/event/sapphire/forms/HasManyComplexTableField.php

Source
======
47:    protected $relationAutoSetting = false;
48:    
49:    function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null,
$sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
50:       parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields,
$sourceFilter, $sourceSort, $sourceJoin);
51:       
52:       $this->Markable = true;
53:
54:       if($controllerClass = $this->controllerClass()) {
55:          $this->joinField = $this->getParentIdName($controllerClass, $this->sourceClass);
* 56:          if(!$this->joinField) user_error("Can't find a has_one relationship from '$this->sourceClass' to
'$controllerClass'", E_USER_WARNING);
57:       } else {
58:          user_error("Can't figure out the data class of $controller", E_USER_WARNING);
59:       }
60:       
61:    }
62:    

Trace
=====
<ul>user_error(Can't find a has_one relationship from 'Speakers' to 'SpeakersPage',512)
line 56 of HasManyComplexTableField.php

HasManyComplexTableField->__construct(SpeakersPage,MySpeakers,Speakers,Array,getCMSFields_forPopup)
line 29 of SpeakersPage.php

SpeakersPage->getCMSFields(CMSMain)
line 441 of CMSMain.php

CMSMain->getEditForm(17)
line 1039 of LeftAndMain.php

LeftAndMain->EditForm()
line 389 of LeftAndMain.php

LeftAndMain->getitem(SS_HTTPRequest)
line 193 of Controller.php

Controller->handleAction(SS_HTTPRequest)
line 143 of RequestHandler.php

RequestHandler->handleRequest(SS_HTTPRequest)
line 147 of Controller.php

Controller->handleRequest(SS_HTTPRequest)
line 282 of Director.php

Director::handleRequest(SS_HTTPRequest,Session)
line 125 of Director.php

Director::direct(/admin/getitem)
line 127 of main.php

</ul>

Any help would be appreciated!

Avatar
tv

20 July 2011 at 1:24am Community Member, 44 Posts

Hey, I'm sorta new to SS, but I don't think DataObjects have a Controller, only Pages. Maybe that will get you started.

Avatar
MagicUK

20 July 2011 at 1:29am Community Member, 60 Posts

Thanks tv. I deleted the controller but it still getting the same error. Looks like the important point in the error message is "Can't find a has_one relationship from 'Speakers' to 'SpeakersPage'"

Looks ok to me though. Am I missing something fundamental?

Avatar
MagicUK

20 July 2011 at 2:11am Community Member, 60 Posts

OK. I think I'm on the right track here. What I need is to be able to add loads of Speakers to the Speakers Page. So Thats why I had the 'HasManyComplexTableField'

I realise now that the corresponding code in the speakersPage can't be

public static $has_one = array(
'MySpeakers' => 'Speakers',
);

I tried:

public static $has_many = array(
'MySpeakers' => 'Speakers',
);

But I still got an error? Little help?

Avatar
tv

20 July 2011 at 2:14am Community Member, 44 Posts

Do you need to create a has_many relationship on Speakers? Or maybe reverse the relationship and have the has_one relationship to SpeakersPage on Speakers, so each Speaker has one SpeakerPage? Just a thought...

Avatar
swaiba

20 July 2011 at 2:15am Forum Moderator, 1796 Posts

You need to reciprocate the relationship with a has_many on the Page. so...

Speakers has_one SpeakersPage
SpeakersPage has_many Speakers

(and the controller is a very valid part of a dataobject)

Avatar
MagicUK

20 July 2011 at 2:28am Community Member, 60 Posts

Great thanks swabia. Sorry for you hasving to spoon feed me here but I tried it but I wasn't exactly sure of the syntax to refer to the speakers page in the has_one function "Speakers has_one SpeakersPage"

I tried in Speakers dataobject:

public static $has_one = array(
      'MySpeakers' => 'Speakers',
   
);

But didn't work. Thanks, appreciate the help.

Avatar
swaiba

20 July 2011 at 2:35am Forum Moderator, 1796 Posts

This might help...
http://doc.silverstripe.org/sapphire/en/topics/datamodel

Go to Top