Advertisement
Guest User

Untitled

a guest
Oct 29th, 2009
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. <?php
  2.  
  3. class Daytrip extends CActiveRecord
  4. {
  5.     public $Categories;
  6.     public $Tags;
  7.  
  8.     public static function model($className = __CLASS__)
  9.     {
  10.         return parent::model($className);
  11.     }
  12.  
  13.     public function tableName()
  14.     {
  15.         return 'Daytrips';
  16.     }
  17.    
  18.     public function rules()
  19.     {
  20.         return array(            
  21.             array('CityId, Daytrip, Street, Number, ZipCode, Website, Categories, Tags, Active', 'required'),
  22.             array('Email', 'email'),
  23.             array('Website, YouTube', 'url'),
  24.             array('Email, Priority', 'safe'),
  25.             array('CityId', 'exist', 'className' => 'City', 'attributeName' => 'IdCity'),
  26.             array('Tags', 'match', 'pattern' => '/^[\w\s,]+$/', 'message' => 'Tags can only contain word characters.'),
  27.             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.'),
  28.         );
  29.     }
  30.    
  31.     public function attributeLabels()
  32.     {
  33.         return array(
  34.             'CityId' => Yii::t('labels', 'CityId'),
  35.             'Daytrip' => Yii::t('labels', 'Daytrip'),
  36.             'ContactPerson' => Yii::t('labels', 'Contact Person'),
  37.             'Street' => Yii::t('labels', 'Street'),
  38.             'Number' => Yii::t('labels', 'Number'),
  39.             'ZipCode' => Yii::t('labels', 'ZipCode'),
  40.             'City' => Yii::t('labels', 'City'),
  41.             'Phone' => Yii::t('labels', 'Phone'),
  42.             'Fax' => Yii::t('labels', 'Fax'),
  43.             'Email' => Yii::t('labels', 'Email'),
  44.             'Website' => Yii::t('labels', 'Website'),
  45.             'YouTube' => Yii::t('labels', 'YouTube'),
  46.             'Categories' => Yii::t('labels', 'Categories'),
  47.             'Tags' => Yii::t('labels', 'Tags'),
  48.             'Priority' => Yii::t('labels', 'Priority'),
  49.             'Active' => Yii::t('labels', 'Active'),
  50.         );
  51.     }
  52.  
  53.     public function relations()
  54.     {
  55.         return array(
  56.             'rCity' => array(self::BELONGS_TO, 'City', 'CityId', 'alias' => 'rCity'),
  57.             'rCategory' => array(self::MANY_MANY, 'Category', 'DaytripCategory(DaytripId, CategoryId)'),
  58.             'rTag' => array(self::MANY_MANY, 'Tag', 'DaytripTag(DaytripId, TagId)'),
  59.             'rTag2' => array(self::MANY_MANY, 'Tag', 'DaytripTag(DaytripId, TagId)',
  60.                 'select' => array('GROUP_CONCAT(Tag SEPARATOR ", ") AS gTags'),
  61.                 //'group' => 'IdDaytrip'
  62.             ),
  63.         );
  64.     }
  65.    
  66.     public function getTagArray()
  67.     {
  68.         $tags = array();
  69.        
  70.         foreach ($this->rTag as $tag)
  71.         {
  72.             $tags[] = trim($tag->Tag);
  73.         }
  74.         return array_unique($tags);
  75.     }
  76.    
  77.     protected function afterSave()
  78.     {
  79.         if(!$this->isNewRecord)
  80.         {
  81.             $this->dbConnection->createCommand("DELETE FROM DaytripTag WHERE DaytripId = {$this->IdDaytrip}")->execute();
  82.             $this->dbConnection->createCommand("DELETE FROM DaytripCategory WHERE DaytripId = {$this->IdDaytrip}")->execute();
  83.         }
  84.        
  85.         foreach(array_unique( array_filter( explode(',', $this->Tags), trim) ) as $name)
  86.         {
  87.             if(($tag = Tag::model()->findByAttributes(array('Tag' => $name))) === null)
  88.             {
  89.                 $tag = new Tag;
  90.                 $tag->Tag = $name;
  91.                 $tag->save();
  92.             }
  93.             $this->dbConnection->createCommand("INSERT INTO DaytripTag (DaytripId, TagId) VALUES ({$this->IdDaytrip}, {$tag->IdTag})")->execute();
  94.         }
  95.  
  96.         foreach($this->Categories as $id)
  97.         {
  98.             $this->dbConnection->createCommand("INSERT INTO DaytripCategory (DaytripId, CategoryId) VALUES ({$this->IdDaytrip}, {$id})")->execute();
  99.         }
  100.     }
  101.  
  102.     protected function afterDelete()
  103.     {
  104.         $this->dbConnection->createCommand("DELETE FROM DaytripTag WHERE DaytripId = {$this->id}")->execute();
  105.         $this->dbConnection->createCommand("DELETE FROM DaytripRubriek WHERE DaytripId = {$this->id}")->execute();
  106.     }
  107.  
  108.     public function afterFind()
  109.     {
  110.         if(!empty($this->rCategory))
  111.         {
  112.             foreach($this->rCategory as $Category)
  113.                 $this->Categories[] = $Category->IdCategory;
  114.         }
  115.     }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement