Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot instantiate abstract class system\database\ORM

Status
Not open for further replies.

aalmeida

MIS
Aug 31, 2000
468
US
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:
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');
    }    
}
This is the ORM class:
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.
 
And what is happening? ... Or not happening as the case maybe.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I enter anything in the text box and press enter and then I get the following error messages:
Calling static method 'where' title, like, %anything%
Warning: implode(): Invalid arguments passed in system\database\orm.php on line 137
Calling static method 'table'
Fatal error: Cannot instantiate abstract class system\database\ORM in system\database\orm.php on line 138

Essentially it's supposed to query the product table in the database and return me a list.

Thanks for helping.

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
All,

I appreciate any help I can get, if you have any question let me know. This was supposed to be a very simple PHP MVC for me to learn instead I'm getting more and more confused.

The basic idea is:
Open the master page that has a form with one text box and a submit button.
The value of the text box is passed to query the database, but instead I get the error above, it's clear that I'm not interacting correctly with the ORM class and that is what's causing the error, unless I'm not totally off.

Thanks,

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
What debugging have you done?

Starting at whichever is line 137

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
ChrisHirst,

I'm new at all this so please bare with me, I'm using Zend Studio 11 and line 137 is:

PHP:
echo "Calling static method '$method' ". implode(', ', $args). "\n";

and line 138 is:

PHP:
$static = new Static;

Both line are part of the
PHP:
public static function __callStatic($method, $args = array())

What else could be helpful?
Thanks,

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
the code BEFORE line 137.

The provided line number is where the error occurred NOT where the fault IS.

To debug this, you insert a line of code that will print or echo the values to the browser so you can see the values that are being passed.

in this case it is $args that does not meet the requirements of the parameter for implode()

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
It would appear to me, the second time you call your method:
Your $args variable is not an array, and thus it cannot be imploded which is why you get the :
Warning: implode(): Invalid arguments passed in system\database\orm.php on line 137
Then you call $static->query->$method with $method = 'table'. and get:
Fatal error: Cannot instantiate abstract class system\database\ORM in system\database\orm.php on line 138

Probably because your $args variable is either empty or not an array, and as such cannot be used when calling your "table" method, which generates an error.

Clearly your echos show that your args are empty. So the next bit of debugging would need to be done at what exactly the table method is expecting in its parameters, very likely something other than being empty, or not an array, and then identifying why your $args are empty or not an array.

Going back to the point where you call it:
$con = ORM::__callStatic('table', 'product');

We see you are not sending an array, but a string. Likely not something it can use.

Debugging and error checking is all about reading the errors you are getting and then looking at your code in that context.









----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top