Navigation:  "The Jet Engine" PHP Application Framework > Programming Concepts > Jet Engine Objects >

iSQL Record Objects

Previous pageReturn to chapter overviewNext page

The "M" in the "MVC" are records and data from the database.

 

Where $iSQL and $oSQL are typically used to run queries that return results to you (as shown in the previous chapter), the iSQL system also has a "record" module that allows you to very easily work with a single row of data from a table, manipulate it, and then safely save any changes (or delete it), all without having to write a single SQL Statement.

 

The ds/X iSQL::Record module is a means of operating on a single piece of data, from a database table.  This is a simplistic means of finding a record, changing values, and saving it (or deleting it) from the database.  Record Objects will validate themselves based on SQL Schema information (defined in the administrative center: SQL Manager).  These objects do not understand or support referential integrity or joins.

 

The full iSQL::Record module documentation follows.

 


SYNOPSIS / SAMPLE

 

 

   $link = new Record(

      array(

         "Parent"=>$oSQL,

         "Table"=>'iweb_hyperseek_links',

         "Schema"=>'hyperseek_links_user',

         "Lookup"=>1,

         "id"=>$CGI['id']

       )

   );

 

 

DESCRIPTION

Implements, via the iSQL database wrapper, an easy and efficient means of working with individual database records.

 

CAVEATS

Make sure that you have already created an iSQL object, prior to using this module, or any of its functions.

 

FUNCTIONS

 

new

 Create a new Record Object

 Expects the following arguments to be sent:

 

 Parent  -> The parent iSQL object that you've already created

 Table   -> The table to do the lookup in.

 Lookup  -> 1 or 0 to indicate whether we're doing a lookup, or creating a blank record

 Schema  -> The table schema to use for data validations, and form generation

 columns/values

    You can send any number of "column_name='value'" strings along with the call

    to "new", this is how the lookups actually get done.

    In the main example above,  you see that we're looking for the record

    in the users table, where the name is 'fredsmith'

 

If the "Lookup" fails, or if you do not send the Lookup parameter the

 object will automatically place itself into insert mode, and a new database

 record will be created when you call the "save" method.

 

   $link = new Record(

      array(

         "Parent"=>$oSQL,

         "Table"=>'iweb_hyperseek_links',

         "Schema"=>'hyperseek_links_user',

         "Lookup"=>1,

         "id"=>$CGI['id']

       )

   );

 

 

update_values

  $record->update_values($CGI);

  If you have a hash or hashref of values corresponding to the record

  (for instance, from a web form), then a call to the "update_values"

  method will apply the values in the hash to the current record.

 

  Each value will be validated, based on the schema that you created

  the record with, and any failures will be stored in the debugging

  information.

 

 

save

 

  $record->save();

  Will write the record back to the database (or insert a new one);

  The record's field values will have already been validated by either

  the "update_values" method, or individually, as you made changes

  to them.

 

 

generate_form

  This method will generate an associative array of information that can be used

  to generate HTML Input form, based on the record data and the schema settings

  that you have defined within the database.  The hash values

  are "prompt", which is defined explicity in the schema definition,

  and "input" which is the HTML input tool, as specified in the definition.

 

  $formarray = $record->generate_form();

 

  $form = '';

  while ( list ($prompt, $fieldinput) = each ($formvalues) ) {

      $form .= "<tr><td>$prompt</td><td>$fieldinput</td></tr>\n";

  }

 

  echo "

    <form action='someprogram.php' method='post'>

    <table>

      $form

      <tr>

        <td colspan='2' align='center'>

          <input type='submit' value='Save'>

        </td>

      </tr>

    </table>

    </form>

  ";

 

 


An Example ...

 

 

function edit_account( $message = '' ) {

 

global $oSQL;

global $CGI;

 

$userid = $CGI['acct_id'];

 

$schema = "accounts_user";

 

$account = new Account( array( "Parent"=>$oSQL, "Table"=>"iweb_accounts", "Schema"=>$schema, "Lookup"=>1, "acct_id"=>$userid ) );

 

$formvalues = $account->generate_form();

 

while ( list ($prompt, $fieldinput) = each ($formvalues) ) {

    $form .= "<tr><td>$prompt</td><td>$fieldinput</td></tr>\n";

}

 

echo "

    <form action='accounts.php' method='post'>

        <table>

        $form

        <tr><td colspan=2 align='center'><input type='submit' value='Save Record'></td></tr>

        </table>

        <input type='hidden' name='action' value='save_account'>

    </form>

";

 

exit;

 

}

 

function save_account( ) {

 

global $language;

global $iSQL;

global $CGI;

 

$userid = $userid = $CGI['acct_id'];

$account = new Account( array( "Parent"=>$iSQL, "Table"=>"iweb_accounts", "Schema"=>"iweb_accounts", "Lookup"=>1, "acct_id"=>$userid ) );

 

// The $CGI associative array contains the fieldname=>values from the web form that was

// submitted.  Because that form was created by the above function wich used the accounts

// schema, we know that the field names from the form ($CGI) will match up with the

// accounts table ... so the update_values method can take $CGI in directly to set the

// new values from the form, in preparation to save the data.

 

if ( $account->update_values($CGI) ) {

    if ( $account->save() ) {

        echo "Record Saved";

    }

    else { edit_account($account->error_text); }

}

else { edit_account($account->error_text); }

}


This help file was created with an unregistered evaluation copy of Help & Manual. © EC Software. All rights reserved. This message will not appear if you compile this help file with the registered version of Help & Manual.