Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » many_many relationships not creating new table

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

Page: 1
Go to End
Author Topic: 189 Views
  • DROBOKVX2
    Avatar
    Community Member
    1 Post

    many_many relationships not creating new table Link to this post

    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

    189 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.