I'm trying to learn MVC and need to query a MySQL database using PDO and ORM but I can't seem to get it right, here is the code:
This is the ORM class:
Any help is greatly appreciated .
AL Almeida
CIO
May all those that come behind us, find us faithful.
PHP:
use system\database\ORM as ORM;
class Product extends ORM {
private $prod_data;
private $con;
private $data;
public function Product(){
$con = ORM::__callStatic('table', 'product');
}
}
PHP:
<?php
namespace system\database;
abstract class ORM{
/**
* Holds the Query object where methods are called
* on it through this class using two magic methods
* __call and __callStatic
*
* @access private
* @var Query
*/
private $query;
public function __construct($table)
{
//$this->query = new Query($this->table($table));
//$query = new Query($this->table());
//$query = $db->getQuery(true);
}
/**
* Get first object from the returned objects of the
* get method
*
* @access public
* @param String $columns
* @return Object | called class object
*/
public static function find($id, $columns = "*")
{
$static = new static;
return $static->where('id', '=', $id)->first($columns);
}
/**
* Get first object from the returned objects of the
* get method
*
* @access public
* @param String $columns
* @return Object | called class object
*/
public function first($columns = "*")
{
return array_shift( $this->get($columns) );
}
/**
* Get Array of objects of the called class
*
* @access public
* @param String $columns
* @return Array | array of called class objects
*/
public function get($columns = "*")
{
return $this->query->get(
$columns,
\PDO::FETCH_CLASS,
get_called_class()
);
}
/**
* Save all this instance variables in the table
* but first we have to matchColumns with the
* instance
*
* @access public
* @return void
*/
public function save()
{
$this->query->save( (array)$this );
}
/**
* If table is not specified, the plural of the class
* name will be assumed as the table name
*
* @access public
* @return String
*/
public function table()
{
// If isset table attribute means the user
// has defined the table name
if(isset($this->table)){
return $this->table;
}else{
return \system\helpers\Inflector::pluralize(strtolower(get_called_class()));}
}
/**
* Magically call methods on the query instance and return the
* instance $this for multiple methods calling
*
* @access public
* @param String $method
* @param Array $args
* @return $this
*/
public function __call($method, $args = array())
{
if ( is_callable($this->query->$method) ) {call_user_func_array( array( $this->query, $method ), $args );}
return $this;
}
/**
* Create new static object and call the specified method on the
* query object then return it.
*
* @access public
* @param String $method
* @param Array $args
* @return Object | called class object
*/
public static function __callStatic($method, $args = array())
{
echo "Calling static method '$method' ". implode(', ', $args). "\n";
$static = new Static;
if ( is_callable($static->query->$method) ) {call_user_func_array( array($static->query, $method) , $args );}
else{echo'The static method '.$static->query->$method.' isn\'t callable';}
return $static;
}
}
Any help is greatly appreciated .
AL Almeida
CIO
May all those that come behind us, find us faithful.