Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Record{
- public $data = array();
- private $entity;
- private $field = 'id';
- private $load = false;
- public function __construct($value = null) {
- if($value){
- $this->load($value);
- }
- }
- public function __set($name, $value) {
- $this->data[$name] = $value;
- }
- public function __get($name) {
- return $this->data[$name];
- }
- protected function setEntity($entity){
- $this->entity = $entity;
- }
- public function setField($field){
- $this->field = $field;
- }
- public function load($value){
- if($value):
- $query = "SELECT * FROM {$this->entity} WHERE {$this->field} = '{$value}'";
- $result = mysql_query($query);
- if(mysql_num_rows($result) > 0):
- $data = mysql_fetch_object($result);
- foreach($data as $key=>$value):
- $this->data[$key] = $value;
- endforeach;
- $this->load = true;
- return true;
- endif;
- endif;
- return false;
- }
- public function store(){
- if($this->load):
- $updates = '';
- foreach($this->data as $field=>$value):
- $updates .= "{$field}='{$value}', ";
- endforeach;
- $updates = substr($updates, 0 , -2);
- $query = "UPDATE {$this->entity} SET {$updates} WHERE {$this->field} = {$this->data[$this->field]}";
- else:
- $fields = implode(", ",array_keys($this->data));
- $values = implode(", ", array_values($this->data));
- $inserts = "({$fields}) VALUES({$values})";
- $query = "INSERT INTO {$this->entity} {$inserts}";
- endif;
- return $query;
- }
- }
- $conexao = mysql_connect("localhost", "root");
- mysql_select_db("truckmotors", $conexao);
- class Loja extends Record {
- public function __construct($id = null) {
- $this->setEntity("loja");
- $this->setField('nome');
- parent::__construct($id);
- }
- }
- $loja = new Loja();
- $loja->load('leo');
- echo "<pre>";
- print_r($loja->data);
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement