/** * @brief Gets a list of fields that are unconstrained (i.e. can be * edited. This is helpful when building forms for this record. * * @returns array($fieldname:string) * @since 2.0 */ function getUnconstrainedFields($sql = null) { //$fkCols = $this->getForeignKeyValues($sql); $tmp = new Dataface_RelatedRecord($this->_record, $this->_relationshipName, array()); $fkCols = $tmp->getForeignKeyValues($sql); if (PEAR::isError($fkCols)) { throw new Exception($fkCols->getMessage(), $fkCols->getCode()); } $unconstrainedFields = array(); $cols = $this->_relationship->fields(); foreach ($cols as $col) { $field = $this->_relationship->getField($col); //print_r($field); $tablename = $field['tablename']; $fieldname = $field['name']; //echo $absFieldname; if (array_key_exists($tablename, $fkCols) and array_key_exists($fieldname, $fkCols[$tablename])) { // This column is already specified by the foreign key relationship so we don't need to pass // this information using the form. // Actually - this isn't entirely true. If there is no auto-incrementing field // associated with this foreign key, then if ($this->_relationship->isNullForeignKey($fkCols[$tablename][$fieldname])) { $furthestField = $fkCols[$tablename][$fieldname]->getFurthestField(); if ($furthestField != $absFieldname) { // We only display this field if it is the furthest field of the key continue; } } else { continue; } } if (@$field['grafted'] && !@$field['transient']) { continue; } $unconstrainedFields[] = $col; } return $unconstrainedFields; }
function testRelatedRecord() { $fragrance = df_get_record('fragrances', array('fragrance_id' => '=1')); $relatedRecord = new Dataface_RelatedRecord($fragrance, 'formulas'); $this->assertEquals($fragrance, $relatedRecord->getParent(), 'getParent() returned wrong record.'); $relatedRecord->setValue('formula_name', 'Test'); $fkvals = $relatedRecord->getForeignKeyValues(); $this->assertEquals(array('formulas' => array('fragrance_id' => '1', 'formula_name' => 'Test')), $fkvals, 'getForeignKeyValues() returned wrong structure.'); $relatedRecord->setValue('ingredients', array(0 => array('ingredient_id' => 3, 'concentration' => 10, 'concentration_units' => 1, 'amount' => 10, 'amount_units' => 2, '__id__' => 'new', '__order__' => 0), '__loaded__' => 1)); $fkvals = $relatedRecord->getForeignKeyValues(); $this->assertEquals(array('formulas' => array('fragrance_id' => '1', 'formula_name' => 'Test')), $fkvals, 'getForeignKeyValues() returned wrong structure.'); $formulaRecord = $relatedRecord->toRecord('formulas'); $this->assertTrue($formulaRecord instanceof Dataface_Record, 'toRecord() is expected to return a Dataface_Record object.'); $this->assertEquals('Test', $formulaRecord->val('formula_name'), 'toRecord() failed to transfer basic value to resulting Dataface_Record object.'); $this->assertEquals(array(0 => array('ingredient_id' => 3, 'concentration' => 10, 'concentration_units' => 1, 'amount' => 10, 'amount_units' => 2, '__id__' => 'new', '__order__' => 0), '__loaded__' => 1), $formulaRecord->val('ingredients'), 'toRecord() failed to transfer value of transient field to resulting Dataface_Record object.'); $records = $relatedRecord->toRecords(); $this->assertEquals(1, count($records), 'toRecords() returned wrong number of records.'); $this->assertTrue($records[0] instanceof Dataface_Record, 'toRecords should return an array of Dataface_Record objects.'); }