Home arrow MySQL arrow Page 4 - Data Management Made Easy Using Nennius: Advanced Data Handling

Creating the User Comments Component - MySQL

Welcome to part three of this tutorial introducing application development for the Nennius engine. In this article, we will enhance the News Manager application from part two.

  1. Data Management Made Easy Using Nennius: Advanced Data Handling
  2. Advanced Application Configuration
  3. Expanding Our Application Menu
  4. Creating the User Comments Component
  5. Creating Component Dependencies
  6. Component Notes
  7. Help Files
  8. File Attachments
By: Brian Vaughn
Rating: starstarstarstarstar / 6
August 03, 2005

print this article



Now that we have finished configuring the application, let's create our basic Comments component. Just like last time, we'll start by creating the entry-point file. Name that file 'comments.php' and place it within the '/nennius/webapps/news/' directory. Then copy the following content into it:


# define location of component & descriptor files

$component_file        = 'components/comments.php';

$descriptor_file       = 'descriptors/comments.php';

# include parent nennius_component class

include_once "../../nennius.component.php";

include_once $component_file;

# create a new object to kick things off

new comments( $descriptor_file );


Next let's move on to the Comments descriptor file ('/descriptor/comments.php'):


# set   display name for component

$GLOBALS['g_main_display_name']               = 'User Comments';

# set primary db table name

$GLOBALS['g_db_primary_table']        = 'comments';

# set primary key for main db table

$GLOBALS['g_db_primary_table_key']    = 'id';

# set (optional) primary display field for db table

$GLOBALS['g_db_primary_table_name']   = 'subject';

# set required min. threshold for overall access

$GLOBALS['g_threshold_overall']               = $GLOBALS['USER'];


So far things should look pretty familiar, which is good. At their most basic level, all Nennius components will look very similar. The components file ('/components/comments.php') is no exception. Let's create it next:


# basic comments component

class comments extends nennius_component {

        # store class-wide reference to control object

        # NOTE: this object is created by the nennius_component class

        var $nennius_control                          = NULL;


        # sets up necessary internal vars, creates new control object

        # and passes self-reference to it


        function comments(     $p_component_descriptor_file   ) {


               # call out to constructor of parent class with descriptor file

               $this->nennius_component(      $p_component_descriptor_file );


        } # END comments



        # retrieves and returns assoc. array containing db column info


        function get_info_db_array() {

               $f_db_info_array       = NULL;


               # define primary key attribute for comments table

               $f_db_info_array['id'] = (

                       array (

                               'db_field_name'        => 'id',

                               'db_field_type'        => 'int',

                               'db_field_length'      => 3,

                               'db_primary_key'       => TRUE,


                               # field is hidden from visibility at all times

                               'hidden_all'           => TRUE




               # news id - links comment entries to parent news release entry

               $f_db_info_array['news_id'] = (

                       array (

                               'db_field_name'        => 'news_id',

                               'db_field_display'     => 'News Release',

                               'db_field_type'        => 'int',

                               'db_field_length'      => 36,

                               'db_field_required'    => TRUE,

                               # foreign key DB information

                               'db_foreign_key'       => 'id',

                               'db_foreign_display'   => 'title',

                               'db_foreign_table'     => 'news'




               # date & time when comment was posted

               $f_db_info_array['datetime'] = (

                       array (

                               'db_field_name'        => 'datetime',

                               'db_field_display'     => 'Date / Time',

                               'db_field_type'        => 'datetime',

                               'db_field_length'      => 19,

                               'db_field_required'    => TRUE,


                               # set default value to be current date & time

                               'update_default'       => date( 'Y-m-d H:i:s' ),


                               # set attribute to be searchable by min & max date range

                               'search_date_range'    => TRUE




               # commentor's name

               $f_db_info_array['name'] = (

                       array (

                               'db_field_name'        => 'name',

                               'db_field_display'     => 'Name',

                               'db_field_type'        => 'char',

                               'db_field_length'      => 50,


                               # set field to be searchable using a partial-match criteria

                               'input_search_type'    => 'TEXT',

                               'search_partial'       => TRUE,




               # comment's subject

               $f_db_info_array['subject'] = (

                       array (

                               'db_field_name'        => 'subject',

                               'db_field_display'     => 'Subject',

                               'db_field_type'        => 'char',

                               'db_field_length'      => 100,


                               # set field to be searchable using a partial-match criteria

                               'input_search_type'    => 'TEXT',

                               'search_partial'       => TRUE,




               # comment body / text

               $f_db_info_array['comment'] = (

                       array (

                               'db_field_name'        => 'comment',

                               'db_field_display'     => 'Comment',

                               'db_field_type'        => 'char',


                               # define size of textarea input

                               'input_update_type'    => 'TEXTAREA',

                               'input_update_rows'    => 10,

                               # hide field from search & view modes

                               'hidden_search'        => TRUE,

                               'hidden_view'          => TRUE




               # after all attributes have been setup, return array

               return $f_db_info_array;


        } # END get_info_db_array()



        # retrieves and returns assoc. array containing display headers


        function get_info_header_array() {   

               $f_header_info_array   = NULL;


               # append default header messages

               $f_header_info_array['news_id'] = 'News Information';

               $f_header_info_array['name']   = 'User Information';

               $f_header_info_array['subject']       = 'Comment';


               # return header array

               return $f_header_info_array;


        } # END get_info_header_array()

} # END comments class


Things should still look somewhat familiar. As you can see we've simply described the SQL table that stores our User Comments information, and set some basic configuration options about each of the component's attributes (aka SQL columns). However, a very important attribute was introduced with the 'news_id' column: the foreign key.

As previously stated, most data management systems will not only need to manage various data components but also control those components in relation to one another. Nennius allows for several methods of interaction between components, one of which is the foreign key attribute we have just identified. By specifying a foreign table, key, and display column we are providing Nennius with all of the information it needs to transparently link the Comments component to the News component. The result of this (in this example) is a drop-down menu containing all News entries, listed by title, that a user may pick from when creating or modifying a Comment record. This creates an association between Comments and News records.

We also introduced a new function in this component file: 'get_info_header_array()'. This function defines headers that are displayed above certain attributes when a record is being created or modified. This is a very useful tool when creating components with a high number of attributes. By using the 'get_info_header_array()' function a developer is easily able to categorize and organize the various form elements - making the resulting update form more user-friendly.

>>> More MySQL Articles          >>> More By Brian Vaughn

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: