oop - php mvc - apply model's methods to result from db -


in many php frameworks can like:

class test extends model {     protected $first;     protected $second;      public function getallattributes() {         return $this->first.','.$this->second;     } } 

how in own solution? current model:

class model {      // ..      public function all() {         return mysqli_query($con, 'select * '.$this->table);     }  } 

usage:

$test = new test(); $result = $test->all(); foreach ($result $t) {     echo $t->getallattributes(); } 

i understand want create objects of current class mysql query, without handling query generation , object instantiation in each model again.

the pattern describe, model responsible load itself, called active record , made popular ruby on rails, adapted in php frameworks cakephp , laravel.

there no single solution how implement it, @ code of these frameworks understand how work.

the important part models must somehow describe themselves, i.e. attributes loaded database, data types, table used. can based on describe $table result in mysql, via "annotations" , reflection, or explicitly code.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -