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.