diff options
Diffstat (limited to 'easyCRUD')
-rw-r--r-- | easyCRUD/Person.class.php | 13 | ||||
-rw-r--r-- | easyCRUD/easyCRUD.class.php | 96 | ||||
-rw-r--r-- | easyCRUD/index.php | 39 |
3 files changed, 148 insertions, 0 deletions
diff --git a/easyCRUD/Person.class.php b/easyCRUD/Person.class.php new file mode 100644 index 0000000..131b6b3 --- /dev/null +++ b/easyCRUD/Person.class.php @@ -0,0 +1,13 @@ +<?php + require_once("easyCRUD.class.php"); + + class Person Extends Crud { + + # Your Table name + protected $table = 'persons'; + + # Primary Key of the Table + protected $pk = 'id'; + } + +?>
\ No newline at end of file diff --git a/easyCRUD/easyCRUD.class.php b/easyCRUD/easyCRUD.class.php new file mode 100644 index 0000000..3e4c647 --- /dev/null +++ b/easyCRUD/easyCRUD.class.php @@ -0,0 +1,96 @@ +<?php +/** +* Easy Crud - This class kinda works like ORM. Just created for fun :) I might make my own framework one day. +* +* @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) +* @version 0.1a +*/ +require_once("../Db.class.php"); +class Crud { + + private $db; + + public $variables; + + public function __set($name,$value){ + $this->variables[$name] = $value; + } + + public function __get($name) + { + if(array_key_exists($name,$this->variables)) { + return $this->variables[$name]; + } + + $trace = debug_backtrace(); + trigger_error( + 'Undefined property via __get(): ' . $name . + ' in ' . $trace[0]['file'] . + ' on line ' . $trace[0]['line'], + E_USER_NOTICE); + return null; + } + + public function __construct($data = array()) { + $this->db = new DB(); + $this->variables = $data; + } + + public function save($id = "0") { + $this->variables[$this->pk] = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk]; + + $fieldsvals = ''; + $columns = array_keys($this->variables); + + foreach($columns as $column) + { + if($column !== $this->pk) + $fieldsvals .= $column . " = :". $column . ","; + } + + $fieldsvals = substr_replace($fieldsvals , '', -1); + + if(count($columns) > 1 ) { + $sql = "UPDATE " . $this->table . " SET " . $fieldsvals . " WHERE " . $this->pk . "= :" . $this->pk; + return $this->db->query($sql,$this->variables); + } + } + + public function create() { + $bindings = $this->variables; + + if(!empty($bindings)) { + $fields = array_keys($bindings); + $fieldsvals = array(implode(",",$fields),":" . implode(",:",$fields)); + $sql = "INSERT INTO ".$this->table." (".$fieldsvals[0].") VALUES (".$fieldsvals[1].")"; + } + else { + $sql = "INSERT INTO ".$this->table." () VALUES ()"; + } + + return $this->db->query($sql,$bindings); + } + + public function delete($id = "") { + $id = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk]; + + if(!empty($id)) { + $sql = "DELETE FROM " . $this->table . " WHERE " . $this->pk . "= :" . $this->pk. " LIMIT 1" ; + return $this->db->query($sql,array($this->pk=>$id)); + } + } + + public function find($id = "") { + $id = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk]; + + if(!empty($id)) { + $sql = "SELECT * FROM " . $this->table ." WHERE " . $this->pk . "= :" . $this->pk . " LIMIT 1"; + $this->variables = $this->db->row($sql,array($this->pk=>$id)); + } + } + + public function all(){ + return $this->db->query("SELECT * FROM " . $this->table); + } +} +?>
\ No newline at end of file diff --git a/easyCRUD/index.php b/easyCRUD/index.php new file mode 100644 index 0000000..23c4e8b --- /dev/null +++ b/easyCRUD/index.php @@ -0,0 +1,39 @@ +<?php + + require("Person.class.php"); + + //$person = new Person(array("Firstname"=>"kona","Age"=>"20","Sex"=>"F")); + //$person = new Person(array("id"=>"67")); + +// Create new person +// $person->Firstname = "Kona"; +// $person->Age = "20"; +// $person->Sex = "F"; +// $creation = $person->Create(); + + //var_dump($creation); + +// Update Person Info +// $person->id = "4"; +// $person->Age = "32"; +// $saved = $person->Save(); + +// var_dump($saved); + +// Find person + //$person->id = "4"; + //$person->Find(); + +// echo $person->Firstname; +// echo $person->Age; + +// Delete person +// $person->id = "17"; + $delete = $person->Delete(); + var_dump($delete); + + // Get all persons + $persons = $person->all(); + + +?>
\ No newline at end of file |