Advertisement
linccce

Can't figure out cause of error

Aug 7th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.74 KB | None | 0 0
  1. Model:
  2. <?php
  3.  
  4. class Advertisement extends CActiveRecord
  5. {
  6.     /**
  7.      * @return string the associated database table name
  8.      */
  9.     public function tableName()
  10.     {
  11.         return 'base_advertisement';
  12.     }
  13.  
  14.     public $picture;
  15.  
  16.     /**
  17.      * @return array validation rules for model attributes.
  18.      */
  19.     public function rules()
  20.     {
  21.         // NOTE: you should only define rules for those attributes that
  22.         // will receive user inputs.
  23.         return array(
  24.             array('user_id,category_id, title, description, city, phone', 'required'),
  25.             array('user_id, category_id, inactive, blocked', 'numerical', 'integerOnly'=>true),
  26.             array('city, phone', 'length', 'max'=>255),
  27.                         array('title', 'length', 'min'=>5, 'max'=>150),
  28.                         array('description', 'length', 'min'=>5, 'max'=>1000),
  29.                         array('phone', 'match', 'pattern'=>'/^[+][370]{3}[0-9]{8}$/', 'message' => Yii::t('svsite', 'Phone number should contain +370XXXXXXXX format')),
  30.             array('title, description, photo_url, expired_date, deactivated_date', 'safe'),
  31.             array('picture', 'length', 'max' => 255, 'tooLong' => '{attribute} is too long (max {max} chars).'),
  32.             array('picture', 'file', 'types' => 'jpg,jpeg,gif,png', 'maxSize' => 1024 * 1024 * 3, 'tooLarge' => Yii::t('ad', 'Size should be less then 3MB !!!'),'allowEmpty'=>true),
  33.             // The following rule is used by search().
  34.             // @todo Please remove those attributes that should not be searched.
  35.             array('id, user_id, category_id, title, description, city, phone, photo_url, inactive, blocked, expired_date, deactivated_date', 'safe', 'on'=>'search'),
  36.         );
  37.     }
  38.  
  39.     /**
  40.      * @return array relational rules.
  41.      */
  42.     public function relations()
  43.     {
  44.         // NOTE: you may need to adjust the relation name and the related
  45.         // class name for the relations automatically generated below.
  46.         return array(
  47.             'category' => array(self::BELONGS_TO, 'Categoriestree', 'category_id'),
  48.             'user' => array(self::BELONGS_TO, 'User', 'user_id'),
  49.             'advertisementAudios' => array(self::HAS_MANY, 'AdvertisementAudio', 'advertisement_id'),
  50.             'advertisementComments' => array(self::HAS_MANY, 'AdvertisementComments', 'advertisement_id'),
  51.             'advertisementPhotos' => array(self::HAS_MANY, 'AdvertisementPhoto', 'advertisement_id'),
  52.             'advertisementPromotions' => array(self::HAS_MANY, 'AdvertisementPromotion', 'advertisement_id'),
  53.             'advertisementRatings' => array(self::HAS_MANY, 'AdvertisementRating', 'advertisement_id'),
  54.             'advertisementVideos' => array(self::HAS_MANY, 'AdvertisementVideo', 'advertisement_id'),
  55.         );
  56.     }
  57.  
  58.     /**
  59.      * @return array customized attribute labels (name=>label)
  60.      */
  61.     public function attributeLabels()
  62.     {
  63.         return array(
  64.             'id' => 'ID',
  65.             'user_id' => 'User',
  66.             'category_id' => Yii::t('ad', 'category'),
  67.             'title' => Yii::t('ad', 'title'),
  68.             'description' => Yii::t('ad', 'description'),
  69.             'city' => Yii::t('ad', 'city'),
  70.             'phone' => Yii::t('ad', 'phone'),
  71.             'photo_url' => 'Photo Url',
  72.             'picture' => Yii::t('ad', 'picture'),
  73.             'inactive' => Yii::t('ad', 'inactive'),
  74.             'blocked' => 'Blocked',
  75.             'expired_date' => 'Expired Date',
  76.             'deactivated_date' => 'Deactivated Date',
  77.             'ts' => 'Ts',
  78.         );
  79.     }
  80.  
  81.     /**
  82.      * Retrieves a list of models based on the current search/filter conditions.
  83.      *
  84.      * Typical usecase:
  85.      * - Initialize the model fields with values from filter form.
  86.      * - Execute this method to get CActiveDataProvider instance which will filter
  87.      * models according to data in model fields.
  88.      * - Pass data provider to CGridView, CListView or any similar widget.
  89.      *
  90.      * @return CActiveDataProvider the data provider that can return the models
  91.      * based on the search/filter conditions.
  92.      */
  93.     public function search()
  94.     {
  95.         // @todo Please modify the following code to remove attributes that should not be searched.
  96.  
  97.         $criteria=new CDbCriteria;
  98.  
  99.         $criteria->compare('id',$this->id);
  100.         $criteria->compare('user_id',$this->user_id);
  101.         $criteria->compare('category_id',$this->category_id);
  102.         $criteria->compare('title',$this->title,true);
  103.         $criteria->compare('description',$this->description,true);
  104.         $criteria->compare('city',$this->city,true);
  105.         $criteria->compare('phone',$this->phone,true);
  106.         $criteria->compare('photo_url',$this->photo_url,true);
  107.         $criteria->compare('inactive',$this->inactive);
  108.         $criteria->compare('blocked',$this->blocked);
  109.         $criteria->compare('expired_date',$this->expired_date,true);
  110.         $criteria->compare('deactivated_date',$this->deactivated_date,true);
  111.         $criteria->compare('ts',$this->ts,true);
  112.  
  113.         return new CActiveDataProvider($this, array(
  114.             'criteria'=>$criteria,
  115.         ));
  116.     }
  117.  
  118.     /**
  119.      * Returns the static model of the specified AR class.
  120.      * Please note that you should have this exact method in all your CActiveRecord descendants!
  121.      * @param string $className active record class name.
  122.      * @return Advertisement the static model class
  123.      */
  124.     public static function model($className=__CLASS__)
  125.     {
  126.         return parent::model($className);
  127.     }
  128.  
  129.     public function beforeValidate() {
  130.         $this->user_id = yii::app()->user->id;
  131.         if (!isset($this->blocked)) $this->blocked = 0;
  132.         $this->expired_date = date('Y-m-d', strtotime('+1 year'));
  133.         if (isset($this->inactive) && $this->inactive == 1)
  134.         $this->deactivated_date = date('Y-m-d', strtotime("+183 day"));
  135.         else $this->deactivated_date = '0000-00-00';
  136.         return parent::beforeValidate();
  137.     }
  138.  
  139.     public function getImageUrl() {
  140.         if ($this->photo_url != ''){
  141.             return Yii::app()->getBaseUrl().'/images/ads/'.$this->id.'/'.$this->photo_url;
  142.         }
  143.         else return Yii::app()->getBaseUrl().'/images/images.jpg';
  144.     }
  145.  
  146.     public function getImagethumbUrl() {
  147.         if ($this->photo_url != ''){
  148.             return Yii::app()->getBaseUrl().'/images/ads/'.$this->id.'/thumb_'.$this->photo_url;
  149.         }
  150.         else return Yii::app()->getBaseUrl().'/images/images.jpg';
  151.     }
  152. }
  153.  
  154.  
  155.  
  156. Controller action:
  157.     public function actionIndex() {
  158.         $tab = 'advertisement';
  159.         $model = new Advertisement;
  160.         $getModelByUser = User::model()->findByPk(yii::app()->user->id);
  161.         $allAdbyUser = $getModelByUser->advertisement;
  162.         $dataProvider = new CActiveDataProvider('Advertisement', array(
  163.             'criteria'=>array('condition'=>'user_id=:user_id', 'params'=>array(':user_id'=>Yii::app()->user->id))
  164.         ));
  165.         $this->render('index', array('model' => $model, 'tab' => $tab, 'ads' => $allAdbyUser, 'dataProvider' => $dataProvider));
  166.     }
  167.  
  168. View ad.php:
  169. <?php
  170. $this->widget('TbButton',array(
  171.     'buttonType'=>'link',
  172.     'icon' => 'floppy-save',
  173.     'url' => $this->createUrl('addnewad'),
  174.     'label'=>Yii::t('user_profile','Add new ad'),
  175.     'htmlOptions'=>array('class'=>'adnewad-button', 'style'=>'text-transform: uppercase; border-radius: 0;margin-top:10px;')
  176. ));
  177. ?>
  178.  
  179. <h4><?php echo Yii::t('user_profile','My ads') ?></h4>
  180. <?php
  181.     $this->widget('booster.widgets.TbGridView', array(
  182.     'id' => 'page-creation-grid',
  183.     'dataProvider' => $dataProvider,
  184.     'filter' => $model->search(),
  185.     'columns' => array(
  186.         'category_id',
  187.         'title',
  188.         'expired_date',
  189.         'deactivated_date',
  190.         array(
  191.             'class' => 'TbButtonColumn',
  192.             'buttons' => array(
  193.                 //'view' => array('url' => 'Yii::app()->controller->createUrl("pageview",array("id"=>$data->id))'),
  194.                 'update' => array('url' => 'Yii::app()->controller->createUrl("updatead",array("id"=>$data->id, "page"=>1))'),
  195.                 'delete' => array('url' => 'Yii::app()->controller->createUrl("addelete",array("id"=>$data->id))'),
  196.             )
  197.         ),
  198.     ),
  199.     'htmlOptions' => array(
  200.         //'class' => 'col-xs-12 col-sm-6 col-md-8'
  201.     )
  202. ));
  203. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement