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

Custom Tables via Framework?


Go to End


6 Posts   1212 Views

Avatar
Bagzli

Community Member, 71 Posts

18 January 2015 at 8:02am

Edited: 18/01/2015 8:03am

Hello,

I'm trying to create a form where user can come enter 5 names for team a, 5 names for team b, select the date, select which team won, choose who the MVP was on each team and then submit it. I have this form created as following:

class ScoreManager_Controller extends Page_Controller {
   private static $allowed_actions = array('GameResultForm');

    public function GameResultForm() {

        $gameDate = new DateField('GameDate');
        $gameDate->setConfig('showcalendar', true);
        $gameDate->setConfig('dateformat', 'dd/MM/YYYY');
        $fields = new FieldList (
            $gameDate,
            new OptionsetField(
               $name = "WinningTeam",
               $title = "Who won?",
               $source = array(
                  "1" => "Team 1",
                  "2" => "Team 2"
               ),
               $value = "1"
            ),
            new OptionsetField(
               $name = "TeamOneMVP",
               $title = "Who is the MVP for Team 1?",
               $source = array(
                  "1" => "Player A",
                  "2" => "Player B",
                  "3" => "Player C",
                  "4" => "Player D",
                  "5" => "Player E"
               ),
               $value = "1"
            ),
            new TextField('PlayerA'),
            new TextField('PlayerB'),
            new TextField('PlayerC'),
            new TextField('PlayerD'),
            new TextField('PlayerE'),
            new OptionsetField(
               $name = "TeamTwoMVP",
               $title = "Who is the MVP for Team 2?",
               $source = array(
                  "1" => "Player F",
                  "2" => "Player G",
                  "3" => "Player H",
                  "4" => "Player I",
                  "5" => "Player J"
               ),
               $value = "1"
            ),
            new TextField('PlayerF'),
            new TextField('PlayerG'),
            new TextField('PlayerH'),
            new TextField('PlayerI'),
            new TextField('PlayerJ')
        );
        $actions = new FieldList(
            new FormAction('doBrowserPoll', 'Add Group')
        );
        return new Form($this, 'GameResultForm', $fields, $actions);
    }
}

This displays on the page as I need it and works great. My problem comes next. I come from ASP.NET development and there usually I will go and manually create the tables I need and then just create methods that connect to DB, write data to it, or retrieve data from it. With silverstripe I'm not 100% sure how to do this because I don't fully understand how /dev/build works.

Basically I want to create a Player table which will hold all the information about the player. So when I submit that form, it will call a function which will loop through each player and then if the player exists, it will calculate their new ratings and add a count +1 if they are MVP. I also then want to create a second table which will keep track of all games played and who was on them. How do I go about doing this with silverstripe?

If I go to phpmyadmin and create the tables myself, what happens when I run /dev/build, will that destroy my tables, will it alter them in any way? Are there scenarios in the future where I would lose the tables and the data there?

Is it possible to create 2 tables like this with silverstripe framework? Can I write a php file which will create the tables if they don't exist and /dev/build not affect them?

Avatar
martimiz

Forum Moderator, 1391 Posts

20 January 2015 at 7:24am

In SilverStripe you hardly ever (read never) have to create tables by hand. In this case you would probably create a 'Player' DataObject (a class representing the Player data) and add some fields to it. dev/build will than automatically create a table for it, and the the class will provide you with methods to read/write/create forms in the CMS and whatever custom methods you would like to add yourself.

In the same fashion you could create a 'Team' DataObject, a 'Match' DataObject, and define relations between Players and Teams, Teams and Matches (without ever touching the database).

So basically, in your case, you would validate the posted values, instantiate your Player object, feed it with data and write it to database. The nice thing is that you can easily extend this to viewing/editing players in the CMS, creating playerlists in your frontend, and so on.

I'd advice you to walk through the tutorials, to really grasp how this works: http://docs.silverstripe.org/framework/en/tutorials/

Or, nicer, the new Beta docs: http://beta.doc.silverstripe.org/en/tutorials/

Avatar
Bagzli

Community Member, 71 Posts

22 January 2015 at 1:46pm

Thank you for your reply martimiz. DataObjects are exactly what I was looking for. One thing I'm having problems is finding examples of how to write data to my DataObject. I don't know to submit a form like in third tutorial, but to lets say increment a single field in a Player class or set fields to certain values for a certain player.

Avatar
martimiz

Forum Moderator, 1391 Posts

23 January 2015 at 12:09am

Ok, that's another question alltogether... You can always edit Players from within your CMS using ModelAdmin - that's the easiest way:

http://doc.silverstripe.org/en/developer_guides/customising_the_admin_interface/modeladmin

if you really need to edit Players from youw website's frontend, the following thread might give you some hints:

http://www.silverstripe.org/community/forums/form-questions/show/35036

In the frontend, to change one field (say Age) in an existing record, you need to post the Player ID and than in your submit function do something like:

...
$myPlayer = Player::get()->byID($data['ID']);
$myPlayer->Age = $data['Age'];
$myPlayer->write();
...

Of course you need to perform your own extra sanitation if you rely on user input. Preparing for database input isn't necessary.

Avatar
Bagzli

Community Member, 71 Posts

23 January 2015 at 11:09am

I am basically creating this all from front end and not cms. This is how it was requested from me. The only question I have at this point is how do I create a new player record or remove an existing one. Your code shows me how to look up and edit which is what I've had hard time finding, if you could tell me the other half I would really appreciate.

I fully understand I need to protect myself against cross site scripting and sql inject attacks and such, which I will do through validating user input via server side. Thank you for pointing that out.

Avatar
martimiz

Forum Moderator, 1391 Posts

23 January 2015 at 11:32pm

The CMS would normally be the place to manage DataObjects, because it is all setup for it, doing it from the frontend is a lot more complex, if you're just starting SilverStripe...

To delete a DataObject you could do Player::get()->byID(17)->delete();

Also there are existing modules that could help, like maybe http://addons.silverstripe.org/add-ons/webbuilders-group/silverstripe-frontendgridfield

My advice to you is to really dive into the docs, especially the datamodel, DataObjects, DataLists an optionally the gridfield. For instance:

http://doc.silverstripe.org/en/developer_guides/model/data_model_and_orm/
http://doc.silverstripe.org/en/developer_guides/forms/field_types/gridfield/