Wrapping Dao Class, Is That A Good Practice?

Hello all,

sorry if my posting is silly.

I’m working on project that just have one database, and base on several articles that points DAO is more lightweight and pretty straightforward (Looks like ADO for me).

I have write a simple class, looks like this:





class DBHelper extends CActiveRecord

{


    const execute = 0;

    const query = 1;

    const queryAll = 2;

    const queryRow = 3;

    const queryColumn = 4;

    const queryScalar = 5;


    private static $_command = null;




    //make this class into singelton


    public static function GetInstance()

    {

        if(!(self::$_command instanceof DBHelper))

        {

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

        }


        return self::$_command;

    }


    public function SelectRecord($query, $executeMod, $fetchAssociative=true, $params=array())

    {

        $command = self::GetInstance()->createCommand($query);


        switch($executeMod)

        {

            case 0:

                $result = $command->execute($params=array());

                break;

            case 1:

                $result = $command->query($params=array());

                break;

            case 2:

                $result = $command->queryAll($fetchAssociative, $params=array());

                break;

            case 3:

                $result = $command->queryRow($fetchAssociative, $params=array());

                break;

            case 4:

                $result = $command->queryColumn($params=array());

                break;

            case 5:

                $result = $command->queryScalar($params=array());


                break;

            default:

                $result = null;

                throw new Exception('Exception from DBComponent Class....');

                break;

        }

        return $result;

    }


    public function  TExecute($query)

    {

        $transaksi = self::GetInstance()->beginTransaction();

        try

        {

            self::GetInstance()->createCommand($query);

            self::GetInstance()->execute();

            $transaksi->commit();

        } catch(Exception $e)

        {

            $transaksi->rollback();

        }

    }


    public function TArrExecute($query)

    {

        $connT = Yii::app()->db();

        $transaksi = Yii::app()->db->beginTransaction();

        try

        {

            foreach($query as $q)

            {

                self::GetInstance()->createCommand($q);

                self::GetInstance()->execute();

            }

            $transaksi->commit();

        } catch(Exception $e)

        {

            $transaksi->rollback();

        }

    }


}



P/S: that code is not tested…

Is that a good practice to write a wrapper for DAO? or maybe i just use the active record or Query builder?

There is no reason to re-invent the wheel unless you want to extend the core functionality. Use query builder is my view on this!

There’s nothing wrong with him extending core functionality (which is what he’s doing by writing an extension of the CActiveRecord class). However, in your case, I can’t really see any point in doing so. It actually looks like you’ve just made your application more confusing. Trying to consolidate all of those different functions in to the one by passing through a different integer value seems like a bad idea.

Thank you all,

How about transaction with query builder? is that a good choice to extend my code for that? because i don’t see any documentation in yii for the transaction.

Extending from CActiveRecord is fine, but why are you wanting to extend it in the first place? What’s your reasoning behind doing so?