But i need a activeFileField and mp3 is an id,not a string i will post my model:
Ritmo model
* The followings are the available columns in table 'ritmo':
* @property integer $idritmo
* @property string $artist
* @property string $title
* @property string $price
* @property integer $mp3
* @property integer $categoria
* @property string $youtubeLnk
* @property integer $image
*
* The followings are the available model relations:
* @property EncomendaLinha[] $encomendaLinhas
* @property Document $image
* @property Categoria $categoria
* @property Document $mp3
* @property Teclado[] $teclados
*/
class Ritmo extends CActiveRecord
{...
public function relations()
{
return array(
'encomendaLinhas' => array(self::HAS_MANY, 'EncomendaLinha', 'style'),
'image' => array(self::BELONGS_TO, 'Document', 'image'),
'categoriaR' => array(self::BELONGS_TO, 'Categoria', 'categoria'),
'mp3' => array(self::BELONGS_TO, 'Document', 'mp3'),
'teclados' => array(self::MANY_MANY, 'Teclado', 'ritmo_teclado(idritmo, idteclado)'),
);
}
My Document model:
/**
* This is the model class for table "document".
*
* The followings are the available columns in table 'document':
* @property integer $idbinarios
* @property string $filename
* @property string $content
*
* The followings are the available model relations:
* @property Ritmo[] $ritmos
* @property Ritmo[] $ritmos1
*/
class Document extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Document the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'document';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('idbinarios, filename, content', 'required'),
array('idbinarios', 'numerical', 'integerOnly'=>true),
array('filename', 'length', 'max'=>345),
array('content', 'file'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('idbinarios, filename, content', 'safe', 'on'=>'search'),
);
}
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'content'))
{
$this->filename=$file->name;
$this->content=file_get_contents($file->tempName);
}
return parent::beforeSave();
}....
Ritmo controller:
public function actionUpdate($id) {
$ritmo = $this->loadModel($id);
$mp3=new Document;
$imagem=new Document;
if (isset($_POST['Ritmo'])) {
$ritmo->attributes = $_POST['Ritmo'];
$mp3->save();
}
view:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'ritmo-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<div class="row">
<?php echo $form->labelEx($ritmo,'mp3'); ?>
<?php echo CHtml::activeFileField($mp3,'content',array('size'=>60)) ?>
<?php echo $form->error($ritmo,'mp3'); ?>
</div>
...