diff --git a/CHANGELOG.md b/CHANGELOG.md index e6fac70..0ad2562 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `Wildfire` will be documented in this file. +## [Unreleased](https://github.com/rougin/wildfire/compare/v0.3.0...HEAD) + +### Added +- `Wildfire` as `CI_Model` + ## [0.3.0](https://github.com/rougin/wildfire/compare/v0.2.1...v0.3.0) - 2016-06-05 ### Added diff --git a/src/Traits/DatabaseTrait.php b/src/Traits/DatabaseTrait.php index ba1f4b6..48676e8 100644 --- a/src/Traits/DatabaseTrait.php +++ b/src/Traits/DatabaseTrait.php @@ -25,11 +25,16 @@ trait DatabaseTrait /** * Parses the table name from Describe class. * - * @param string $table + * @param string $table + * @param boolean $isForeignKey * @return string */ - protected function getTableName($table) + protected function getTableName($table, $isForeignKey = false) { + if ( ! $isForeignKey && $this->table) { + $table = $this->table; + } + $table = ucfirst(singular($table)); $array = explode('.', $table); diff --git a/src/Traits/ObjectTrait.php b/src/Traits/ObjectTrait.php index 213f3e8..254e066 100644 --- a/src/Traits/ObjectTrait.php +++ b/src/Traits/ObjectTrait.php @@ -23,13 +23,14 @@ trait ObjectTrait /** * Creates an object from the specified table and row. * - * @param string $table - * @param object $row + * @param string $table + * @param object $row + * @param boolean $isForeignKey * @return array */ - protected function createObject($table, $row) + protected function createObject($table, $row, $isForeignKey = false) { - list($model, $newTable) = $this->getModel($table); + list($model, $newTable) = $this->getModel($table, $isForeignKey); if ( ! array_key_exists($newTable, $this->tables)) { $tableInfo = $this->describe->getTable($newTable); @@ -62,9 +63,10 @@ protected function createObject($table, $row) * * @param string $table * @param array|integer $delimiters + * @param boolean $isForeignKey * @return object|boolean */ - abstract protected function find($table, $delimiters = []); + abstract protected function find($table, $delimiters = [], $isForeignKey = false); /** * Sets the foreign field of the column, if any. @@ -84,8 +86,8 @@ protected function setForeignField(CI_Model $model, Column $column) $foreignTable = $column->getReferencedTable(); $delimiters = [ $foreignColumn => $model->$key ]; - $foreignData = $this->find($foreignTable, $delimiters); - $newColumn = $this->getTableName($foreignTable); + $foreignData = $this->find($foreignTable, $delimiters, true); + $newColumn = $this->getTableName($foreignTable, true); $model->$newColumn = $foreignData; } @@ -94,15 +96,16 @@ protected function setForeignField(CI_Model $model, Column $column) * Gets the model class of the said table. * * @param string|null $table + * @param boolean $isForeignKey * @return array */ - protected function getModel($table = null) + protected function getModel($table = null, $isForeignKey = false) { - if ($table == null) { + if ($table == null && $this->table == null) { return [ null, '' ]; } - $newTable = $this->getTableName($table); + $newTable = $this->getTableName($table, $isForeignKey); $model = new $newTable; if (property_exists($model, 'table')) { @@ -115,8 +118,9 @@ protected function getModel($table = null) /** * Parses the table name from Describe class. * - * @param string $table + * @param string $table + * @param boolean $isForeignKey * @return string */ - abstract protected function getTableName($table); + abstract protected function getTableName($table, $isForeignKey = false); } diff --git a/src/Traits/ResultTrait.php b/src/Traits/ResultTrait.php index 80966d7..9098f3e 100644 --- a/src/Traits/ResultTrait.php +++ b/src/Traits/ResultTrait.php @@ -42,11 +42,12 @@ public function asDropdown($description = 'description') /** * Creates an object from the specified table and row. * - * @param string $table - * @param object $row + * @param string $table + * @param object $row + * @param boolean $isForeignKey * @return array */ - abstract protected function createObject($table, $row); + abstract protected function createObject($table, $row, $isForeignKey = false); /** * Returns all rows from the specified table. @@ -94,6 +95,10 @@ protected function getQueryResult() $result = $this->query->result(); } + if ($this->table) { + $this->get($this->table); + } + return $result; } } diff --git a/src/Wildfire.php b/src/Wildfire.php index 33366da..73d0f7e 100644 --- a/src/Wildfire.php +++ b/src/Wildfire.php @@ -2,6 +2,8 @@ namespace Rougin\Wildfire; +use CI_Model; + use Rougin\Wildfire\Traits\ObjectTrait; use Rougin\Wildfire\Traits\ResultTrait; use Rougin\Wildfire\Traits\DatabaseTrait; @@ -15,7 +17,7 @@ * @package Wildfire * @author Rougin Royce Gutib */ -class Wildfire +class Wildfire extends CI_Model { use DatabaseTrait, DescribeTrait, ObjectTrait, ResultTrait; @@ -37,11 +39,12 @@ public function __construct($database = null, $query = null) * * @param string $table * @param array|integer $delimiters + * @param boolean $isForeignKey * @return object|boolean */ - public function find($table, $delimiters = []) + public function find($table, $delimiters = [], $isForeignKey = false) { - list($model, $table) = $this->getModel($table); + list($model, $table) = $this->getModel($table, $isForeignKey); if ( ! is_array($delimiters)) { $primaryKey = $this->describe->getPrimaryKey($table); @@ -54,7 +57,7 @@ public function find($table, $delimiters = []) $query = $this->db->get($table); if ($query->num_rows() > 0) { - return $this->createObject($table, $query->row()); + return $this->createObject($table, $query->row(), $isForeignKey); } return false; diff --git a/tests/TestApp/application/models/Comment.php b/tests/TestApp/application/models/Comment.php index 5bd369e..8b7dae8 100644 --- a/tests/TestApp/application/models/Comment.php +++ b/tests/TestApp/application/models/Comment.php @@ -1,6 +1,6 @@