Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 10th, 2012  |  syntax: PHP  |  size: 1.48 KB  |  views: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //a controller
  2. <?php
  3. class ItemController extends CController
  4. {
  5.     public function actionCreate()
  6.     {
  7.         $model=new Item;
  8.         if(isset($_POST['Item']))
  9.         {
  10.             $model->attributes=$_POST['Item'];
  11.             $model->item=CUploadedFile::getInstance($model,'item');
  12.             if($model->save())
  13.             {
  14.                 $model->item->saveAs('assets/media');
  15.                 // redirect to success page
  16.             }
  17.         }
  18.         $this->render('create', array('model'=>$model));
  19.     }
  20. }
  21.  
  22.  
  23.  
  24. //a modell
  25. <?php
  26. class Item extends CActiveRecord
  27. {
  28.     public $item;
  29.    
  30.     public static function model($className=__CLASS__)
  31.     {
  32.             return parent::model($className);
  33.     }
  34.    
  35.     public function tableName()
  36.     {
  37.             return '{{item}}';
  38.     }
  39.    
  40.     public function relations()
  41.     {
  42.             // NOTE: you may need to adjust the relation name and the related
  43.             // class name for the relations automatically generated below.
  44.             return array(
  45.                     'post' => array(self::BELONGS_TO, 'Post', 'pid'),
  46.             );
  47.     }
  48. }
  49.  
  50. //_form a post _form részből
  51.         <div class="row">
  52.             <?php $item = new Item; ?>
  53.             <?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
  54.             <?php echo CHtml::activeFileField($item, 'item'); ?>
  55.         </div>
  56.  
  57.  
  58. //az adatbázisban pedig egy id, egy pid (ez lenne a post id) és egy name van
clone this paste RAW Paste Data