Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix Database Utility Class getXMLFromResult() #8276

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');