Ask. where we put the code when we want to use DAO?

[font="Trebuchet MS"]after reading yii guide book on Chapter Performance Tuning, section Database Optimization. now i want to use a DAO technology.

[b]for the MVC best practice, where we put/write the DAO command and code?

is it placed on the model folder? if yes what type of class that we must use/ extends?[/b]

perhaps someone will help me to show the way and giving me the hint or example.

thanks.

[/font]

If you use Dao I’d like to raccomed to place the code in the controller and the presentation in view.

As you are avoiding at all models, feel free to jump this step.

If you are going to write reusable code, take in cosideration the possibility to create some public functions.

In the models in /models folder and if you want some free features such as validate(), make them CModel extensions.

I’d put them in a new directory called /dao. Thus you’ll always now where they are, you’ll keep your controllers and models “thin” (and if you ever recreate them, there will be only a few lines to carry over) and they’ll be as reusable as you whish.

[font="Trebuchet MS"]if i mix the code on controller and view . does it out of MVC rule?

looks like we will go back to traditional code then. and might be the code is difficult to maintain.

i think we will wait for another ideas. thanks for your reply [/font][font="Trebuchet MS"]zaccaria[/font][font="Trebuchet MS"].[/font]

[font="Trebuchet MS"]looks like this will be a good idea.

and we are still on the MVC rules and yii structure.[/font]

[font="Trebuchet MS"]i think this a good ide too.

what should that i must do when creating the models on a new folder? we need to modified a config right?

and what class that i must extend to write my dao code?

tq.[/font]

Go to the /config/main.php and add the new directory to the import section.

In theory your daos shouldn’t inherit from anything… it’s custom code. They should be called from the controllers and they’ll use the models to access the db. As you have more and more daos i guess you’ll have some common functionality you will want to extract to a super class. Or maybe you will use some functionality provided by a yii class so you’ll end up inheriting from it, but it’s completely up to you.

Cool twocandles, i got your point and all other’s idea. and your explaination is clearly enough for me. Thanks.


In theory your daos shouldn't inherit from anything...

What is this theory that prevents you from inheriting?

How do you call these classes in the folder /dao? if you want to give them a name.

What happens if you exchange the names of these folders /DAO and /models?

There’s no theory against inheriting (although object composition is preferred to inheritance according to software gurus). A DAO is just a layer to abstract from a DB access concrete API. AFAIK, the DAO pattern should be something like this:




interface UserDao

{

  public function saveUser( $user );

}


class UserDaoImpl implements UserDao

{

  public function saveUser( $user )

  {

    $user->save();

  }

}



And lately I’m becoming a big fan of factories: they allow for easy switching between implementations, so I’d go on with something like this:




// This can be implemented as a singleton or make the methods static.

// I usually go for the former: static methods are tougher to change

class DaoFactory

{

  // I'd implement the factory as a singleton. I'll avoid such a code.

  static public function getFactory()

  {

    (...)

  }


  /**

    * @returns UserDao THIS METHOD SHOULD RETURN THE INTERFACE

    */

  public function getUserDao()

  {

    // You can use a singleton too if you whish, 

    // or return a new instance every time it's invoked

    return new UserDaoImpl;

  }

}



And finally in your controller




public function actionCreate()

{

  $user = new User;

  $user = $_POST['user'];

  $userDao = DaoFactory::getFactory()->getUserDao();

  $userDao->saveUser( $user );

}



It’s just some simplified code, but I think you can get the idea…

Right, but Yii has its own DAO…




Yii DAO mainly consists of the following four classes:


CDbConnection: represents a connection to a database.

CDbCommand: represents an SQL statement to execute against a database.

CDbDataReader: represents a forward-only stream of rows from a query result set.

CDbTransaction: represents a DB transaction.



[font=“Trebuchet MS”]interesting… i’m have no depth knowledge about OOP concept and the implementation on each functionality.

is there any significant difference between using yours then using yii classes?[/font]

DAO stands for Database Access Object, so any code that serves for that purpose may be considered a DAO. Of course Yii has it’s own methods to do it, but the aim of applying a DAO pattern is to abstract your code from Yii’s (or any particular framework) methods. In my previous code, if you ever change your framework or you want to use the methods from CDbCommand, CDbConnection and so on instead of CActiveRecord methods, changing the code in YOUR daos will be enough, instead of changing any Yii custom methods scattered around your code.