Show Table In Home Page/sort By Nearest Date

Hey guys, very new to YII but so far I’ve managed to get the majority of things I want to work through the many threads on this forum, until now. What I would like to do is set a table to be loaded on the default site/index page when the site is loaded, however the table is also displayed in one of the model views, what would be the best way to complete what I want to do?

Also , on the same table I would like it to sort date by the nearest to the current date (i.e. nearest 25th of April), what would be the best way to complete this? My code can be found below for the model index and the model itself.

Model





<?php


/**

 * This is the model class for table "gig".

 *

 * The followings are the available columns in table 'gig':

 * @property integer $id

 * @property string $start

 * @property string $sname

 * @property integer $venue

 * @property integer $act

 * @property string $img

 *

 * The followings are the available model relations:

 * @property Venue $venue0

 * @property Act $act0

 */

class Gig extends CActiveRecord

{

	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'gig';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('id', 'required'),

			array('id, venue, act', 'numerical', 'integerOnly'=>true),

			array('sname, img', 'length', 'max'=>255),

			array('start', 'safe'),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('id, start, sname, metro, venue, act, img', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'venue0' => array(self::BELONGS_TO, 'Venue', 'venue'),

			'act0' => array(self::BELONGS_TO, 'Act', 'act'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'start' => 'Start',

			'sname' => 'Sname',

			'venue0->metro' => 'Metro',

			'act' => 'Act',

			'img' => 'Img',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 *

	 * Typical usecase:

	 * - Initialize the model fields with values from filter form.

	 * - Execute this method to get CActiveDataProvider instance which will filter

	 * models according to data in model fields.

	 * - Pass data provider to CGridView, CListView or any similar widget.

	 *

	 * @return CActiveDataProvider the data provider that can return the models

	 * based on the search/filter conditions.

	 */

	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare("DATE_FORMAT(start,'%m/%d/%Y')",$this->start);

		$criteria->compare('sname',$this->sname,true);

		$criteria->compare('venue',$this->venue);

		$criteria->compare('act',$this->act);

		$criteria->compare('img',$this->img,true);

		


		




		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort'=>$sort

		));

		

	}


	/**

	 * Returns the static model of the specified AR class.

	 * Please note that you should have this exact method in all your CActiveRecord descendants!

	 * @param string $className active record class name.

	 * @return Gig the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

}




Index







<?php

/* @var $this GigController */

/* @var $dataProvider CActiveDataProvider */


$this->breadcrumbs=array(

    'Gigs',

);


$this->menu=array(

    array('label'=>'Create Gig', 'url'=>array('create')),

    array('label'=>'Manage Gig', 'url'=>array('admin')),

);

?>


<h1>Gigs</h1>




<?php $this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

		 'id',

		 'sname',

        'start',

		array(

		 'name'=>'venue',

		 'value'=>'CHtml::encode($data->venue0->sname)'),

		array(

		 'name'=>'metro',

		 'value'=>'CHtml::encode($data->venue0->metro)'),

		 array(

		 'header'=>'Photo',

		 'type'=>'image',

		 'value'=>'CHtml::encode($data->act0->img)'

		 ),

		 		array(

			'class'=>'CButtonColumn',

		),			

	),

		

));




Ideally I would like the same CGridView to appear on the home page with upcoming gigs.

Okay, I have the table displaying on the home index page but now I would like it to start the records from the current date in ascending order, this is my code so far:


<?php $dataProvider=new CActiveDataProvider('Gig', array(

    'criteria'=>array(

        'condition'=>'start',

        'order'=>'start',

    ),

    'countCriteria'=>array(

        'condition'=>'id',

        // 'order' and 'with' clauses have no meaning for the count query

    ),

    'pagination'=>array(

        'pageSize'=>20,

    ),

));


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

		 'id',

		 'sname',

        'start',

		array(

		 'name'=>'venue',

		 'value'=>'CHtml::encode($data->venue0->sname)'),

		array(

		 'name'=>'metro',

		 'value'=>'CHtml::encode($data->venue0->metro)'),

		 array(

		 'header'=>'Photo',

		 'type'=>'image',

		 'value'=>'CHtml::encode($data->act0->img)'

		 ),

		 		array(

			'class'=>'CButtonColumn',

		),			

	),

		

));

A couple of questions first: You said that you wanted the table information on the index/home page and in another location, correct? Is it the exact same information? exact same layout of information?

For a selection of the same information, (SELECT * FROM blah WHERE x=y…blah,blah,blah) Look into named scopes in the model. Actually named scopes are useful in a number of places.

From your code: creating a DataProvider in the view file is, well wrong. You should place it in the controller action (look at actionAdmin for example). I very regularly place the default CRUD admin actionAdmin Code in actionIndex. Just because I like the GridView better than the ListView.

OK display the exact same table in two locations: $this->render(‘index’) display index.php from the current dir. $this->render(’/site/index’) displays views.site.index.php. I would also move the actual GridView code to an _admin.php file, then on admin page call renderPartial instead. Then on the home page you could use $this->renderPartial(’/blah/_admin’, array(‘model’=>$model) on the index/hone page where you want it to display. Remember you will need to pass any models to the renderPartial()

Thanks for this, will have a look into it later. As for sorting the date on the actual gig table where about would I do that? In the Gig CDbCriteria?

as for the sorting nearest to current date? I don’t think that is possible. placing today, tomorrow, 2 days ago, 3days in future, etc; can’t be done very easily. If you are talking just upcomming OR just recent past, crate a namedScope (scpoes()) in the model and sort ASC and condition <=today. You could another one date DESC >= today.