Using relations in the view

Can i use a relation variable in a form view like:

<?php echo $form->labelEx($ritmo->mp3->filename,'filename'); ?>

$ritmo is the model passed from controller to view , mp3 is the relation name and filename the attribute.

with this i am getting this error:

Call to a member function isAttributeRequired() on a non-object in


<?php echo $form->labelEx($ritmo,'mp3.filename'); ?>


ok that’s fine but now when i try to get the filename by this:

if (isset($_POST['Ritmo'])) {

            $ritmo->attributes = $_POST['Ritmo'];

            $mp3->content=CUploadedFile::getInstance($ritmo, 'mp3.filename');}

i get:

Creating default object from empty value 


You should check before using CUploadFile :D.


In the result print out, you can catch all posted value, it’s the easy debug way (for me)

good luck!

thank can i in the controller have access to relation filename?

anyone knows how to get the filename from the view to controller?

here’s my vardump


  'artist' => string 'General' (length=7)

  'title' => string 'Kizomba1' (length=<img src='' class='bbc_emoticon' alt='8)' />

  'price' => string '6' (length=1)

  'mp3.filename' => string '' (length=0)

  'youtubeLnk' => string '' (length=0)

  'categoria' => string '1' (length=1)

why mp3.filename is empty?

this what i have done recently. not sure if it’s right way but it works.

in control

public function actionCreate() {

	$ritmo = new Ritmo;

	$mp3 = new Mp3;

	if (isset($_POST['Ritmo'])) {




		if ((is_object($upload) && get_class($upload)==='CUploadedFile')) {

			if(!$upload->getHasError()) {

				$upload->saveAs(Yii::app()->params['uploadPath'] . DIRECTORY_SEPARATOR . $mp3->mp3, true);




		if ($ritmo->save()) {

			$mp3->ritmo_id = $ritmo->id;



			$this->redirect(array('view', 'id' => $ritmo->id));



	$this->render('create', array( 'ritmo' => $ritmo, 'mp3' => $mp3));


in view

<?php $form = $this->beginWidget('CActiveForm', array(

	'id' => 'ritmo-form',

//*** check here ***,

	'htmlOptions' => array(

		'enctype' => 'multipart/form-data'




	<div class="row">

	<?php echo $form->labelEx($ritmo,'artist'); ?>

	<?php echo $form->textField($ritmo, 'artist'); ?>

	<?php echo $form->error($ritmo,'artist'); ?>

	</div><!-- row -->

<!--*** and here ***-->

	<div class="row">

	<?php echo $form->label($mp3,'mp3'); ?>

	<?php echo $form->fileField($mp3, 'mp3'); ?>

	<?php echo $form->error($mp3,'mp3'); ?>

	</div><!-- row -->


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()









    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'];





<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(




)); ?>

<div class="row">

		<?php echo $form->labelEx($ritmo,'mp3'); ?>

		<?php echo CHtml::activeFileField($mp3,'content',array('size'=>60))  ?>

		<?php echo $form->error($ritmo,'mp3'); ?>



a little bit confused here.

if MP3 is the parent and ritmo is a child.

then in MP3 model, you should have HAS_ONE relation to ritmo,

then you upload your file in Mp3 model and save related ritmo in aftersave of Mp3 model

seems you’re doing the other way around.

No, ritmo is the parent and mp3 the child(Document),mp3 is the foreign key of the Document Id.Table document has:



content (binary)

if so, relation mp3 in ritmo model should be


and you should have field ritmo_id in mp3 model to link ritmo and mp3 models