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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

complex data model


Go to End


2 Posts   1349 Views

Avatar
jsaade

Community Member, 18 Posts

16 May 2011 at 9:47am

Edited: 17/05/2011 9:27am

Hi,
I have searched the forums for this scenario and I cannot find it, here is my data model:

- User A
	- Project 1
		- Specimen a
		- Specimen b
	- Project 2 
		- Specimen a'
		- Specimen b'

Each user has many projects, each project has many specimens.
I am using $has_many and in the getCMSFields, I use HasManyComplexTableField.
My problem is that if you go to the user, you can add/edit the "Projects"
if you click on a project you can only edit the Specimens, you cannot add.
The add button is simply missing.

Translated into code:
Member Object:

<?php 
class Member_Object extends DataObject
{
    static $db = array (
        "UserName"=>"Text",
		"Password"=>"Text"
    );
	
    static $has_many = array (
        'Projects' => 'Projects_Object'
    );
 
    public function getCMSFields()
    {
		$mng_records = new HasManyComplexTableField(
         $this,
         'Projects',
         'Projects_Object',
         array(
               'Title' => 'Title',
         ),
         'getCMSFields'
      );
		 $fields = parent::getCMSFields();
 
        //Main Tab
        $fields->addFieldToTab("Root.Main", new UniqueTextField('UserName', 'User Name'));   
        $fields->addFieldToTab("Root.Main", new TextField('Password',"Password"));
        $fields->addFieldToTab("Root.Projects", $mng_records);
         
        
        return $fields;
    }
	
   
   static $summary_fields = array(
        'UserName' => 'User Name'
    );  
}
?>

Projects Object:

<?php 
class Projects_Object extends DataObject
{
    static $db = array (
       "Title"=>"Text",
	   "ASTM"=>"Text",
	   "Project"=>"Text",
	   "Client"=>"Text",
	   "Consultant"=>"Text",
	   "DateOfTesting"=>"Text",
	   "DateOfCasting"=>"Text",
	   "AgeOfSpecimen"=>"Text",
	   "Structure"=>"Text",
	   "Formula"=>"Text",
	   "StrengthRequired"=>"Text",
	   "Slump"=>"Text",
	   "TypeOfCuring"=>"Text",
	   "TestedBy"=>"Text",
	   "CheckedBy"=>"Text",
	   "ClientRepresentative"=>"Text"
    );
	
	static $has_many = array (
        'Specimens' => 'Specimens_Object'
    );
	
    static $has_one = array (
        'ParentMember' => 'Member_Object'
    );
	function getCMSFields() {
	  $mng_records = new HasManyComplexTableField(
         $this,
         'Specimens',
         'Specimens_Object',
         array(
               'SpecimenReference' => 'Specimen Reference',
         ),
         'getCMSFields'
      );
	  $fields = parent::getCMSFields();
 
        //Main Tab
        $fields->addFieldToTab("Root.Main", new TextField( 'Title', 'Title'));   
        $fields->addFieldToTab("Root.Main", new TextField( 'ASTM', 'ASTM'));
        $fields->addFieldToTab("Root.Specimens", $mng_records);
		return $fields;
   }
   static $summary_fields = array(
        'Title' => 'Title'
    );   
}
?>

Specimens Object:

<?php 
class Specimens_Object extends DataObject
{
    static $db = array (
	   "SpecimenReference"=>"Text",
	   "Height"=>"Text",
	   "Diameter"=>"Text",
	   "Weight"=>"Text",
	   "Density"=>"Text",
	   "MaxLoad"=>"Text",
	   "CompressiveStrength"=>"Text",
       "TypicalFracturePatterns"=>"Enum('Type I,Type II,Type III,Type IV,Type V,Type VI')",
    );
	
    static $has_one = array (
        'ParentProject' => 'Projects_Object'
    );
	function getCMSFields() {
	  $category =  new DropdownField('TypicalFracturePatterns','Typical Fracture Patterns',singleton('Specimens_Object')->dbObject('TypicalFracturePatterns')->enumValues());
	   $fields = parent::getCMSFields();
 
        //Main Tab
        $fields->addFieldToTab("Root.Main", new TextField( 'SpecimenReference', 'Specimen Reference'));
		$fields->addFieldToTab("Root.Main", new TextField( 'Height', 'Height'));  
		$fields->addFieldToTab("Root.Main", new TextField( 'Diameter', 'Diameter'));  
		$fields->addFieldToTab("Root.Main", new TextField( 'Weight', 'Weight'));  
		$fields->addFieldToTab("Root.Main", new TextField( 'Density', 'Density'));  
		$fields->addFieldToTab("Root.Main", new TextField( 'MaxLoad', 'Max Load'));  
      $fields->addFieldToTab("Root.Main",$category);
		return $fields;
   
   static $summary_fields = array(
        'SpecimenReference' => 'Specimen Reference'
    );  
}
?>

So I can add a member, the go to that member, go to the projects tab, add a project but I cannot add specimens although
according to my login it should work.

What Am I doing wrong?

Thank you.

Avatar
jsaade

Community Member, 18 Posts

17 May 2011 at 9:25am

I am beginning to think it is a bug in nested has_many relations.
it definitely relates to showing the nested relation.