/** * @return DOMDocument */ public function exportCollection(BaseCollection $collection) { $dom = new DOMDocument('1.0', 'utf-8'); $root = $dom->createElement('export'); $dom->appendChild($root); // $this->appendRecord($dom, $root, $record, NULL, true); $schema = $collection->getSchema(); $relations = $schema->getRelations(); // find foreign many-to-many schema foreach ($relations as $rel) { if ($rel['type'] === Relationship::MANY_TO_MANY) { $junctionRel = $relations[$rel['relation_junction']]; $junctionSchema = $junctionRel->newForeignSchema(); $foreignRel = $junctionSchema->getRelation($rel['relation_foreign']); $foreignCollection = $foreignRel->newForeignCollection(); $foreignSchema = $foreignRel->newForeignSchema(); $collectionRoot = $dom->createElement('collection'); $collectionRoot->setAttribute('schema', get_class($foreignSchema)); $collectionRoot->setAttribute('class', get_class($foreignCollection)); $root->appendChild($collectionRoot); foreach ($foreignCollection as $record) { $this->appendRecord($dom, $collectionRoot, $record, $foreignSchema, true); } } } $collectionRoot = $dom->createElement('collection'); $collectionRoot->setAttribute('schema', get_class($schema)); $collectionRoot->setAttribute('class', get_class($collection)); $root->appendChild($collectionRoot); foreach ($collection as $record) { $this->appendRecord($dom, $collectionRoot, $record, $schema, true); } $dom->formatOutput = true; return $dom; }
/** * Export collection object into CSV file. * * int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] ) */ public function exportCollection(BaseCollection $collection, array $intersectKeys = null) { $schema = $collection->getSchema(); $keys = $schema->getColumnNames(); if ($intersectKeys) { $keys = array_intersect_key($keys, array_flip($intersectKeys)); } $php54 = version_compare(phpversion(), '5.5.0') < 0; if ($php54) { fputcsv($this->fd, $keys, $this->delimiter, $this->enclosure); } else { fputcsv($this->fd, $keys, $this->delimiter, $this->enclosure, $this->escapeChar); } foreach ($collection as $record) { $array = $record->toArray(); $fields = []; foreach ($keys as $key) { $fields[] = $array[$key]; } if ($php54) { fputcsv($this->fd, $fields, $this->delimiter, $this->enclosure); } else { fputcsv($this->fd, $fields, $this->delimiter, $this->enclosure, $this->escapeChar); } } return true; }
public function __construct(BaseCollection $collection) { $this->collection = $collection; $this->schema = $collection->getSchema(); }