예제 #1
0
파일: IOTest.php 프로젝트: Zunair/xataface
    function test_import_data()
    {
        $t =& $this->_table1;
        /*
         * For our first test we will try to import data directly into a table.
         * We do not worry about relationships here.
         */
        $data = '<?xml version="1.0"?>
			<dataface>
				<Profiles>
					<fname>John</fname>
					<lname>Smith</lname>
					<title>Professor</title>
				</Profiles>
				<Profiles>
					<fname>Julia</fname>
					<lname>Vaughn</lname>
					<title>Assistant</title>
				</Profiles>
			
			
			</dataface>';
        $io = new Dataface_IO('Profiles', $this->db);
        $record = null;
        // First we try to import the data into a temporary import table.
        $importTablename = $io->importData($record, $data);
        $res = xf_db_query("SELECT * FROM `{$importTablename}`", $this->db);
        $rows = array();
        while ($row = xf_db_fetch_array($res)) {
            $rows[] = $row;
        }
        $this->assertEquals(2, count($rows), "Incorrect number of rows in import table: '{$importTablename}'");
        $this->assertEquals('John', $rows[0]['fname']);
        $this->assertEquals('Smith', $rows[0]['lname']);
        $this->assertEquals('Professor', $rows[0]['title']);
        // now we try to commit the records
        $records = $io->importData($record, $importTablename, null, null, true);
        $this->assertEquals(2, count($records));
        $this->assertEquals('John Smith', $records[0]->val('fname') . ' ' . $records[0]->val('lname'));
        $this->assertEquals('Julia Vaughn', $records[1]->val('fname') . ' ' . $records[1]->val('lname'));
        $this->assertTrue($records[0]->val('id') > 0);
        $this->assertTrue($records[1]->val('id') > 0);
        //Now let's try to imort some records into a relationship
        /*
         * Now we attempt to import data into a one-to-many relationship
         */
        $data = '
			<dataface>
				<Appointments>
					<position>Trucker</position>
					<startdate>2003-11-12</startdate>
					<enddate>2004-05-06</enddate>
					<salary>1234.56</salary>
				</Appointments>
				<Appointments>
					<position>Director</position>
					<startdate>2002-01-02</startdate>
					<enddate>2005-02-03</enddate>
					<salary>5678.57</salary>
				</Appointments>
			</dataface>';
        $record = new Dataface_Record('Profiles', array());
        $io->read(array('id' => 10), $record);
        $importTablename = $io->importData($record, $data, 'xml', 'appointments');
        $res = xf_db_query("SELECT * FROM `{$importTablename}`", $this->db);
        if (!$res) {
            trigger_error("Error selecting records from import table '{$importTablename}'.  A mysql error occurred: " . xf_db_error($this->db) . "\n" . Dataface_Error::printStackTrace(), E_USER_ERROR);
        }
        $this->assertEquals(2, xf_db_num_rows($res));
        $rows = array();
        while ($row = xf_db_fetch_array($res)) {
            $rows[] = $row;
        }
        $this->assertEquals('Trucker', $rows[0]['position']);
        $this->assertEquals('Director', $rows[1]['position']);
        // now to commit this import
        $records = $io->importData($record, $importTablename, 'xml', 'appointments', true);
        if (PEAR::isError($records)) {
            trigger_error($records->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR);
        }
        $this->assertEquals(2, count($records));
        $this->assertEquals('dataface_relatedrecord', strtolower(get_class($records[0])));
        $this->assertEquals(10, $records[0]->val('profileid'));
        $this->assertEquals('Trucker', $records[0]->val('Appointments.position'));
        //print_r($records[0]->getValues());
        $res = xf_db_query("select * from `Appointments`", $this->db);
        $rows = array();
        while ($row = xf_db_fetch_array($res)) {
            $rows[] = $row;
        }
        $this->assertEquals(10, $rows[3]['profileid']);
        $this->assertEquals('Trucker', $rows[3]['position']);
        $this->assertEquals('Director', $rows[4]['position']);
        /*
         *
         * Finally we try to import data into a many-to-many relationship.
         *
         */
        $data = '
			<dataface>
				<Courses>
					<dept>Math</dept>
					<coursenumber>332</coursenumber>
				</Courses>
				<Courses>
					<dept>CMPT</dept>
					<coursenumber>475</coursenumber>
				</Courses>
			</dataface>';
        $importTablename = $io->importData($record, $data, 'xml', 'courses');
        if (PEAR::isError($importTablename)) {
            trigger_error($importTablename->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR);
        }
        $res = xf_db_query("SELECT * FROM `{$importTablename}`", $this->db);
        if (!$res) {
            trigger_error("Error selecting records from import table '{$importTablename}'.  A mysql error occurred: " . xf_db_error($this->db) . "\n" . Dataface_Error::printStackTrace(), E_USER_ERROR);
        }
        $this->assertEquals(2, xf_db_num_rows($res));
        $rows = array();
        while ($row = xf_db_fetch_array($res)) {
            $rows[] = $row;
        }
        $this->assertEquals('Math', $rows[0]['dept']);
        $this->assertEquals('CMPT', $rows[1]['dept']);
        $records = $io->importData($record, $importTablename, 'xml', 'courses', true);
        if (PEAR::isError($records)) {
            trigger_error($records->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR);
        }
        $this->assertEquals(2, count($records));
        foreach ($records as $rec) {
            $this->assertEquals('dataface_relatedrecord', strtolower(get_class($rec)));
        }
        //echo "Records: $records";
        $this->assertEquals('Math', $records[0]->val('dept'));
        $this->assertEquals('CMPT', $records[1]->val('dept'));
        $res = xf_db_query("SELECT * FROM Courses c inner join Student_Courses sc on c.id=sc.courseid inner join Profiles p on p.id=sc.studentid where p.id='10'", $this->db);
        if (!$res) {
            trigger_error(xf_db_error($this->db) . Dataface_Error::printStackTrace(), E_USER_ERROR);
        }
        $this->assertEquals(2, xf_db_num_rows($res));
        $course1 = xf_db_fetch_array($res);
        $course2 = xf_db_fetch_array($res);
        $this->assertEquals(10, $course1['studentid']);
        $this->assertTrue($course1['courseid'] > 0);
        $this->assertEquals(10, $course2['studentid']);
        $this->assertTrue($course2['courseid'] > 0);
        $this->assertEquals('Math', $course1['dept']);
        $this->assertEquals('CMPT', $course2['dept']);
        $this->assertEquals('John Smith', $course1['fname'] . ' ' . $course1['lname']);
    }
예제 #2
0
파일: ImportForm.php 프로젝트: promoso/HVAC
 function import($values)
 {
     if (intval($this->_step) === 1) {
         $upload =& $this->getElement('upload');
         if ($upload->isUploadedFile()) {
             /*
              * A file was uploaded.
              */
             $val =& $upload->getValue();
             $data = file_get_contents($val['tmp_name']);
         } else {
             /*
              * No file was uploaded so we will get data from the paste field.
              */
             $data = $values['content'];
         }
         $io = new Dataface_IO($this->_table->tablename);
         $relname = $this->_relationship === null ? null : $this->_relationship->getName();
         $importTablename = $io->importData($this->_record, $data, $values['filter'], $relname, false, @$values['__default_values__']);
         return $importTablename;
     } else {
         if ($this->_step == 2) {
             $io = new Dataface_IO($this->_table->tablename);
             $relname = $this->_relationship === null ? null : $this->_relationship->getName();
             $records = $io->importData($this->_record, $values['--importTablename'], @$values['filter'], $relname, true);
             return $records;
         }
     }
 }