Retrieve Data From Database

Hi

I’m total new to Yii, and have a question about retrieve data from database.

I have created a model + CRUD for a table x (tbl_x).

At the create form, I have added some javascript there need data from the database. I have added some dummy data in the controller:


$prebooked = "['23-4-2013','22-4-2013']";

                

                $booked = "['10-4-2013','11-4-2013']";

                

		$this->render('create',array(

			'model'=>$model,

                        'prebooked'=>$prebooked,

                        'booked'=>$booked,

		));

So I need to get data from the database into $booked and $prebooked.

AS I have understod MVC then I need to add a fuction in the model? and from the controller call the function, and pass the data to the view?

I have looked / searchin the documentation but im not sure how to do this :frowning:

Any help / tips?

Best Regards

Jesper Vels

I hope I understand your question properly. But I believe you’re asking how you would retrieve the data from the database through your activerecord model and pass it to your view for consumption by your javascript. I’m assuming the following:

Database table: tbl_x (two integer columns ‘prebooked’ and ‘booked’)

ActiveRecord model: TblX

To retrieve your data from the database in your controller, you would use something like:




$bookedData    = TblX:model()->findAll('booked = 1');

$prebookedData = TblX:model()->findAll('prebooked = 1');



$booked and $prebooked would both contain arrays of the TblX model with data. You would then iterate through the array and build the string you would pass to the view.

Hi

You have almost understod my issue (or I have not describe it correct :) )

In table x I have a column "bookdate" of the type date with the format YYYY-DD-MM

Based on a status column bookdate should be prebooked or booked.

I also need to manipulate the data because the javascript need the data in this format:

[‘23-4-2013’,‘2-4-2013’]

(the sample with two days)

Where would the best place be to convert the date to the format needed by the javascript, and concardinate if there are more than one date in the database?

/Jesper

With PHP there are a number of ways you can format the date. Assuming you have pulled the record into a variable called $record:




$formatted = date('d-m-Y',strtotime($record->bookdate));



There’s also many clever ways to concat strings together, especially with the helper functions that Yii offers (such as CHtml::listData), but I’ll give you a simple solution. Assume that $records is an array of your model retrieved by using TblX::model()->findAll():




$list = array();


foreach($records as $record) {

    $list[] = date('d-m-Y',strtotime($record->bookdate));

}


$output = "['" .implode( "','", $list ). "']";



I hope that helps.

Hi

Thanks :slight_smile:

It works.

I have moved the code from the controller to the model, and moved the javascript from create form to the _form, becauase then I also have the date picker at the edit form.

/Jesper