function test_relationships() { $record = new Dataface_Record('Profiles', array()); $record->setValue('id', 10); $record->setValue('fname', 'John'); $table =& $this->table1; $this->table1->relationships(); // loads the relationships // make sure that the relationships were loaded properly $rels = array('appointments', 'degrees', 'addresses'); foreach ($rels as $rel) { $this->assertTrue(is_a($table->_relationships[$rel], 'Dataface_Relationship'), "Relationship {$rel} not loaded"); } //make sure the sql was loaded properly $appointments =& $table->getRelationship('appointments'); $this->assertEquals($appointments->_schema['sql'], "select * from Appointments where Appointments.profileid='\$id'"); $this->assertEquals($record->parseString($appointments->_schema['sql']), "select * from Appointments where Appointments.profileid='10'"); $this->assertEquals(count($appointments->_schema['tables']), 1); $this->assertEquals($appointments->_schema['tables'][0], 'Appointments'); $this->assertEquals(count($appointments->_schema['selected_tables']), 1); $this->assertEquals($appointments->_schema['selected_tables'][0], 'Appointments'); $this->assertEquals(count($appointments->_schema['columns']), 6); //(profileid, position, startdate, enddate, salary) $this->assertEquals($appointments->_schema['columns'][0], 'Appointments.id'); $this->assertEquals($appointments->_schema['columns'][1], 'Appointments.profileid'); $this->assertEquals($appointments->_schema['columns'][2], 'Appointments.position'); $this->assertEquals($appointments->_schema['columns'][3], 'Appointments.startdate'); $this->assertEquals($appointments->_schema['columns'][4], 'Appointments.enddate'); $this->assertEquals($appointments->_schema['columns'][5], 'Appointments.salary'); $degrees =& $table->getRelationship('degrees'); $this->assertEquals($degrees->_schema['sql'], "select name, institution from Degrees where Degrees.profileid='\$id'"); $this->assertEquals($record->parseString($degrees->_schema['sql']), "select name, institution from Degrees where Degrees.profileid='10'"); $this->assertEquals(count($degrees->_schema['tables']), 1); $this->assertEquals($degrees->_schema['tables'][0], 'Degrees'); $this->assertEquals(count($degrees->_schema['columns']), 2); $this->assertEquals($degrees->_schema['columns'][0], 'Degrees.name'); $this->assertEquals($degrees->_schema['columns'][1], 'Degrees.institution'); $courses =& $table->getRelationship('courses'); $this->assertEquals($courses->_schema['selected_tables'][0], 'Courses'); $this->assertEquals(count($courses->_schema['columns']), 4); $this->assertEquals($courses->_schema['sql'], "select Courses.* from Courses, Student_Courses where Courses.id=Student_Courses.courseid and Student_Courses.studentid='\$id'"); // try default get related... should only return 3 records unset($degrees); $degrees = $record->getRelatedRecords('degrees'); $this->assertEquals($degrees[0]['name'], 'Master of Technology'); $this->assertEquals(count($degrees), 3, 'Degrees relationship returned wrong number of records. Should be 3.'); foreach ($degrees as $degree) { $this->assertTrue(isset($degree['name']), 'name is not set for ' . $degree); $this->assertTrue(isset($degree['institution']), 'institution is not set for' . $degree); } // Explicitly request multiple records $degrees = $record->getRelatedRecords('degrees', true); $this->assertEquals($degrees[0]['name'], 'Master of Technology'); $this->assertEquals(count($degrees), 3, 'Degrees relationship returned wrong number of records. Should be 3.'); foreach ($degrees as $degree) { $this->assertTrue(isset($degree['name']), 'name is not set for ' . $degree); $this->assertTrue(isset($degree['institution']), 'institution is not set for' . $degree); } // Explicitly request single record $degrees = $record->getRelatedRecords('degrees', false); $this->assertEquals($degrees['name'], 'Master of Technology'); $this->assertTrue(isset($degrees['name']), 'name is not set for ' . $degree); $this->assertTrue(isset($degrees['institution']), 'institution is not set for' . $degree); // Try using getValue() $this->assertEquals($record->getValue('degrees.name'), 'Master of Technology'); // Try multiple values //$names = $record->getValue('degrees.name', true); //$this->assertEquals(3, count($names), 'Wrong number of names returned for getValue(degrees.name)'); //$this->assertEquals($names[0], 'Master of Technology'); //$this->assertEquals($names[1], 'PH.D of Technology'); //$this->assertEquals($names[2], 'Bachelor of Science'); // // Now we use a relationship that was defined directly with SQL //(profileid,line1,line2,line3,city,state,country,postalcode) $addresses =& $this->table1->getRelationship('addresses'); $this->assertEquals($addresses->_schema['sql'], "select * from Addresses where profileid='\$id'"); $this->assertEquals($record->parseString($addresses->_schema['sql']), "select * from Addresses where profileid='10'"); $this->assertEquals(count($addresses->_schema['tables']), 1); $this->assertEquals(count($addresses->_schema['columns']), 9); $this->assertEquals($addresses->_schema['tables'][0], 'Addresses'); $this->assertEquals($addresses->_schema['columns'][0], 'Addresses.id'); $this->assertEquals($addresses->_schema['columns'][1], 'Addresses.profileid'); $this->assertEquals($addresses->_schema['columns'][2], 'Addresses.line1'); $this->assertEquals($addresses->_schema['columns'][3], 'Addresses.line2'); $line1 = $record->getValue('addresses.line1'); $this->assertEquals($line1, '555 Elm St'); }