pdoTools — a set of quick snippets and a library


I want to present to your attention its own development to the rapid conclusion of the content on websites MODX Revolution.

As you know, this system is entirely built on its own ORM called xPDO. It is easier to work, allows one to write generic code for different databases, and much more.

Unfortunately, it cannot boast of speed of output (as probably any ORM), so I tried to combine its advantages with the usual PDO, and add best work with chunks and make a convenient library for MODX.

Main features:
the
    the
  • Quick work with the database. All requests are prepared on xPDO, and selects no objects on the PDO.
  • the
  • Pre-processing simple placeholders in chunks. The MODX parser understands only with complex challenges.
  • the
  • Code chunks may be specified in the call to the snippet, upload the usual way or from static files.
  • the
  • properly sorting, preparation, processing, and TV output settings.
  • the
  • Maintain detailed log snippet with timestamps, for debugging.
  • the
  • Easy loading of classes and a set of functions that can be applied in their developments.
  • the
  • included 8 generic snippets that give a good basis to the developer.

Start with the last point.

8 universal snippets


Originally pdoTools was invented as a library for other features, for example it uses Tickets.

Over time it became obvious that he allows with minimal costs to write a good set of tools can replace standard MODX snippets.

pdoResources
The snippet for fetching resources can replace getResources. It supports almost all of its features, and Bladet features:
— connect more tables using different JOIN
— you can specify what to select from the table columns
— flexible terms of sampling, up to the guidance of a pure SQL

When this snippet runs every 5 — 10 faster.

pdoMenu
Snippet to generate the site menu. Can replace Wayfinder, supports almost all of its parameters and chunks.

Works faster, especially when you first start with a cold cache.

pdoPage
Replacement for getPage. Generates much more correct pagination, and allows you to push incorrect queries in the query parameters limit and page.

pdoUsers
The snippet that displays the users of the site can filter by groups and roles.

pdoCrumbs
Fast generation breadcrumbs, BreadCrumb substitutes.

pdoNeighbors
The output of neighboring documents pages. That is: next, previous and parent. Very convenient to use to navigate the news.

pdoField
Snippet receiving any resource field, or its parent, including the TV option. Replaces getResourcesField and UltimateParent.
Able to display "default".

pdoSitemap
The Sitemap generation. Very fast replacement GoogleSiteMap, the difference is up to 12 times.



I specifically gave a brief description snippets in the beginning of the post, because most novice users this will be enough to quickly and comfortably work in MODX Revoltuion.

In fact, when replacing the standard snippets analogues of pdoTools, the average website starts to run faster to 2-3 times. You can look at the documentation page and read more to see the parameters and examples snippets here.

The beauty of my supplements in that it does not require any special manipulation. You simply install it from the repository and can be used. If something does not suit, buggy, doesn't work as you expect — old native snippets still with you, you can use them.

And now I will tell you by what pdoTools fast.

Sample from database


For each table in MODX there is a description in .map file. Out of the box the system supports 2 databases: MySQL and MSSQL, so all the queries in pdoTools generated using xPDO.
The beginning is always the same:
the
$q = $modx- > newQuery('modResource');
$q- > select(array('id', 'pagetitle', 'content'));
$q- > sortby('id', 'asc');
$q->limit(10);


But then we can choose or close
the
$resources = $modx- > getCollection('modResource', $q);
foreach ($resources as $resource) {
print_r($resource- > toArray());
}


Or arrays:
the
if ($q->prepare() && $q->stmt->execute()) {
$resources = $q->stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($resources as $resource) {
print_r($resource);
}
}


The classic way MODX Revolution, which is used in the components of its authors is a sample of using xPDO and further work with objects. It is convenient, of course — because in the model it is possible to register different conversions for field objects, and check.

But it is very slow, unfortunately. So I use the second way — sample using PDO. It is much faster.

good work with chunks


