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 /

many_many relationships not creating new table


Reply


283 Views

Avatar
DROBOKVX2

Community Member, 1 Post

15 May 2014 at 11:02am

Hello all,

I am new to SS and am loving it so far!.

I am currently working on the tutorial number 5 and have encountered a problem I cant seem to fix in the many_many relationship part when creating a mentor.

http://doc.silverstripe.org/framework/en/tutorials/5-dataobject-relationship-management

I am using ss v 3.1.5

So i follow the tutorial instructions and create the mentor object in a new file mysite/code/Mentor.php with the code:

<?php
class Mentor extends DataObject {
private static $db = array(
'Name' => 'Varchar',
);
private static $belongs_many_many = array(
'Projects' => 'Project'
);
}

Then i set the relation with the project page type in mysite/code/Project.php which after adding the code now looks like this:

<?php

class Project extends Page {
private static $has_many = array(
'Students' => 'Student'
);

private static $many_many = array(
'Mentors' => 'Mentor'
);

public function getCMSFields() {
// Get the fields from the parent implementation
$fields = parent::getCMSFields();
// Create a default configuration for the new GridField, allowing record editing
$config = GridFieldConfig_RelationEditor::create();
// Set the names and data for our gridfield columns
$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Name' => 'Name',
'Project.Title'=> 'Project' // Retrieve from a has-one relationship
));
// Create a gridfield to hold the student relationship
$studentsField = new GridField(
'Students', // Field name
'Student', // Field title
$this->Students(), // List of all related students
$config
);
// Create a tab named "Students" and add our field to it
$fields->addFieldToTab('Root.Students', $studentsField);
return $fields;

}
}
class Project_Controller extends Page_Controller {
}

now the tutorial says that when i do a rebuild ( url: localhost/silverstripe/dev/build) a table will be created called "Project_Mentors".
The only problem is that no such table is created.

I tried switching the relationship so that under Projects.php , instead of:

private static $many_many = array(
'Mentors' => 'Mentor'
);

I put :

private static $belongs_many_many = array(
'Mentors' => 'Mentor'
);

and in Mentor.php instead of:

private static $belongs_many_many = array(
'Projects' => 'Project'
);

I put:

private static $many_many = array(
'Projects' => 'Project'

which resulted in a table being created but of course it was called "Mentor_Projects" which i assume affects Mentor.php doccument rather than the Project.php page type that I want.

I have tried everything and though I am grateful for a better understanding of how things should work, I can not get my head around this one and fear I am missing something fundemental.

If anyone can help, I am most grateful.

Finlay