[Solved] Beginner, problem with activeHiddenField


I’m trying to make a simple image webhost using YII.

The database relations are: each user has many albums, each album has many photos.

I’m having problems assigning the right album_id when I upload a new photo.

In the album show, I create a link to call the photo insert, passing the album_id:


[<?php echo CHtml::link('Add photos to this album',array('photos/create', 'album_id'=>$model->album_id)); ?>]


In the photo insert form, I add an hidden field containing the album_id:

<div class="yiiForm">

<?php echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data')); ?>

<?php echo CHtml::errorSummary($model); ?>

<div class="simple">

<?php echo CHtml::label('Browse','photo_image'); ?>

<?php echo CHtml::fileField('photo_image'); ?>


<div class="simple">

<?php echo CHtml::activeLabelEx($model,'photo_description'); ?>

<?php echo CHtml::activeTextArea($model,'photo_description',array('rows'=>6, 'cols'=>50)); ?>


<div class="action">

<?php echo CHtml::submitButton($update ? 'Save' : 'Upload'); ?>

<?php echo CHtml::activeHiddenField($model,'album_id',array('value'=>$_GET['album_id'])); ?>


<?php echo CHtml::endForm(); ?>


In the page source, the hidden field is rendered correctly (showing value="1" for example).

However, when I upload the file, I get this error

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1364 Field 'album_id' doesn't have a default value

It’s like the album_id isn’t inserted in the model.

If I set album_id manually in the beforeValidate function in the model, image gets posted correctly, with all the other values, in the database.

I’m puzzled. Thanks in advance for the replies.

Add ‘album_id’ to Image model’s safeAttributes return array, and welcome to the forums. :)

+1, totally agree with pestaa, he’s wrote his reply faster :)

I don’t believe that the CHtml::Link allows you to put get request through it.

The third array is meant for properties about html tags, such as size="", value="" and the works however I don’t think aside from href="" that a link has any other values so that array is useless to the CHtml link. I’d have to check out the direct declaration to see about it but at any rate here’s what you should be doing:

In your controller before you make the page, create a url like so:

// Create links for the control menu

        $calendar->nextMonthUrl = $this->createUrl('site/calendar', array('month'=>$calendar->nextMonth,'year'=>$calendar->yearNextMonth));

The first field is what action you are directing to (it creates a reference url, based on where the use is - not an absolute one. If you need that then use createAbsoluteUrl().

The second parameter is an array with the "get" request setup the way you were trying to use it in the link you were making.

Now I set this url to a variable part of the model I use so I can pass it to the view through the controller.

Then in your view you’re link would effectively become:

CHtml::link("Next Month >>", $calendar->nextMonthUrl);

Cause if you go back to the definition of link, the first paramater is text (what the link will show), the 2nd is the url - which we created in that stored variable :)

Hope that helps :)

here’s a reference to CHtml::Link --> http://www.yiiframework.com/doc/api/CHtml#link-detail


  • It actually does. (It’s another story that the assignment should be done in the controller.)

  • Who wanted to pass get param to CHtml::link anyway? I can’t see it.

Er, what about target, rel and name?

Right, it works. Thanks a lot.

Actually it works, but I’ll look into your approach because it’s more MVC-oriented. Thanks.