See {@link serialize()}.
_Note: This function will successfully load DataTables serialized by Piwik 1.X._
public addRowsFromSerializedArray ( string $serialized ) | ||
$serialized | string | A string with the format of a string in the array returned by {@link serialize()}. |
/** * Creates a new DataTable instance from a serialized DataTable string. * * See {@link getSerialized()} and {@link addRowsFromSerializedArray()} * for more information on DataTable serialization. * * @param string $data * @return \Piwik\DataTable */ public static function fromSerializedArray($data) { $result = new DataTable(); $result->addRowsFromSerializedArray($data); return $result; }
/** * General tests that tries to test the normal behaviour of DataTable * * We create some tables, add rows, some of the rows link to sub tables * * Then we serialize everything, and we check that the unserialize give the same object back */ public function testGeneral() { /* * create some fake tables to make sure that the serialized array of the first TABLE * does not take in consideration those tables */ $useless1 = $this->createDataTable(array(array(13))); /* * end fake tables */ /* * MAIN TABLE */ $table = new DataTable(); $subtable = new DataTable(); $idtable = $table->getId(); /* * create some fake tables to make sure that the serialized array of the first TABLE * does not take in consideration those tables * -> we check that the DataTable_Manager is not impacting DataTable */ $useless1->addRowFromArray(array(Row::COLUMNS => array(8487))); $useless3 = $this->createDataTable(array(array(8487))); /* * end fake tables */ $row = array(Row::COLUMNS => array(0 => 1554, 1 => 42, 2 => 657, 3 => 155744), Row::METADATA => array('logo' => 'test.png')); $row = new Row($row); $table->addRow($row); $table->addRowFromArray(array(Row::COLUMNS => array(0 => 1554, 1 => 42), Row::METADATA => array('url' => 'piwik.org'))); $table->addRowFromArray(array(Row::COLUMNS => array(0 => 787877888787), Row::METADATA => array('url' => 'OUPLA ADDED'), Row::DATATABLE_ASSOCIATED => $subtable)); /* * SUB TABLE */ $row = array(Row::COLUMNS => array(0 => 1554), Row::METADATA => array('searchengine' => 'google')); $subtable->addRowFromArray($row); $row = array(Row::COLUMNS => array(0 => 84894), Row::METADATA => array('searchengine' => 'yahoo')); $subtable->addRowFromArray($row); $row = array(Row::COLUMNS => array(0 => 4898978989), Row::METADATA => array('searchengine' => 'ask')); $subtable->addRowFromArray($row); /* * SUB SUB TABLE */ $subsubtable = new DataTable(); $subsubtable->addRowFromArray(array(Row::COLUMNS => array(245), Row::METADATA => array('yes' => 'subsubmetadata1'))); $subsubtable->addRowFromArray(array(Row::COLUMNS => array(13), Row::METADATA => array('yes' => 'subsubmetadata2'))); $row = array(Row::COLUMNS => array(0 => 666666666666666), Row::METADATA => array('url' => 'NEW ROW ADDED'), Row::DATATABLE_ASSOCIATED => $subsubtable); $subtable->addRowFromArray($row); $idsubsubtable = $subsubtable->getId(); $serialized = $table->getSerialized(); $this->assertEquals(array_keys($serialized), array(2, 1, 0)); // subtableIds are now consecutive // In the next test we compare an unserialized datatable with its original instance. // The unserialized datatable rows will have positive DATATABLE_ASSOCIATED ids. // Positive DATATABLE_ASSOCIATED ids mean that the associated sub-datatables are not loaded in memory. // In this case, this is NOT true: we know that the sub-datatable is loaded in memory. // HOWEVER, because of datatable id conflicts happening in the datatable manager, it is not yet // possible to know, after unserializing a datatable, if its sub-datatables are loaded in memory. $expectedTableRows = array(); $i = 0; foreach ($table->getRows() as $currentRow) { $expectedTableRow = clone $currentRow; $currentRowAssociatedDatatableId = $currentRow->subtableId; if ($currentRowAssociatedDatatableId != null) { $expectedTableRow->setNonLoadedSubtableId(++$i); // subtableIds are consecutive } $expectedTableRows[] = $expectedTableRow; } $tableAfter = new DataTable(); $tableAfter->addRowsFromSerializedArray($serialized[0]); $this->assertEquals($expectedTableRows, $tableAfter->getRows()); $subsubtableAfter = new DataTable(); $subsubtableAfter->addRowsFromSerializedArray($serialized[$consecutiveSubtableId = 2]); $this->assertEquals($subsubtable->getRows(), $subsubtableAfter->getRows()); $this->assertEquals($subsubtable->getRows(), DataTable::fromSerializedArray($serialized[$consecutiveSubtableId = 2])->getRows()); $this->assertTrue($subsubtable->getRowsCount() > 0); $this->assertEquals($table, Manager::getInstance()->getTable($idtable)); $this->assertEquals($subsubtable, Manager::getInstance()->getTable($idsubsubtable)); }