Skip to content

Commit

Permalink
Merge pull request #8276 from kenjis/fix-docs-db-utils
Browse files Browse the repository at this point in the history
docs: fix Database Utility Class `getXMLFromResult()`
  • Loading branch information
kenjis authored Nov 30, 2023
2 parents 667cfd2 + b555ce2 commit c5cb69e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
44 changes: 34 additions & 10 deletions user_guide_src/source/database/utilities.rst
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
#########
Utilities
#########
######################
Database Utility Class
######################

The Database Utility Class contains methods that help you manage your database.

.. contents::
:local:
:depth: 2

*******************
Get XML from Result
*******************
******************************
Initializing the Utility Class
******************************

getXMLFromResult()
==================
Load the Utility Class as follows:

This method returns the xml result from database result. You can do like this:
.. literalinclude:: utilities/002.php
:lines: 2-

You can also pass another database group to the DB Utility loader, in case
the database you want to manage isn't the default one:

.. literalinclude:: utilities/003.php
:lines: 2-

In the above example, we're passing a database group name as the first
parameter.

****************************
Using the Database Utilities
****************************

Export a Query Result as an XML Document
========================================

Permits you to generate an XML file from a query result. The first
parameter expects a query result object, the second may contain an
optional array of config parameters. Example:

.. literalinclude:: utilities/001.php

and it will get the following xml result::
and it will get the following xml result when the ``mytable`` has columns ``id`` and ``name``::

<root>
<element>
<id>1</id>
<name>bar</name>
</element>
</root>

.. important:: This method will NOT write the XML file for you. It
simply creates the XML layout. If you need to write the file
use the :php:func:`write_file()` helper.
18 changes: 10 additions & 8 deletions user_guide_src/source/database/utilities/001.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

class MyModel extends \CodeIgniter\Model
{
protected $table = 'foo';
protected $primaryKey = 'id';
}
$db = db_connect();
$dbutil = \Config\Database::utils();

$model = new MyModel();
$query = $db->query('SELECT * FROM mytable');

$util = \CodeIgniter\Database\Config::utils();
$config = [
'root' => 'root',
'element' => 'element',
'newline' => "\n",
'tab' => "\t",
];

echo $util->getXMLFromResult($model->get());
echo $dbutil->getXMLFromResult($query, $config);
3 changes: 3 additions & 0 deletions user_guide_src/source/database/utilities/002.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$dbutil = \Config\Database::utils();
3 changes: 3 additions & 0 deletions user_guide_src/source/database/utilities/003.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$dbutil = \Config\Database::utils('group_name');

0 comments on commit c5cb69e

Please sign in to comment.