Skip to content

Commit

Permalink
Add Wildfire as CI_Model
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Jun 11, 2016
1 parent 3de4a60 commit 113c073
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Traits/DatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 16 additions & 12 deletions src/Traits/ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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')) {
Expand All @@ -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);
}
11 changes: 8 additions & 3 deletions src/Traits/ResultTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -94,6 +95,10 @@ protected function getQueryResult()
$result = $this->query->result();
}

if ($this->table) {
$this->get($this->table);
}

return $result;
}
}
11 changes: 7 additions & 4 deletions src/Wildfire.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +17,7 @@
* @package Wildfire
* @author Rougin Royce Gutib <[email protected]>
*/
class Wildfire
class Wildfire extends CI_Model
{
use DatabaseTrait, DescribeTrait, ObjectTrait, ResultTrait;

Expand All @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestApp/application/models/Comment.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Comment extends CI_Model {
class Comment extends Rougin\Wildfire\Wildfire {

public $table = 'comments';

Expand Down

0 comments on commit 113c073

Please sign in to comment.