Creating a simple class

How could i create a simple class using the features of yii?

i dont need active records and nothing, just a class that can access to database using Yii

maybe its under models? CModel?

sorry im new

Yii is a framework, so to access data, i think is better to use the feature of yii. So use gii tool on one table as see the code generated. I hope you will find what you want.

Hi,

if you don’t like/need the features of active record, you can use DAO.

http://www.yiiframework.com/doc/guide/1.1/en/database.dao

That is something like an "low level intervace" if you like to read or write an small amount of data. (because DAO is faster).

If you like to process the data before or after save (to database), if you like to read user input, if you like to list the data, search … and so on … so use active record.

rall0r

In fact you have to use a CActiveRecord, wich is a model representation of a table.

You can have a look at the definitive guide, and at the api.

I recommend you to use the generator of code gii wich will generate a class code based on your table metadata automatically. You have to enable it in your main/config file. and then go on the page gii/ of your website.

Think to have a look at the blog tutorial, it can help you in many ways.

maybe you guys are not understanding what i want

i want to create a simple class for multiple tasks, and i dont want to extend nothing from other class

is this a correct usage?


<?php


/**

 * Comunity class.

 * Comunity is the data structure for keeping

 */

class Comunity // extends CModel

{

	const tableName = 'society';

	

	// Get all comunities and store into cache

	public static function getAll()

	{

		$data=Yii::app()->cache->get('site.society');

		if($data===false || !is_array($data))

		{

			// regenerate $data because it is not found in cache

			// and save it in cache for later use:

			$command = Yii::app()->createCommand();

			$data = $command

					->select()

					->from(Comunity::tableName)

					->queryAll();

			$command->reset();  // clean up the previous query

			Yii::app()->cache->set('site.society', $data, 18000); // 5 Hours cache

		}

		return $data;

	}

}

but returns error


Fatal error: Call to a member function get() on a non-object in D:\wamp\www\yii\eurom\protected\models\Comunity.php on line 14

using in a view file as test:


<?php echo Comunity::getAll(); ?>

Check the line that the error points to…

I presume it’s this one… as you did not add the line numbers…


$data=Yii::app()->cache->get('site.society');

As the error says: Call to a member function get() on a non-object

Seems that Yii::app()->cache does not return a cache components…

So check the value of Yii::app()->cache before that call…

It’s possible that you have not defined it?

yes, this one

did i need to enable caching somewhere? i use default config and i did not have memcache

Check the documentation for caching - http://www.yiiframework.com/doc/guide/1.1/en/caching.overview

yes, my fault. it fixed

now i have other error:


CWebApplication não possui um método chamado "createCommand".

as


 throw new CException(Yii::t('yii','{class} does not have a method named "{name}".',

Again… you need to check what is on the line that the error points you…

I supose this is the line


$command = Yii::app()->createCommand();

app() does not have createCommand…

The solution is now easy… I leave the solution for this to you… :)

true i fell stupid, i copy from some place, maybe docs, how could Ctrl+C & Ctrl+V betray me

thanks!

You are right… I found this typo error here - http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder#building-multiple-queries

Fixed it in the SVN trunk…

Thanks

oh, so i have to say sorry to ctrl+c ;)

Thanks