Invenzzia »

Opt Format Doctrine

From Invenzzia wiki

Jump to: navigation, search
Opt_Format_Doctrine
Project Open Power Template 2
Type Data format
Author Tomasz Jędrzejewski
Website http://www.invenzzia.org/
License New BSD License
Status No information

This data format allows to use Doctrine_Query objects from Doctrine ORM as section datasources. The section will automatically execute the query and set the appropriate hydration mode, so that you do not have to worry about it. The data format does not implement the whole iteration process from scratch, but extends one of the standard data formats and changes the initialization procedure.

Source code

<?php
/**
* The Doctrine_Query format. The section automatically executes
* the Doctrine_Query provided as an argument and starts displaying
* the result data.
*
* @author Tomasz "Zyx" Jędrzejewski
* @copyright (c) Invenzzia Group 2009
*/

 
class Opt_Format_Doctrine extends Opt_Format_SingleArray
{
protected $_supports = array(
'section', 'item'
);
 
protected $_properties = array(
'section:useReference' => false,
'section:anyRequests' => null,
'section:itemAssign' => false,
'section:variableAssign' => false,
'item:assign' => false,
'item:useReference' => true,
);
 
protected $_vals;
 
/**
* Build a PHP code for the specified hook name.
*
* @param String $hookName The hook name
* @return String The output PHP code
*/

protected function _build($hookName)
{
if($hookName == 'section:isNotEmpty')
{
$section = $this->_getVar('section');
return '$_sect'.$section['name'].'_vals instanceof Doctrine_Query && is_array($_sect'.$section['name'].'_vals = $_sect'.$section['name'].'_vals->execute(array(), Doctrine::HYDRATE_ARRAY)) && ($_sect'.$section['name'].'_cnt = sizeof($_sect'.$section['name'].'_vals)) > 0';
}
return parent::_build($hookName);
} // end _build();
} // end Opt_Format_Doctrine;

Installation

Save the file as format.Doctrine.php in your plugin directory. The data format will be loaded automatically and registered under the Doctrine name. If you do not use plugins, save this file in /Opt/Format/Doctrine.php and register it manually:

$opt->register(Opt_Class::OPT_FORMAT, 'Doctrine');

Usage

Below, you can find a sample PHP code that uses this data format:

$view->query = Doctrine_Query::create()
->select('id, title')
->from('SomeTable');
$view->setFormat('query', 'Doctrine');

The returned elements are available as section variables in templates:

<ol>
<opt:section name="query">
<li>{$query.title}</li>
</opt:section>
</ol>

Note that you can use order="desc" attribute in the template. This does not add the ORDER BY clause to the executed query, so it can be safely used with the LIMIT statements without changing the result. The data from the joined tables are available as container subitems in the template, i.e. $sectionName.JoinedTable.column.

You are not allowed to modify the contents of Doctrine section elements and access the Doctrine_Query object directly from the template level. The data are hydrated with the HYDRATE_ARRAY attribute. If you are using auto accessor overriding feature, please note that you will not be able to access the overrided columns from the template level unless you create an extra event listener for the tables with the extra hydration event that will add the missing columns to the result set.

Personal tools