Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Daytrip extends CActiveRecord
- {
- public $Categories;
- public $Tags;
- public static function model($className = __CLASS__)
- {
- return parent::model($className);
- }
- public function tableName()
- {
- return 'Daytrips';
- }
- public function rules()
- {
- return array(
- array('CityId, Daytrip, Street, Number, ZipCode, Website, Categories, Tags, Active', 'required'),
- array('Email', 'email'),
- array('Website, YouTube', 'url'),
- array('Email, Priority', 'safe'),
- array('CityId', 'exist', 'className' => 'City', 'attributeName' => 'IdCity'),
- array('Tags', 'match', 'pattern' => '/^[\w\s,]+$/', 'message' => 'Tags can only contain word characters.'),
- array('Phone, Fax', 'match', 'pattern' => '/^0[1-9][0-9]{0,2}-?[1-9][0-9]{5,7}$/', 'message' => 'Phone/Fax can only contain numeric characters.'),
- );
- }
- public function attributeLabels()
- {
- return array(
- 'CityId' => Yii::t('labels', 'CityId'),
- 'Daytrip' => Yii::t('labels', 'Daytrip'),
- 'ContactPerson' => Yii::t('labels', 'Contact Person'),
- 'Street' => Yii::t('labels', 'Street'),
- 'Number' => Yii::t('labels', 'Number'),
- 'ZipCode' => Yii::t('labels', 'ZipCode'),
- 'City' => Yii::t('labels', 'City'),
- 'Phone' => Yii::t('labels', 'Phone'),
- 'Fax' => Yii::t('labels', 'Fax'),
- 'Email' => Yii::t('labels', 'Email'),
- 'Website' => Yii::t('labels', 'Website'),
- 'YouTube' => Yii::t('labels', 'YouTube'),
- 'Categories' => Yii::t('labels', 'Categories'),
- 'Tags' => Yii::t('labels', 'Tags'),
- 'Priority' => Yii::t('labels', 'Priority'),
- 'Active' => Yii::t('labels', 'Active'),
- );
- }
- public function relations()
- {
- return array(
- 'rCity' => array(self::BELONGS_TO, 'City', 'CityId', 'alias' => 'rCity'),
- 'rCategory' => array(self::MANY_MANY, 'Category', 'DaytripCategory(DaytripId, CategoryId)'),
- 'rTag' => array(self::MANY_MANY, 'Tag', 'DaytripTag(DaytripId, TagId)'),
- 'rTag2' => array(self::MANY_MANY, 'Tag', 'DaytripTag(DaytripId, TagId)',
- 'select' => array('GROUP_CONCAT(Tag SEPARATOR ", ") AS gTags'),
- //'group' => 'IdDaytrip'
- ),
- );
- }
- public function getTagArray()
- {
- $tags = array();
- foreach ($this->rTag as $tag)
- {
- $tags[] = trim($tag->Tag);
- }
- return array_unique($tags);
- }
- protected function afterSave()
- {
- if(!$this->isNewRecord)
- {
- $this->dbConnection->createCommand("DELETE FROM DaytripTag WHERE DaytripId = {$this->IdDaytrip}")->execute();
- $this->dbConnection->createCommand("DELETE FROM DaytripCategory WHERE DaytripId = {$this->IdDaytrip}")->execute();
- }
- foreach(array_unique( array_filter( explode(',', $this->Tags), trim) ) as $name)
- {
- if(($tag = Tag::model()->findByAttributes(array('Tag' => $name))) === null)
- {
- $tag = new Tag;
- $tag->Tag = $name;
- $tag->save();
- }
- $this->dbConnection->createCommand("INSERT INTO DaytripTag (DaytripId, TagId) VALUES ({$this->IdDaytrip}, {$tag->IdTag})")->execute();
- }
- foreach($this->Categories as $id)
- {
- $this->dbConnection->createCommand("INSERT INTO DaytripCategory (DaytripId, CategoryId) VALUES ({$this->IdDaytrip}, {$id})")->execute();
- }
- }
- protected function afterDelete()
- {
- $this->dbConnection->createCommand("DELETE FROM DaytripTag WHERE DaytripId = {$this->id}")->execute();
- $this->dbConnection->createCommand("DELETE FROM DaytripRubriek WHERE DaytripId = {$this->id}")->execute();
- }
- public function afterFind()
- {
- if(!empty($this->rCategory))
- {
- foreach($this->rCategory as $Category)
- $this->Categories[] = $Category->IdCategory;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement