Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Record{
  5.  
  6.     public $data = array();
  7.     private $entity;
  8.     private $field = 'id';
  9.     private $load = false;
  10.  
  11.     public function  __construct($value = null) {
  12.  
  13.         if($value){
  14.             $this->load($value);
  15.         }
  16.  
  17.     }
  18.  
  19.     public function  __set($name,  $value) {
  20.         $this->data[$name] = $value;
  21.     }
  22.  
  23.     public function  __get($name) {
  24.         return $this->data[$name];
  25.     }
  26.  
  27.     protected function setEntity($entity){
  28.         $this->entity = $entity;
  29.     }
  30.  
  31.     public function setField($field){
  32.         $this->field = $field;
  33.     }
  34.  
  35.     public function load($value){
  36.  
  37.             if($value):
  38.  
  39.                 $query = "SELECT * FROM {$this->entity} WHERE {$this->field} = '{$value}'";
  40.                 $result = mysql_query($query);
  41.  
  42.                 if(mysql_num_rows($result) > 0):
  43.                    
  44.                     $data =  mysql_fetch_object($result);
  45.  
  46.                     foreach($data  as $key=>$value):
  47.                         $this->data[$key] = $value;
  48.                     endforeach;
  49.  
  50.  
  51.                     $this->load = true;
  52.                     return true;
  53.                    
  54.                 endif;
  55.  
  56.             endif;
  57.  
  58.             return false;
  59.     }
  60.  
  61.  
  62.     public function store(){
  63.  
  64.         if($this->load):
  65.  
  66.                 $updates = '';
  67.  
  68.                foreach($this->data as $field=>$value):
  69.                        $updates .= "{$field}='{$value}', ";
  70.                endforeach;
  71.  
  72.                $updates = substr($updates, 0 , -2);
  73.  
  74.                 $query = "UPDATE {$this->entity} SET {$updates} WHERE {$this->field} = {$this->data[$this->field]}";
  75.         else:
  76.  
  77.                 $fields = implode(", ",array_keys($this->data));
  78.                 $values = implode(", ", array_values($this->data));
  79.  
  80.                 $inserts = "({$fields}) VALUES({$values})";
  81.                
  82.                 $query = "INSERT INTO {$this->entity} {$inserts}";
  83.  
  84.         endif;
  85.  
  86.  
  87.         return $query;
  88.  
  89.     }
  90.  
  91. }
  92.  
  93.  
  94. $conexao = mysql_connect("localhost", "root");
  95. mysql_select_db("truckmotors", $conexao);
  96.  
  97. class Loja extends  Record {
  98.  
  99.         public function  __construct($id = null) {
  100.            $this->setEntity("loja");
  101.            $this->setField('nome');
  102.             parent::__construct($id);
  103.         }
  104.  
  105. }
  106.  
  107. $loja = new Loja();
  108.  
  109. $loja->load('leo');
  110.  
  111. echo "<pre>";
  112. print_r($loja->data);
  113. echo "</pre>";
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement