Rev 29 | Rev 35 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 29 | Rev 34 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | <?php
 | 
            1 | <?php
 | 
          
| 2 | 2 | ||
| - | 3 | require_once __DIR__ . '/../Application.php';  | 
          |
| 3 | require_once __DIR__ . '/../AbstractModel.php';  | 
            4 | require_once __DIR__ . '/../AbstractModel.php';  | 
          
| 4 | 5 | ||
| 5 | /**
 | 
            6 | /**
 | 
          
| 6 |  * Generic database table model class
 | 
            7 |  * Generic database table model class
 | 
          
| 7 |  *
 | 
            8 |  *
 | 
          
| 8 |  * @author Thomas Lahn
 | 
            9 |  * @author Thomas Lahn
 | 
          
| 9 |  * @property-read int $lastInsertId
 | 
            10 |  * @property-read int $lastInsertId
 | 
          
| 10 |  *   ID of the last inserted row, or the last value from
 | 
            11 |  *   ID of the last inserted row, or the last value from
 | 
          
| 11 |      a sequence object, depending on the underlying driver.
 | 
            12 |      a sequence object, depending on the underlying driver.
 | 
          
| 12 |  */
 | 
            13 |  */
 | 
          
| 13 | abstract class Table extends AbstractModel  | 
            14 | class Table extends AbstractModel  | 
          
| 14 | {
 | 
            15 | {
 | 
          
| 15 |   /**
 | 
            16 |   /**
 | 
          
| 16 |    * Name of the table
 | 
            17 |    * Name of the table
 | 
          
| 17 |    */
 | 
            18 |    */
 | 
          
| 18 | protected $_name = '';  | 
            19 | protected $_name = '';  | 
          
| Line 23... | Line 24... | ||
| 23 |    */
 | 
            24 |    */
 | 
          
| 24 | protected $_database;  | 
            25 | protected $_database;  | 
          
| 25 | 26 | ||
| 26 | protected $_id = 'id';  | 
            27 | protected $_id = 'id';  | 
          
| 27 | 28 | ||
| - | 29 |   /**
 | 
          |
| - | 30 |    * Creates a new <code>Table</code> instance.
 | 
          |
| - | 31 |    *
 | 
          |
| - | 32 |    * Each of the parameters is optional and can also be given
 | 
          |
| - | 33 |    * by a protected property where the parameter name is preceded
 | 
          |
| - | 34 |    * by <code>_</code>.  Parameter values overwrite the default
 | 
          |
| - | 35 |    * property values.  It is recommended to use default property
 | 
          |
| - | 36 |    * values of inheriting classes except for small applications
 | 
          |
| 28 | public function __construct()  | 
            37 |    * and testing purposes.
 | 
          
| - | 38 |    *
 | 
          |
| - | 39 |    * @param Database $database
 | 
          |
| - | 40 |    *   Database of the table (required in order to use a fitting
 | 
          |
| - | 41 |    *   query language)
 | 
          |
| - | 42 |    * @param string $name
 | 
          |
| - | 43 |    *   Table name
 | 
          |
| - | 44 |    * @param string $id
 | 
          |
| - | 45 |    *   Name of the primary key column
 | 
          |
| - | 46 |    * @throws InvalidArgumentException
 | 
          |
| - | 47 |    */
 | 
          |
| - | 48 | public function __construct(Database $database = null, $name = '', $id = '')  | 
          |
| 29 |   {
 | 
            49 |   {
 | 
          
| - | 50 | if ($database === null)  | 
          |
| - | 51 |     {
 | 
          |
| 30 | $this->_database = Application::getInstance()->getDefaultDatabase();  | 
            52 | $this->_database = Application::getInstance()->getDefaultDatabase();  | 
          
| - | 53 |     }
 | 
          |
| - | 54 |     else
 | 
          |
| - | 55 |     {
 | 
          |
| - | 56 | $this->_database = $database;  | 
          |
| - | 57 |     }
 | 
          |
| - | 58 | ||
| - | 59 | if ($name !== '')  | 
          |
| - | 60 |     {
 | 
          |
| - | 61 | $this->_name = $name;  | 
          |
| - | 62 |     }
 | 
          |
| - | 63 | ||
| - | 64 | if (!$this->_name)  | 
          |
| - | 65 |     {
 | 
          |
| - | 66 | throw new InvalidArgumentException('a table name is required');  | 
          |
| - | 67 |     }
 | 
          |
| - | 68 | ||
| - | 69 | if ($id !== '')  | 
          |
| - | 70 |     {
 | 
          |
| - | 71 | $this->_id = $id;  | 
          |
| - | 72 |     }
 | 
          |
| 31 |   }
 | 
            73 |   }
 | 
          
| 32 | 74 | ||
| 33 |   /**
 | 
            75 |   /**
 | 
          
| 34 |    * Returns the database for the table
 | 
            76 |    * Returns the database for the table
 | 
          
| 35 |    * @return Database
 | 
            77 |    * @return Database
 |