model:
<?php
/**
* This is the model class for table "device".
*
* The followings are the available columns in table 'device':
* @property integer $deviceID
* @property integer $productID
* @property string $devicename
* @property string $serialnumber
* @property string $spit_dealer
* @property integer $spit_dealer_postalcode
* @property string $spit_dealer_city
* @property string $invoicenumber
* @property string $invoicedate
* @property integer $companyID
* @property string $registration_email
* @property string $date_waranty
* @property string $date_fos
* @property string $state_fos
* @property string $state_customer
* @property string $registration_date
*/
class Device extends CActiveRecord
{
public function getProducts()
{
return CHtml::listData(Product::model()->findAll(), 'productID', 'title');
}
public function getStateCustomer()
{
return CHtml::listData(DeviceStateCustomer::model()->findAll(), 'id', 'title');
}
public function getStateFos()
{
return CHtml::listData(DeviceStateFos::model()->findAll(), 'id', 'title');
}
// Return Product_Title.
public function getProductTitle(){
$product = Product::model()->findByPk($this->productID);
return $product->title;
}
// Return Productarticlenumber.
public function getProductarticlenumber(){
$product = Product::model()->findByPk($this->productID);
return $product->articlenumber;
}
// Return Product_Warrantytime.
public function getProductWarrantytime(){
$product = Product::model()->findByPk($this->productID);
return $product->warrantytime;
}
// Return Product_Repaircosts.
public function getProductRepaircosts(){
$product = Product::model()->findByPk($this->productID);
return $product->repaircosts;
}
// Return StateCustomer_Title.
public function getStateCustomerTitle(){
$statecustomer = DeviceStateCustomer::model()->findByPk($this->state_customer);
return $statecustomer->title;
}
// Return StateFos_Title.
public function getStateFosTitle(){
$statefos = DeviceStateFos::model()->findByPk($this->state_fos);
return $statefos->title;
}
// Return CompanyFirma.
public function getCompanyFirma(){
$company = Company::model()->findByPk($this->companyID);
return $company->company;
}
// Return CompanyStrasse.
public function getCompanyStrasse(){
$company = Company::model()->findByPk($this->companyID);
return $company->street;
}
// Return CompanyPLZ.
public function getCompanyPLZ(){
$company = Company::model()->findByPk($this->companyID);
return $company->postalcode;
}
// Return CompanyOrt.
public function getCompanyOrt(){
$company = Company::model()->findByPk($this->companyID);
return $company->city;
}
// Return CompanyLand.
public function getCompanyLand(){
$company = Company::model()->findByPk($this->companyID);
return $company->country;
}
// Return CompanyAnsprechpartner.
public function getCompanyAnsprechpartner(){
$company = Company::model()->findByPk($this->companyID);
return $company->contact;
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'device';
}
/**
* @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('productID, serialnumber, spit_dealer, spit_dealer_postalcode, spit_dealer_city, invoicedate, invoicenumber', 'required'),
array('serialnumber', 'unique', 'message' => Yii::t("Device","This serialnumber already exists.")),
array('productID, spit_dealer_postalcode, companyID', 'numerical', 'integerOnly'=>true),
array('devicename, serialnumber, spit_dealer, spit_dealer_city, invoicenumber, registration_email, state_fos, state_customer', 'length', 'max'=>255),
array('invoicedate, date_waranty, date_fos', 'safe'),
array('invoicedate, date_waranty, date_fos', 'default', 'value' => date('dd.mm.yyyy H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('deviceID, productID, devicename, serialnumber, spit_dealer, spit_dealer_postalcode, spit_dealer_city, invoicenumber, invoicedate, companyID, registration_email, date_waranty, date_fos, state_fos, state_customer', '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(
'DeviceStateFos'=>array(self::BELONGS_TO, 'DeviceStateFos', 'state_fos'),
'Product'=>array(self::BELONGS_TO, 'Product', 'productID'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'deviceID' => Yii::t('Device','Device'),
'productID' => Yii::t('Device','Product'),
'devicename' => Yii::t('Device','Devicename'),
'serialnumber' => Yii::t('Device','Serialnumber'),
'spit_dealer' => Yii::t('Device','Spit Dealer'),
'spit_dealer_postalcode' => Yii::t('Device','Spit Dealer Postalcode'),
'spit_dealer_city' => Yii::t('Device','Spit Dealer City'),
'invoicenumber' => Yii::t('Device','Invoice No.'),
'invoicedate' => Yii::t('Device','Invoice Date (yy-mm-dd)'),
'companyID' => Yii::t('Device','Company'),
'registration_email' => Yii::t('Device','Registration Email'),
'date_waranty' => Yii::t('Device','Date Waranty'),
'date_fos' => Yii::t('Device','Date Fos'),
'state_fos' => Yii::t('Device','State Fos'),
'state_customer' => Yii::t('Device','State Customer'),
'registration_date' => Yii::t('Device','Registration Date'),
);
}
/**
* 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;
If (Yii::app()->user->isAdmin())
{
//$criteria->condition='companyID=0';
}
else
{
If (Yii::app()->user->isGuest)
{
$criteria->condition='companyID like \'%\'';
}
else
{
$criteria->condition='companyID='.Yii::app()->user->created_by;
}
}
$criteria->compare('deviceID',$this->deviceID);
$criteria->compare('productID',$this->productID);
$criteria->compare('devicename',$this->devicename,true);
$criteria->compare('serialnumber',$this->serialnumber,true);
$criteria->compare('spit_dealer',$this->spit_dealer,true);
$criteria->compare('spit_dealer_postalcode',$this->spit_dealer_postalcode);
$criteria->compare('spit_dealer_city',$this->spit_dealer_city,true);
$criteria->compare('invoicenumber',$this->invoicenumber,true);
$criteria->compare('invoicedate',$this->invoicedate,true);
$criteria->compare('companyID',$this->companyID);
$criteria->compare('registration_email',$this->registration_email,true);
$criteria->compare('date_waranty',$this->date_waranty,true);
$criteria->compare('date_fos',$this->date_fos,true);
$criteria->compare('state_fos',$this->state_fos,true);
$criteria->compare('state_customer',$this->state_customer,true);
$criteria->compare('registration_date',$this->registration_date,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* 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 Device the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
CGridView:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'device-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'devicename',
'value' => 'CHtml::link($data->devicename, Yii::app()->createUrl("product/viewsmall",array("id"=>$data->productID)), array("target"=>"_blank","title"=>"Seriennummer finden"))',
'type' => 'raw',
),
'serialnumber',
'invoicenumber',
array(
'name'=>'invoicedate',
'value'=>'$data->invoicedate',
'filter'=>false,
),
array(
'name'=>'date_waranty',
'value'=>'$data->date_waranty',
'filter'=>false,
),
array(
'name'=>'date_fos',
'value'=>'$data->date_fos',
'filter'=>false,
),
array(
'header'=>Yii::t('Device','State Fos'),
'value'=>'$data->DeviceStateFos->title',
),
array(
'header'=>Yii::t('Product','Repair Costs in €'),
'value'=>'$data->Product->repaircosts',
),
),
)); ?>