A wrapper for DAO based models

I m trying to use DAO in my models instead of AR.

I ve created a base model file in the components folder:




class BaseModel extends CModel 

{

	public static $db;

	

	public function init() {

		parent::init();

		$this->getDbConnection()->setActive(true);

	}

	

	public function attributeNames() {

		return array();

	}


	public function getDbConnection() {

		if(self::$db!==null)

			return self::$db;

		else {

			self::$db=Yii::app()->getDb();

			if(self::$db instanceof CDbConnection) {

				self::$db->setActive(true);

				return self::$db;

			}

			else

				throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));

		}

	}

	

	

}

?>



My model file is:




class Content extends BaseModel

{

	public function getContent() {

	  $sqlStatement = "SELECT title, body FROM `tbl_content`";

	  $command=$db->createCommand($sqlStatement);

	

	  return($command->queryAll()); 

	}

}



When i try to run getContent() function, i get the following error:

"Fatal error: Call to a member function createCommand() on a non-object"

So what should i do, what is the best way for a DAO warapper to use as a parent class in model files?

thank you…

hm, looks like $db is not an object.

try this:

$command=parent::db->createCommand($sqlStatement);

If you want to do it this way, you need to access $db with self::$db in your getContent method.

i ve tried




$db = self::db;

$command=$db->createCommand($sqlStatement);



and




$db = parent::db;

$command=$db->createCommand($sqlStatement);



both returns same error:

Fatal error: Undefined class constant ‘db’

then i ve tried




$db = $this->db;

$command=$db->createCommand($sqlStatement);



this time i get this one :

Fatal error: Call to a member function createCommand() on a non-object

$this->db->createCommand($sqlStatement); ?

EDIT: and you have made sure that BaseModel is included when you call Content class?

You make things pretty complicated :). First: CModel doesn’t have a init() method, so your init() is never called. Thus self::$db is empty. It should be sufficient to have it like:


class BaseModel extends CModel 

{

        public static $db;

        

        public function getDbConnection() {

                if(self::$db===null) {

                        self::$db=Yii::app()->getDb();

                        self::$db->setActive(true);

                }

                return self::$db;

        }

}



Then in your concrete model $this->getDbConnection() should always give you the same activated connection.

Yes, you r right, so i ve added constructor method to the basemodel class:


	

public function __construct()

{

$this->getDbConnection()->setActive(true);

}



i ve seen my other mistake, instead of self::$db or parent::$db , i ve used self::db and parent::db

now i can access the db object but still cant get the rows :(

my apache server simply stops running when i execute this code:

$command=parent::$db->createCommand($sqlStatement);

since it stops without any error message, i dont know what to do…

i think that was all about my test server, i ve moved the code to another server, and worked :)

thanks to everyone who helped me to see my faults.

hope someone will write a detailed and extensive wrapper for dao, preferably with cache component.