Data Management Made Easy Using Nennius: Advanced Data Handling - Creating Component Dependencies
(Page 5 of 8 )
Now that we have created an association between our Comments component and the previously created News component, a potential problem presents itself. What happens if a user deletes a News entry which contains one or more Comment entries (attached)?
Nennius offers a simple solution for this scenario as well. Simply update the following data found in the DB Field array in the News component file ('/components/news.php'):
# define primary key attribute for news 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
)
# setup dependency information to clean all associated comments as well upon deletion
'db_dependency_tables' => array(
array ( 'db_table_name' => 'comments',
'db_field_name' => 'news_id',
'db_display_name' => 'Comment' )
),
'db_dependency_flush' => TRUE
);
As you can see we've added two array keys to the 'id' descriptor array: 'db_dependency_tables' and 'db_depencency_flush'. The first, 'db_dependency_tables', contains an array of arrays - each array specifying information about a unique record dependency. Since only one dependency exists in our example application, only one array has been specified.
The second variable, 'db_depencency_flush', contains a boolean value that tells Nennius how to act in the event that a News record (with one or more Comments records attached) is deleted. In our case we specified 'TRUE', which means that all associated Comments will also be deleted. (Note: If we had specified 'FALSE' then Nennius would have refused to delete any News releases with one or more User Comments attached.)
Next: Component Notes >>
More MySQL Articles
More By Brian Vaughn