Yii Framework Forum: Table adapter class like Zend_db_Table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Table adapter class like Zend_db_Table Rate Topic: -----

#1 User is offline   sutharshan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-September 11

Posted 05 June 2012 - 03:23 AM

Hi Friends,

I'm new to Yii and I was using Zend framework for long time. Comparing to Zend, yii have good performance and light weight. I was looking to use the DB classes in Yii and searched for the available options to work with. I found we can use CActiveRecord class to do that. But I was searching for an easy way to implement the table operations as i was able to do with the Zend_Db_Table class where I can simply create an Zend_Db_Table object with the table name as a parameter as below.
$empobj = new Zend_Db_Table('tbl_employee');


If we have a similar feature like this in Yii, it would be easy and flexible to use as its more friendly and suits for rapid coding. I'm not sure similar mechanism is exists with the new releases, But I checked in forums and websites and i was unable to find something similar to this. Its just my thought :)

Is it possible to have some features like this in the framework 2.0?

Thanks.
0

#2 User is offline   pavlepredic 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 30-August 11

Posted 05 June 2012 - 03:50 AM

+1

Quite often you simply need to perform basic read/write on a table in an OOP fashion. As long as you don't need any further validation or other advanced features, there's no need to create a new class for it. You might end up with loads of 'scaffolding' code that does nothing whatsoever.
0

#3 User is offline   pavlepredic 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 30-August 11

Posted 07 June 2012 - 06:03 AM

Here's one solution to this problem:

<?php
class DynamicActiveRecord extends CActiveRecord
{
	protected $_tableName;
	protected $_md; //must redeclare, as parent::_md is private. Why not protected, Qiang?

	public function __construct($scenario = 'insert', $tableName = null)
	{
		$this->_tableName = $tableName;
		parent::__construct($scenario);
	}
	
	protected function instantiate($attributes)
	{
		$class=get_class($this);
		$model=new $class(null, $this->tableName());
		return $model;
	}

	public function getMetaData()
	{
		if($this->_md!==null)
			return $this->_md;
		else
			return $this->_md = new CActiveRecordMetaData($this);
	}
	
	public function tableName()
	{
		if (!$this->_tableName)
			$this->_tableName = parent::tableName();
		return $this->_tableName;	
	}
	
	public static function forTable($tableName)
	{
		return new DynamicActiveRecord('insert', $tableName);
	}
}


Then you can do something like:

$user = DynamicActiveRecord::forTable('user');
$user->name = 'Pavle';
$user->save();


or:

$user = DynamicActiveRecord::forTable('user')->findByPk(1);
echo $user->name;


Obviously, this should be properly tested first. Hopefully some Yii developer will comment if this is OK.
0

#4 User is offline   kernel 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 22-November 10

Posted 10 June 2012 - 10:20 PM

Please refer to this post which is with the solution.

http://www.yiiframew...__1#entry136002
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users