Export


The export library easily converts model data into common output formats like json, xls, or csv. This can be used for creating your own API or for providing file export mechanisms to site administrators.

csv()

Export data as CSV. The input data can be a model, a list (zajFetcher), a database query resultset, or a standard array.

Export model data as csv.

@param array|zajlib_db_session|zajlib_mssql_resultset|zajFetcher $fetcher A list of model objects, db results, or array elements which need to be exported.
@param array|bool $fields A list of fields from the model which should be included in the export.
@param string|File $file The name of the file which will be used during download or the File object if writing to a file.
@param boolean|string $encoding The value can be OFW_EXPORT_ENCODING_DEFAULT (utf8), OFW_EXPORT_ENCODING_EXCEL (Excel-compatible UTF-16LE), or any custom-defined encoding string.
@param bool|string $delimiter The separator for the CSV data. Defaults to comma, unless you set excel_encoding…then it defaults to semi-colon.
@param int $rowcount_resume Set this to the row count at which you wish to resume the export. This only makes sense if writing to file. Header row not included!
@return integer|boolean Will return false if error, the csv data if downloading, or the row count if writing to file.

If and how model fields are exported during this process is determined based on the field definition files’ export settings (system/plugins/_jquery/fields/*.field.php).

Export a list of objects:

$users = User::fetch();
return $this->zajlib->export->csv($users);

Export SQL results:

$results = $this->zajlib->db->query("SELECT * FROM example;");
return $this->zajlib->export->csv($results);

xls()

Export data in Microsoft Excel format (xlsx). The input data can be a model, a list (zajFetcher), a database query resultset, or a standard array.

Requires the php_zip, php_xmlreader, and php_simplexml extensions.

@param array|zajlib_db_session|zajlib_mssql_resultset|zajFetcher $fetcher A list of model objects, db results, or array elements which need to be exported.
@param array|bool $fields A list of fields from the model which should be included in the export.
@param string|File $file The name of the file which will be used during download or the File object if writing to a file.
@param int $rowcount_resume Set this to the row count at which you wish to resume the export. This only makes sense if writing to file. Header row not included!
@return integer|boolean Will return false if error, the xls data if downloading, or the row count if writing to file.

If and how model fields are exported during this process is determined based on the field definition files’ export settings (system/plugins/_jquery/fields/*.field.php).

Export a list of objects:

$users = User::fetch();
return $this->zajlib->export->xls($users);

Export SQL results:

$results = $this->zajlib->db->query("SELECT * FROM example;");
return $this->zajlib->export->xls($results);

json()

Export data as JSON. The input data can be a model, a list (zajFetcher), or a standard array.

@param zajModel|array|zajFetcher $input A zajFetcher list of zajModel objects which need to be exported. It can also be an array of objects (such as a zajDb query result) or a multi-dimensional array.
@param array|bool $fields A list of fields from the model which should be included in the export. NOT YET IMPLEMENTED!
@param string $file The name of the file which will be used during download. Defaults to export.json.
@return string|boolean Print the json or return it in test mode.

Export a list of objects

$users = User::fetch();
return $this->zajlib->export->json($users);
Outlast Web & Mobile Development (c) 2023 | Privacy Policy |