Adding fields

From AV Scripts Wiki
Revision as of 14:03, 17 February 2016 by Andy (Talk | contribs) (Created page with "If you want to add a field to a form in the admin panel and have that value save to the database you can do so without massive code changes. For the sake of this guide, we wi...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

If you want to add a field to a form in the admin panel and have that value save to the database you can do so without massive code changes.

For the sake of this guide, we will be adding a new field called "my_field" to the database and a form field to set that value. You should replace instances of "my_field" and "myField" with your field name.

Adding the field to the database

Firstly you will want to add a new column to your database called "my_field". This will need to go on the right table. For the guide we will add it to the videos table called avcms_videos. The tables for other content types are named exactly as you'd expect with the prefix you decided at install (defaults to avcms_) followed by the name of the content.

You can add your new column to the database using PHPMyAdmin or similar software. The exact details of MySQL column creation are outside of the scope of this tutorial, it is assumed you know how to do this.

Adding the getter and setter

To allow AVCMS to write to the newly created column two methods need to be added to the content entity. Every content type in AVCMS is represented by an object. For example, the Video object can be found at:

src/AVCMS/Bundles/Videos/Model/Video.php

You will find the content model you are looking for in a similar location, for example:

Games: src/AVCMS/Bundles/Games/Model/Game.php Wallpapers: src/AVCMS/Bundles/Wallpapers/Model/Wallpaper.php

When tracking those files down you will see there is one file with the singular name (Video) and one file with the plural name (Videos). You want the file with the singular name.

In these files you will see many methods that look similar to this:

   public function setMyField($value)
   {
       $this->set("my_field", $value);
   }
   public function getMyField()
   {
       return $this->get("my_field");
   }

The above code shows you the two methods that need to be added to support the "my_field" column. You can modify the code to replace the instances of "my_field" and "MyField" with your field name.

Note how the method names are the same as the column name in the database but with the underscore removed. We also capitalise the separate words but this is optional as PHP methods are case-insensitive.