Yes to the first question.
Quote
You say it matches towards the $_POST,but then the input fields need to have specific names to make this work I assume.
It needs to have the specific column names in the $_POST array but this is not difficult to manage. You need to call them something in the HTML. You can always make a class to manage HTML generation too.
The base functions for CRUD are:
create() - insert record into the database
retrieve_record() - gets one record by id from db
retrieve_all() - gets all records from db
delete() - removes a record from the db
Other functions for setting up the data_mapper for usage are:
set_query() - use a custom query for CRUD functions
set_input_data() - set the data array to use with CRUD functions
set_input_exceptions() - set the exceptions for CRUD queries (such as when you insert a row in the database you will need to set an exception for the primary key)
set_primary_key() - this can be an array of keys, used with update and select statements
There are of course other functions but these are private for the data_mapper to build the queries only.
You will definitely need to extend the class at this point for advanced features. Say you have a categories module that you want to use for news, pages and files (aka downloads). You will need the category_id, the record_id and the module_id. You would extend the data_mapper and create the necessary function to insert a new category record for each module. This would be as simple as setting up a custom query.
class Pages extend Data_Mapper
{
public function set_category_association($page_id, $category_id)
{
$this->set_query("INSERT INTO category_items (item_id, category_id, module_id) VALUES($page_id, $category_id, $this->module_id)");
return $this->create();
}
}
You don't even need to extend the class you can do all that in the PHP file itself.
It has taken me a while to build this class, it does a lot but does it do too much? Should I stick to the Active Record pattern or something? My concerns are mainly about scalability and performance.
Here is a snippet of the processing. It basically is working with a module called file types - nothing special. The database columns are file_type, file_type_name and file_type_description. This page is what happens after you fill in the fields and click submit.
require_once(PATH_CLASSES . 'data_mapper.class.php');
$module_name = 'file_types';
$module = new Data_Mapper($module_name);
//if ID is set load data and assign smarty variables, send error if no ID match
if(isset($_GET['id']) && is_numeric($_GET['id']))
{
$module->set_primary_key($_GET['id']);
}
if(isset($_REQUEST['option']))
{
$option = $_REQUEST['option'];
switch($option)
{
case 'add':
$module->set_input_data($_POST);
$exceptions = array('file_type_id');
$module->set_input_exceptions($exceptions);
$result = $module->create();
if($result === TRUE)
{
$status = 'The File type has been added.';
}
else
{
$status = 'Failed to create.';
}
break;
case 'edit':
$module->set_input_data($_POST);
$module->set_primary_key($_POST['file_type_id']);
$result = $module->update();
if($result === TRUE)
{
$status = 'Successfully edited.';
}
else
{
echo 'Failed to update.';
}
break;
case 'delete':
$result = $module->delete();
if($result === TRUE)
{
$status = 'Deleted.';
}
else
{
$status = 'Failed to delete.';
}
break;
}
}