//a controller
<?php
class ItemController extends CController
{
public function actionCreate()
{
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
$model->item=CUploadedFile::getInstance($model,'item');
if($model->save())
{
$model->item->saveAs('assets/media');
// redirect to success page
}
}
$this->render('create', array('model'=>$model));
}
}
//a modell
<?php
class Item extends CActiveRecord
{
public $item;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{item}}';
}
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'post' => array(self::BELONGS_TO, 'Post', 'pid'),
);
}
}
//_form a post _form részből
<div class="row">
<?php $item = new Item; ?>
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
<?php echo CHtml::activeFileField($item, 'item'); ?>
</div>
//az adatbázisban pedig egy id, egy pid (ez lenne a post id) és egy name van