Skip to content
staticall edited this page Sep 30, 2012 · 4 revisions

Get records with standart parameters from database

<?php
class App_Module_Example extends App_Module
{
  protected $_table = null;

  public function __construct()
  {
    $this->_table = new Table_Example();
  }

  public function getExamples()
  {
    return $this->getRecords();
  }
}

Get rowset from different table

<?php
class App_Module_Example extends App_Module
{
  protected $_table = null;
  protected $_table_bugs = null;

  public function __construct()
  {
    $this->_table = new Table_Example();
    $this->_table_bugs = new Table_Example_Bugs();
  }

  public function getExampleBugs()
  {
    return $this->getRowset(null, null, null, null, null, null, '_table_bugs');
  }
}

Get row with related tables

<?php
class App_Module_Example extends App_Module
{
  protected $_table = null;

  public function __construct()
  {
    $this->_table = new Table_Example();
  }

  public function getExample()
  {
    return $this->getRow();
  }

  public function joinRelatedTables($select, $select_fields = true, $var_table_name = self::DEFAULT_VAR_TABLE_NAME)
  {
    // prepare correct values for $select and $var_table_name
    parent::joinRelatedTables($select, $select_fields, $var_table_name);
    $var_table_name = $this->prefilterVarTableName($var_table_name);

    // if we going to join few tables, Zend will throw some exceptions, we don't want that
    $select->setIntegrityCheck(false);

    // variable initialization
    $test_fields = array();

    if($select_fields)
    {
      $test_fields = array(
        'test__id'   => 'test.id',
        'test__data' => 'test.data',
      );
    }

    // creating new instance of related table
    $tbl_related = new Table_Related();
    // condition to join tables
    $join_cond = 'test.id = '. self::PLACEHOLDER_TABLENAME .'.test_id';

    // replace placeholders, so instead of "self::PLACEHOLDER_TABLENAME .'.test_id'" we will get "'example.test_id'" or something similar
    $join_cond = $this->_replacePlaceholders($join_cond, $var_table_name);

    // join itself
    $select->joinLeft(
      array('test' => $tbl_related->getName()),
      $join_cond,
      $test_fields
    );

    // we must return $select object, or else we got null in result
    return $select;
  }
}

Get row from different table with related tables for different table

<?php
class App_Module_Example extends App_Module
{
  protected $_table = null;
  protected $_table_bugs = null;

  public function __construct()
  {
    $this->_table = new Table_Example();
    $this->_table_bugs = new Table_Example_Bugs();
  }

  public function getExample()
  {
    return $this->getRow(null, null, null, null, '_table_bugs');
  }

  public function joinRelatedTables($select, $select_fields = true, $var_table_name = self::DEFAULT_VAR_TABLE_NAME)
  {
    // prepare correct values for $select and $var_table_name
    parent::joinRelatedTables($select, $select_fields, $var_table_name);
    $var_table_name = $this->prefilterVarTableName($var_table_name);

    // if we going to join few tables, Zend will throw some exceptions, we don't want that
    $select->setIntegrityCheck(false);

    switch($var_table_name)
    {
      case '_table_bugs':
          // variable initialization
          $test2_fields = array();

          if($select_fields)
          {
            $test2_fields = array(
              'test2__id'   => 'test2.id',
              'test2__data' => 'test2.data',
            );
          }

          // creating new instance of related table
          $tbl_related = new Table_Related2();
          // condition to join tables
          $join_cond = 'test2.id = '. self::PLACEHOLDER_TABLENAME .'.test_id';

          // replace placeholders, so instead of "self::PLACEHOLDER_TABLENAME .'.test_id'" we will get "'example.test_id'" or something similar
          $join_cond = $this->_replacePlaceholders($join_cond, $var_table_name);

          // join itself
          $select->joinLeft(
            array('test2' => $tbl_related->getName()),
            $join_cond,
            $test_fields
          );

          // free memory from object
          unset($tbl_related);
        break;
      default:
          // variable initialization
          $test_fields = array();

          if($select_fields)
          {
            $test_fields = array(
              'test__id'   => 'test.id',
              'test__data' => 'test.data',
            );
          }

          // creating new instance of related table
          $tbl_related = new Table_Related();
          // condition to join tables
          $join_cond = 'test.id = '. self::PLACEHOLDER_TABLENAME .'.test_id';

          // replace placeholders, so instead of "self::PLACEHOLDER_TABLENAME .'.test_id'" we will get "'example.test_id'" or something similar
          $join_cond = $this->_replacePlaceholders($join_cond, $var_table_name);

          // join itself
          $select->joinLeft(
            array('test' => $tbl_related->getName()),
            $join_cond,
            $test_fields
          );

          // free memory from object
          unset($tbl_related);
        break;
    }


    // we must return $select object, or else we got null in result
    return $select;
  }
}

Filter results. In example below, calling getExamples() with $filter array as parameter will return already filtered result. Also, note, that you don't need to call parent::prepareFilter($select);, because prepareFilter() method in parent object don't do anything useful;

<?php
class App_Module_Example extends App_Module
{
  protected $_table = null;

  public function __construct()
  {
    $this->_table = new Table_Example();
  }

  public function prepareFilter($select, $filter = array())
  {
    $filter = $this->prefilterArray($filter);

    if(!empty($filter))
    {
      $rules = array(
        'multiple' => array(
          'status' => array(
            'column' => 'status',
            'parameters' => array(
              'validation' => Table_Example::getStatuses(),
            ),
          ),
          'type' => array(
            'parameters' => array(
              'validation' => Table_Example::getTypes(),
            ),
          ),
        ),
      );

      $conditions = parent::applyFilterRules($filter, $rules);
    }

    $conditions = $this->prefilterWhere($conditions);

    if(!empty($conditions))
    {
      $select = $this->applyWhere($select, $conditions);
    }

    if(isset($filter)) unset($filter);
    if(isset($conditions)) unset($conditions);

    return $select;
  }

  public function getExamples($filter = array())
  {
    $records = $this->getRecords(null, null, null, array(), $filter);

    return $records;
  }
}
Clone this wiki locally