Dataface Code Tip #1: Relationships the easy way

This is just a sample of how to deal with related records using the dataface library.

< ?
    require_once '/path/to/dataface/init.php';
    init(__FILE__, 'http://mydomain.com/path/to/dataface');
    require_once 'Dataface/Application.php';
    $app =& Dataface_Application::getInstance();

    require_once 'Dataface/Table.php';
    $studentsTable =& Dataface_Table::loadTable('Students');

    $studentsTable->addRelationship(
	    'courses', 		// the name of the relationship to add
	    array(
		    'Courses.CourseID' => 'StudentCourses.CourseID',
		    'StudentCourses.StudentID' => '$StudentID'
		    )
    );

    require_once 'dataface-public-api.php';

    $student =& df_get_record('Students', array('StudentID'=>1000));

    foreach ( $s tudent->getRelatedRecords('courses') as $course ){
	    echo $course['Name']."\n";
    }
    ?>

 This was pretty easy, especially since most of this code would be tucked away in a configuration file leaving you with 2 nice lines to get the student’s courses:


$student =& df_get_record('Students', array('StudentID'=>1000));
$courses = $student->getRelatedRecords('courses');

comments powered by Disqus