Created
December 24, 2015 16:05
-
-
Save cezar62882/6d534da5e2d3f7e86757 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Class Eloquent_base_model | |
| * | |
| * @property Illuminate\Database\Query\Builder $queryBuilder | |
| */ | |
| class Eloquent_base_model | |
| { | |
| protected $_table; | |
| protected $queryBuilder; | |
| public function __construct() | |
| { | |
| $this->queryBuilder = get_instance()->db->eloquent->getConnection()->query(); | |
| } | |
| public function select($columns = '*') | |
| { | |
| $this->queryBuilder->selectRaw($columns); | |
| return $this; | |
| } | |
| public function from($table) | |
| { | |
| $this->queryBuilder->from($table); | |
| return $this; | |
| } | |
| public function query($sql) | |
| { | |
| $this->queryBuilder->whereRaw($sql); | |
| return $this; | |
| } | |
| public function get($table) | |
| { | |
| $this->queryBuilder->from($table)->get(); | |
| return $this; | |
| } | |
| public function count_all_results() | |
| { | |
| $this->_resetBuilder(); | |
| $result = $this->queryBuilder->from($this->_table)->count(); | |
| $this->_resetBuilder(); | |
| return $result; | |
| } | |
| public function count() | |
| { | |
| $result = $this->count(); | |
| $this->_resetBuilder(); | |
| return $result; | |
| } | |
| public function result_array() | |
| { | |
| $collection = new \Illuminate\Support\Collection($this->queryBuilder->get()); | |
| $result = $collection->toArray(); | |
| $collection-> | |
| var_dump($collection->toArray()->all()); | |
| $this->_resetBuilder(); | |
| return $result; | |
| } | |
| public function result() | |
| { | |
| $result = $this->queryBuilder->get(); | |
| $this->_resetBuilder(); | |
| return $result; | |
| } | |
| public function where($attribute, $value = NULL) | |
| { | |
| if (is_array($attribute)) | |
| { | |
| foreach ($attribute as $key => $val) | |
| { | |
| $this->where($key, $val); | |
| } | |
| } | |
| preg_match("/ ?([!>=<]+)/", $attribute, $operators); | |
| if ( ! empty($operators)) | |
| { | |
| $operator = $operators[1]; | |
| $attribute = preg_replace("/ ?([!>=<]+)/", "", $attribute); | |
| } | |
| else | |
| { | |
| $operator = '='; | |
| } | |
| if ($value === NULL) | |
| { | |
| $this->queryBuilder->whereRaw($attribute); | |
| } | |
| else | |
| { | |
| $this->queryBuilder->where($attribute, $operator, $value); | |
| } | |
| return $this; | |
| } | |
| public function row() | |
| { | |
| $result = $this->queryBuilder->from($this->_table)->first(); | |
| $this->_resetBuilder(); | |
| return $result; | |
| } | |
| public function num_rows() | |
| { | |
| return $this->queryBuilder->count(); | |
| } | |
| public function where_in($attribute, $values) | |
| { | |
| $this->queryBuilder->whereIn($attribute, $values); | |
| return $this; | |
| } | |
| public function limit($limit, $offset = 0) | |
| { | |
| $this->queryBuilder->take($limit)->offset($offset); | |
| return $this; | |
| } | |
| public function order_by($name, $asc = 'ASC') | |
| { | |
| $this->queryBuilder->orderBy($name, $asc); | |
| return $this; | |
| } | |
| public function group_by($name) | |
| { | |
| $this->queryBuilder->groupBy($name); | |
| return $this; | |
| } | |
| public function __get($name) | |
| { | |
| if ($name === 'db') | |
| { | |
| return $this; | |
| } | |
| return get_instance()->$name; | |
| } | |
| public function join($table, $references, $type = NULL) | |
| { | |
| if ($references) | |
| { | |
| $references_explode = explode('=', $references); | |
| $one = trim($references_explode[0]); | |
| $operator = '='; | |
| $two = trim($references_explode[1]); | |
| } | |
| $this->queryBuilder->join($table, $one, $operator, $two, $type); | |
| return $this; | |
| } | |
| private function _resetBuilder() | |
| { | |
| $this->queryBuilder = $this->queryBuilder->newQuery(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment