<?php
/**
* This is the model class for table "{{photos_albums}}".
*
* The followings are the available columns in table '{{photos_albums}}':
* @property string $id
* @property string $type
* @property string $name
* @property integer $total_photos
* @property string $updated_at
*
* The followings are the available model relations:
* @property Photos[] $photos
* @property User $user
*/
class GalleryAlbum extends CActiveRecord {
/**
* @return string the associated database table name
*/
public function tableName() {
return '{{gallery_albums}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
return array(
array('name', 'required'),
array('name', 'length', 'max' => 255),
array('user_id', 'length', 'max' => 10),
// array('total_photos', 'length', 'max' => 5),
// array('total_photos', 'numerical', 'integerOnly' => true),
// The following rule is used by search().
array('id, type, name, total_photos, updated_at', 'safe', 'on' => 'search'),
);
}
/**
* @return array relational rules.
*/
public function relations() {
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'photos' => array(self::HAS_MANY, 'GalleryPhoto', 'album_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'user_id' => 'User',
'type' => Yii::t('Gallery', 'Type of album'),
'name' => Yii::t('Gallery', 'Name'),
'total_photos' => Yii::t('Gallery', 'Total photos'),
'updated_at' => Yii::t('Gallery', 'Last photo DateTime'),
);
}
/**
* 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() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('user_id', $this->user_id, true);
$criteria->compare('type', $this->type, true);
$criteria->compare('name', $this->name, true);
$criteria->compare('total_photos', $this->total_photos, 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 GalleryAlbum the static model class
*/
public static function model($className=__CLASS__) {
return parent::model($className);
}
/**
* Default named scope
*/
public function defaultScope() {
return array(
'order' => 'updated_at DESC',
);
}
/**
* Deletes all photos inside album
*/
public function beforeDelete() {
if (parent::beforeDelete()) {
foreach ($this->photos as $photo)
if ($photo->delete() !== true)
return false;
return true;
}
return false;
}
public function getPossibleAlbums($user) {
$albums = array(null => '---');
$reader = $this->dbConnection->createCommand()->select('id, name')->from($this->tableName())->where('user_id = :user', array(':user' => $user))->query();
foreach ($reader as $album)
$albums[$album['id']] = $album['name'];
return $albums;
}
}