Skip to content

Commit

Permalink
Merge pull request #318 from j4mie/develop
Browse files Browse the repository at this point in the history
Document the raw_execute() method
  • Loading branch information
treffynnon authored Mar 21, 2017
2 parents 00b5fcd + 3fdc455 commit f2f170c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ php:
- 5.4
- 5.6
- 7.0
- 7.1
- hhvm
script: "phpunit --colors --coverage-text"
install: "composer install"
script: "composer run-script test -- --colors --coverage-text"
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ foreach ($tweets as $tweet) {

Changelog
---------
#### 1.5.3 - released 2017-03-21

* Document the `raw_execute()` method and add a note for `get_db()` in the querying documentation [[treffynnon](https://github.com/treffynnon)]

#### 1.5.2 - released 2016-12-14

* Fix autoincremented compound keys inserts [[lrlopez](https://github.com/lrlopez)] - [issue #233](https://github.com/j4mie/idiorm/issues/233) and [pull #235](https://github.com/j4mie/idiorm/pull/235)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
}
],
"scripts": {
"test": "vendor/bin/phpunit"
"test": "phpunit"
},
"require-dev": {
"phpunit/phpunit": "^5.6"
"phpunit/phpunit": "^4.8"
},
"license": [
"BSD-2-Clause",
Expand Down
100 changes: 94 additions & 6 deletions docs/querying.rst
Original file line number Diff line number Diff line change
Expand Up @@ -798,9 +798,97 @@ to stop you from specifying a completely different table in the query.
This is because if you wish to later called ``save``, the ORM will need
to know which table to update.

Note that using ``raw_query`` is advanced and possibly dangerous, and
Idiorm does not make any attempt to protect you from making errors when
using this method. If you find yourself calling ``raw_query`` often, you
may have misunderstood the purpose of using an ORM, or your application
may be too complex for Idiorm. Consider using a more full-featured
database abstraction system.
.. note::

Using ``raw_query`` is advanced and possibly dangerous, and
Idiorm does not make any attempt to protect you from making errors when
using this method. If you find yourself calling ``raw_query`` often, you
may have misunderstood the purpose of using an ORM, or your application
may be too complex for Idiorm. Consider using a more full-featured
database abstraction system.

Raw SQL execution using PDO
'''''''''''''''''''''''''''

.. warning::

By using this function you're dropping down to PHPs PDO directly. Idiorm
does not make any attempt to protect you from making errors when using this
method.

You're essentially just using Idiorm to manage the connection and configuration
when you implement ``raw_execute()``.

It can be handy, in some instances, to make use of the PDO instance underneath
Idiorm to make advanced queries. These can be things like dropping a table from
the database that Idiorm doesn't support and will not support in the future. These
are operations that fall outside the 80/20 philosophy of Idiorm. That said there is
a lot of interest in this function and quite a lot of support requests related to
it.

This method directly maps to `PDOStatement::execute()`_ underneath so please
familiarise yourself with it's documentation.

Dropping tables
~~~~~~~~~~~~~~~

This can be done very simply using ``raw_execute()``.

.. code-block:: php
<?php
if (ORM::raw_execute('DROP TABLE my_table')) {
echo "Table dropped";
} else {
echo "Drop query failed";
}
Selecting rows
~~~~~~~~~~~~~~

.. warning::

You really, should not be doing this, use Idiorm with ``raw_query`()` instead
where possible.
Here is a simple query implemented using ``raw_execute()`` - note the call to
``ORM::get_last_statement()`` as ``raw_execute()`` returns a boolean as per the
`PDOStatement::execute()`_ underneath.

.. code-block:: php
$res = ORM::raw_execute('SHOW TABLES');
$statement = ORM::get_last_statement();
$rows = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
}
It is also worth noting that ``$statement`` is a ``PDOStatement`` instance so calling
its ``fetch()`` method is the same as if you had called against PDO without Idiorm.

Getting the PDO instance
''''''''''''''''''''''''

.. warning::

By using this function you're dropping down to PHPs PDO directly. Idiorm
does not make any attempt to protect you from making errors when using this
method.

You're essentially just using Idiorm to manage the connection and configuration
when you implement against ``get_db()``.

If none of the preceeding methods suit your purposes then you can also get direct
access to the PDO instance underneath Idiorm using ``ORM::get_db()``. This will
return a configured instance of `PDO`_.

.. code-block:: php
$pdo = ORM::get_db();
foreach($pdo->query('SHOW TABLES') as $row) {
var_dump($row);
}
.. _PDOStatement::execute(): https://secure.php.net/manual/en/pdostatement.execute.php
.. _PDO: https://secure.php.net/manual/en/class.pdo.php
2 changes: 1 addition & 1 deletion docs/transactions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ it’s very easy to use PDO’s built-in methods:
For more details, see `the PDO documentation on Transactions`_.

.. _the PDO documentation on Transactions: http://www.php.net/manual/en/pdo.transactions.php
.. _the PDO documentation on Transactions: https://secure.php.net/manual/en/pdo.transactions.php

0 comments on commit f2f170c

Please sign in to comment.