Hi,
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>
<div class="simple">
<?php echo CHtml::activeLabelEx($model,'photo_description'); ?>
<?php echo CHtml::activeTextArea($model,'photo_description',array('rows'=>6, 'cols'=>50)); ?>
</div>
<div class="action">
<?php echo CHtml::submitButton($update ? 'Save' : 'Upload'); ?>
<?php echo CHtml::activeHiddenField($model,'album_id',array('value'=>$_GET['album_id'])); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div>
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.