/** * Returns true if the record currently represented in the Table already exists * in the database. * * @param tablename Alternative table where records may be stored. This is useful if we are reading form import or delete tables. * */ function recordExists(&$record, $keys = null, $tablename = null) { if (!is_a($record, "Dataface_Record")) { trigger_error(df_translate('scripts.Dataface.IO.recordExists.ERROR_PARAMETER_1', "In Dataface_IO::recordExists() the first argument is expected to be either a 'Dataface_Record' object or an array of key values, but received neither.\n<br>") . Dataface_Error::printStackTrace(), E_USER_ERROR); } if ($tablename === null and $this->_altTablename !== null) { $tablename = $this->_altTablename; } $tempRecordCreated = false; if ($record->snapshotExists()) { $tempRecord = new Dataface_Record($record->_table->tablename, $record->getSnapshot()); $tempRecordCreated = true; } else { $tempRecord =& $record; } if ($keys == null) { // Had to put in userialize(serialize(...)) because getValues() returns by reference // and we don't want to change actual values. $query = unserialize(serialize($tempRecord->getValues(array_keys($record->_table->keys())))); } else { $query = $keys; } $table_keys = array_keys($this->_table->keys()); foreach ($table_keys as $key) { if (!isset($query[$key]) or !$query[$key]) { //trigger_error("In Dataface_IO::recordExists()d attempt to determine if a record exists, not enough information was given. At least ALL of the keys of the table in question ('".$this->_table->tablename."') must be included in the provided Dataface_Record object (or query array), but some keys (notably '$key') are missing. Impossible to determine existence unless all key fields are provided.\n<br>".Dataface_Error::printStackTrace(), E_USER_ERROR); return false; } } foreach (array_keys($query) as $key) { $query[$key] = '=' . $this->_serializer->serialize($key, $tempRecord->getValue($key)); } $qb = new Dataface_QueryBuilder($this->_table->tablename, $query); $sql = $qb->select_num_rows(array(), $this->tablename($tablename)); $res = mysql_query($sql, $this->_table->db); // We just use regular mysql query to see if record exists because this should be sufficient //$res = $this->dbObj->query($sql, $this->_table->db, $this->lang); if (!$res || PEAR::isError($res)) { die("SQL error in {$sql} : " . mysql_error($this->_table->db) . Dataface_Error::printStackTrace()); } list($rows) = mysql_fetch_row($res); mysql_free_result($res); if ($rows > 1) { $err = PEAR::raiseError(Dataface_LanguageTool::translate('recordExists failure. Too many rows returned.', "Test for existence of record in recordExists() returned {$rows} records. \n\t\t\t\t\tIt should have max 1 record. \n\t\t\t\t\tThe query must be incorrect. \n\t\t\t\t\tThe query used was '{$sql}'. On line " . __LINE__ . " of file " . __FILE__, array('table' => $this->_table->tablename, 'line' => __LINE__, 'file' => __FILE__, 'sql' => $sql)), DATAFACE_E_IO_ERROR); trigger_error($err->toString() . "\n<br>" . Dataface_Error::printStackTrace()); } if ($tempRecordCreated) { $tempRecord->__destruct(); } return intval($rows) === 1; }
function test_getValues() { $record = new Dataface_Record('Profiles', array()); $vals = array('id' => 5, 'fname' => 'John', 'lname' => 'Smith', 'description' => 'This is a description', 'dob' => 'December 27, 1978', 'phone1' => '555-555-5555'); $record->setValues($vals); $ret = $record->getValues(); foreach ($vals as $key => $value) { $this->assertTrue(isset($ret[$key])); } $ret = $record->getValues(array('id', 'fname')); $this->assertTrue(count($ret) == 2); $this->assertTrue(isset($ret['id'])); $this->assertTrue(isset($ret['fname'])); }
/** * Returns true if the record currently represented in the Table already exists * in the database. * * @param tablename Alternative table where records may be stored. This is useful if we are reading form import or delete tables. * */ function recordExists(&$record, $keys = null, $tablename = null) { $this->lastVersionNumber = null; if (!is_a($record, "Dataface_Record")) { throw new Exception(df_translate('scripts.Dataface.IO.recordExists.ERROR_PARAMETER_1', "In Dataface_IO::recordExists() the first argument is expected to be either a 'Dataface_Record' object or an array of key values, but received neither.\n<br>"), E_USER_ERROR); } if ($tablename === null and $this->_altTablename !== null) { $tablename = $this->_altTablename; } $tempRecordCreated = false; if ($record->snapshotExists()) { $tempRecord = new Dataface_Record($record->_table->tablename, $record->getSnapshot()); $tempRecordCreated = true; } else { $tempRecord =& $record; } if ($keys == null) { // Had to put in userialize(serialize(...)) because getValues() returns by reference // and we don't want to change actual values. $query = unserialize(serialize($tempRecord->getValues(array_keys($record->_table->keys())))); } else { $query = $keys; } $table_keys = array_keys($this->_table->keys()); foreach ($table_keys as $key) { if (!isset($query[$key]) or !$query[$key]) { return false; } } foreach (array_keys($query) as $key) { //$query[$key] = '='.$this->_serializer->serialize($key, $tempRecord->getValue($key) ); $query[$key] = $this->_serializer->serialize($key, $tempRecord->getValue($key)); } if ($tempRecordCreated) { $tempRecord->__destruct(); } //$qb = new Dataface_QueryBuilder($this->_table->tablename, $query); //$sql = $qb->select_num_rows(array(), $this->tablename($tablename)); if ($record->table()->isVersioned()) { $versionField = "`" . $record->table()->getVersionField() . "`"; } else { $versionField = "NULL"; } $sql = "select `" . $table_keys[0] . "`, {$versionField} from `" . $this->tablename($tablename) . "` where "; $where = array(); foreach ($query as $key => $val) { $where[] = '`' . $key . "`='" . addslashes($val) . "'"; } $sql .= implode(' AND ', $where) . ' limit 1'; $res = df_q($sql, $this->_table->db); $num = xf_db_num_rows($res); $row = xf_db_fetch_row($res); @xf_db_free_result($res); if ($num === 1) { // We have the correct number... // let's check the version $this->lastVersionNumber = intval($row[1]); return true; } if ($num > 1) { $err = PEAR::raiseError(Dataface_LanguageTool::translate('recordExists failure. Too many rows returned.', "Test for existence of record in recordExists() returned {$rows} records. \n\t\t\t\t\tIt should have max 1 record. \n\t\t\t\t\tThe query must be incorrect. \n\t\t\t\t\tThe query used was '{$sql}'. ", array('table' => $this->_table->tablename, 'line' => 0, 'file' => '_', 'sql' => $sql)), DATAFACE_E_IO_ERROR); throw new Exception($err->toString(), E_USER_ERROR); } return false; }