Chunks, if anyone knows — these are the elements in the tree MODX, which contain only HTML. They are necessary for the clearance of work of PHP scripts and separate logic from presentation in the system.

Here's a typical chunk:
the
<p>[[+idx]]. <a href="/[[+uri]]" > [[+pagetitle]]</a></p>


In the chunk may be special commands to the parser, for example:
the
<p>[[+idx]]. <a href="/[[+uri]]" > [[+menutitle:isempty=`[[+pagetitle]]`]]</a></p>


Each tag [[+key]] is an object in the MODX parser. It is possible to specify different parameters, filters, and generally, to program that way.

This is the same cool as slow.

Therefore, in pdoTools chunks previously treated — all that is possible, is replaced by a banal str_replace() from poluchennyh data, then replaced with links to [[~[[+id]]]]then the placeholders of lexicons [[%key]], in here all that remains (10% of the original work) is sent to the MODX parser.

The end result is no different, but the speed at times higher than the standard.

To use this is very simple:
the
$pdo = $modx- > getService('pdoTools');

return $pdo- > getChunk('Kazanka', array('array with the values to lookup'));


In otlichie from the original modX::getChunk () in pdoTools::getChunk() we can use already prepared the chunks, but just specify them using binding INLINE:
the
$pdo = $modx- > getService('pdoTools');
$tpl = '@INLINE <p>[[+idx]]. <a href="/[[+uri]]" > [[+pagetitle]]</a></p>';
$array = array(
'idx' => 1,
'pagetitle' = > 'page Name',
'url' => '/page.html'
);

return $pdo->getChunk($tpl, $array);


TV


pdoTools snippets and try to put all the work in 1 query to the database. So all these TV options are joined using a LEFT JOIN.

It becomes very convenient to filter on TV, because you can just specify:
the
[[!pdoResources?
&parents=`2`
&includeTVs=`myTV`
&where=`{"myTV:>":10}`
]]


And you get all resources from a container with id = 2 and value TV myTV more than 10. You can specify very complex conditions and samples, so I made a logging system:

Log library


Almost all snippets to understand the &shoLog=`1` and output the Manager of a puttee:

It is very convenient to build the conditions and to debug queries to the database.

using the library


The library is connected via modX::getService() like so:
the
// If we only need the basic functions
$pdo = $modx- > getService('pdoTools');
// If we need to work with the database
$pdo = $modx- > getService('pdoFetch');


The first allows you to work quickly with chunks:
the
$pdo->getChunk();
$pdo- > parseChunk();


And the second can quickly choose resources:
the
$pdo->getObject('modResource', 1);
$pdo->getCollection('modTemplate', array('id:>=' => 2));


If they combine, it will look like your entire site:
the
<?php
// Podklyuchaem class
$pdo = $modx- > getService('pdoFetch');
// Define the template
$tpl = '@INLINE <p><a href="[[+id]]" > [[+pagetitle]]</a></p>';
// Select all the resources on the website
$resources = $pdo->getCollection('modResource');

$output = ";
foreach ($resources as $resource) {

$output .= $pdo->getChunk($tpl, $resource);
}
// And add log
$output .= '<pre>'.$pdo->getTime().'</pre>';

return $output;


How do you output 2012 pages in half a second?

If you use modX::getChunk(), it will be 4 seconds instead of 0.5. This is the only difference on the processing of simple chunks, without conditions.

Optionally, you can enable and test rights, putting them in the &checkPermissions. It would slow down, but will still be faster than standard methods of MODX.

Opinion


This is a very brief information about the capabilities of pdoTools.

Component has been under development for almost a year and has the widest opportunities. Frankly, it is hard to describe all that he knows, but I'm trying.

Link documentation.
Link repository.
Recent versions of in the repository Simple Dream.
Stable version official repository.
To test the software without problems on modx-test.com.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Monitoring PostgreSQL + php-fpm + nginx + disk using Zabbix

Templates ESKD and GOST 7.32 for Lyx 1.6.x

Customize your Google