class Purchase extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Purchase the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return CDbConnection database connection
*/
public function getDbConnection()
{
return Yii::app()->dbhcr;
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'purchase';
}
/**
* @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('customer_id, product_id, usertype', 'required'),
array('year', 'numerical', 'integerOnly'=>true),
array('customer_id, product_id', 'length', 'max'=>10),
array('serialnumber, usertype, orcnumber', 'length', 'max'=>25),
array('datepurchased, warrantyexpiry', 'type', 'type' => 'date', 'message' => '{attribute}: is not in date format yyyy-mm-dd', 'dateFormat' => 'yyyy-mm-dd'),
array('datepurchased, warrantyexpiry', 'default', 'setOnEmpty' => true, 'value' => null),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, customer_id, product_id, serialnumber, year, datepurchased, warrantyexpiry, orcnumber', 'safe', 'on'=>'search'),
array('orcnumber, year, serialnumber', 'safe'),
);
}
/**
* @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(
'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
'customer' => array(self::BELONGS_TO, 'Customer', 'customer_id'),
'services' => array(self::HAS_MANY, 'Service', 'purchase_id'),
);
}
/**
* @return array of Products.
*/
public function getProductList()
{
// this gives all possible Editions
$criteria = new CDbCriteria();
$criteria->select = array('id', 'pnc');
$products = product::model()->findAll($criteria);
if (count($products))
{
return CHtml::listData($products, 'id', 'pnc');
}
else
{
return $array();
}
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'customer_id' => 'Customer',
'product_id' => 'Product',
'serialnumber' => 'Serial#',
'usertype' => 'User type',
'year' => 'Year',
'datepurchased' => 'Date purchased',
'warrantyexpiry' => 'Warranty expiry',
'orcnumber' => 'ORC#',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('customer_id',$this->customer_id,false);
$criteria->compare('product_id',$this->product_id,false);
$criteria->compare('serialnumber',$this->serialnumber,true);
$criteria->compare('year',$this->year);
$criteria->compare('datepurchased',$this->datepurchased,false);
$criteria->compare('warrantyexpiry',$this->warrantyexpiry,false);
$criteria->compare('orcnumber',$this->orcnumber,true);
return new CActiveDataProvider($this, array(
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
'criteria'=>$criteria,
));
}
}