MediaSearch_Model
[ class tree: MediaSearch_Model ] [ index: MediaSearch_Model ] [ all elements ]

Source for file AbstractMapper.php

Documentation is available at AbstractMapper.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     MediaSearch_Model
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * This model class represents a single file with its metadata.
  23.  *
  24.  * Its required that each MApper class follows a specific naming schema:
  25.  * Model class name for which it mapps plus the suffix 'Mapper'.
  26.  *
  27.  * Example:
  28.  * model class -> MediaSearch_Model_Foo
  29.  * mapper class -> MediaSearch_Model_FooMapper
  30.  *
  31.  * @see Zend_Application_Bootstrap_Bootstrap
  32.  *
  33.  * @category   MediaSearch
  34.  * @package    MediaSearch_Model
  35.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  36.  */
  37.     private $dbTable;
  38.  
  39.     /**
  40.      * Sets the database table gateway object.
  41.      * You can provide a class name as string or an object.
  42.      *
  43.      * The objects should be of the class returned by getTableClassName().
  44.      *
  45.      * @access public
  46.      * 
  47.      * @param mixed $dbTable 
  48.      * @return MediaSearch_Model_AbstractMapper 
  49.      */
  50.     public function setDbTable($dbTable{
  51.         if (is_string($dbTable)) {
  52.             $dbTable new $dbTable();
  53.         }
  54.  
  55.         $requiredType $this->getTableClassName();
  56.  
  57.         if (!$dbTable instanceof $requiredType{
  58.             throw new Exception('Invalid table data gateway provided');
  59.         }
  60.  
  61.         $this->dbTable $dbTable;
  62.  
  63.         return $this;
  64.     }
  65.  
  66.     /**
  67.      * Returns the atabase table gateway object.
  68.      * Constructs a new one if no one is set.
  69.      *
  70.      * @access public
  71.      * @return MediaSearch_Model_DbTable_Abstract 
  72.      */
  73.     public function getDbTable({
  74.         if (null === $this->dbTable{
  75.             $this->setDbTable($this->getTableClassName());
  76.         }
  77.  
  78.         return $this->dbTable;
  79.     }
  80.  
  81.     /**
  82.      * Constructs the required class name depending on the own classname.
  83.      * The schema is as followed. The beginning of each class name is
  84.      * MediaSearch_Model_DbTable_ after that the trailing part of mapper class
  85.      * name is appended to it without the trailing 'Mapper'.
  86.      *
  87.      * Example:
  88.      * mapper class -> MediaSearch_Model_FooMapper
  89.      * resulting db class -> MediaSearch_Model_DbTable_Foo
  90.      *
  91.      * @access private
  92.      * @return string 
  93.      */
  94.     private function getTableClassName({
  95.         $thisClassName  get_class($this);
  96.         $tableClassname substr($thisClassNamestrrpos($thisClassName'_'1);
  97.  
  98.         return 'MediaSearch_Model_DbTable_' str_replace('Mapper'''$tableClassname);
  99.     }
  100.  
  101.     /**
  102.      * Sets up a given model object with the data in the row array.
  103.      *
  104.      * @abstract
  105.      * @access protected
  106.      * @param array $row 
  107.      * @param MediaSearch_Model_FileMeta $fileMeta 
  108.      */
  109.     abstract protected function createModel($rowMediaSearch_Model_Abstract $model);
  110.  
  111.     /**
  112.      * Creates model objectes from multiple rows.
  113.      *
  114.      * @abstract
  115.      * @access protected
  116.      * @param array $resultSet 
  117.      * @return array 
  118.      */
  119.     abstract protected function createModelFromRowSet(array $resultSet);
  120.  
  121.     /**
  122. /**
  123.      * Returns the count of data rows in the db table.
  124.      * 
  125.      * @access public
  126.      * @return int 
  127.      */
  128.     public function count({
  129.         return $this->getDbTable()->count();
  130.     }
  131.  
  132.     /**
  133.      *
  134.      * @param <type> $limit 
  135.      * @param <type> $offset 
  136.      * @return <type> 
  137.      */
  138.     public function fetchAll($limit null$offset null{
  139.         $resultSet $this->getDbTable()->fetchAll(nullnull$limit$offset);
  140.  
  141.         if (== count($resultSet)) {
  142.             return;
  143.         }
  144.  
  145.         return $this->createModelFromRowSet($resultSet->toArray());
  146.     }
  147. }

Documentation generated on Mon, 17 Aug 2009 14:51:37 +0200 by phpDocumentor 1.4.2