I have a property model view. In this view, there's TbFileUpload widget to upload photos of the property. I'm trying to pass property_id from property view to photo/upload controller but got undefined property_id error.
Here's my property view:
<h1>View Property #<?php echo $model->id; ?></h1>
<!--content begins-->
<!--content ends-->
<div id="prop_description">
<h5 id="prop_hd">Description</h5>
<?php echo $model->description; ?>
</div>
<?php
echo $property_id= $model->id;
$model_photo=new Photo;
$model_photo->property_id = $property_id;
?>
<?php $this->widget('bootstrap.widgets.TbFileUpload', array(
'url' => $this->createUrl("photo/upload"),
'model' => $model_photo,
'attribute' => 'picture', // see the attribute?
'multiple' => true,
'options' => array(
'maxFileSize' => 2000000,
'acceptFileTypes' => 'js:/(\.|\/)(gif|jpe?g|png)$/i',
))); ?>
the photo/upload controller
class PhotoController extends CController {
// ... more code here
/**
* Handles resource upload
* @throws CHttpException
*/
public function actionUpload()
{
//Here we define the paths where the files will be stored temporarily
$path = realpath( Yii::app( )->getBasePath( )."/../images/uploads/tmp/" )."/";
$publicPath = Yii::app( )->getBaseUrl( )."/images/uploads/tmp/";
header('Vary: Accept');
if (isset($_SERVER['HTTP_ACCEPT']) &&
(strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false))
{
header('Content-type: application/json');
} else {
header('Content-type: text/plain');
}
$data = array();
$model_photo = new Photo('upload');
$model_photo->picture = CUploadedFile::getInstance($model_photo, 'picture');
if ($model_photo->picture !== null && $model_photo->validate(array('picture')))
{
// data from file
$model_photo->type = $model_photo->picture->getType( );
$model_photo->size = $model_photo->picture->getSize( );
//(optional) Generate a random name for our file
$filename = md5( Yii::app( )->user->id.microtime( ).$model_photo->name);
$filename .= ".".$model_photo->picture->getExtensionName( );
$model_photo->picture->saveAs($path.$filename);
$model_photo->name = $model_photo->picture->name;
///////////////////// i got the error here ////////////////////////////////
//$model_photo->property_id = $property_id;
$model_photo->url = $publicPath.$filename;
// save picture name
if( $model_photo->save())
{
// return data to the fileuploader
$data[] = array(
//'property_id'=>$model_photo->picture->property_id,
'name' => $model_photo->picture->name,
'type' => $model_photo->picture->type,
'size' => $model_photo->picture->size,
// we need to return the place where our image has been saved
'url' => $publicPath.$filename, // Should we add a helper method?
// we need to provide a thumbnail url to display on the list
// after upload. Again, the helper method now getting thumbnail.
'thumbnail_url' => $publicPath."thumbs/$filename",
// we need to include the action that is going to delete the picture
// if we want to after loading
'delete_url' => $this->createUrl('photo/delete',
array('id' => $model_photo->id, 'method' => 'uploader')),
'delete_type' => 'POST');
} else {
$data[] = array('error' => 'Unable to save model after saving picture');
}
} else {
if ($model_photo->hasErrors('picture'))
{
$data[] = array('error', $model_photo->getErrors('picture'));
} else {
throw new CHttpException(500, "Could not upload file ". CHtml::errorSummary($model_photo));
}
}
// JQuery File Upload expects JSON data
echo json_encode($data);
}
}
any idea to solve this?
you have that problem because you haven't passed $property_id
to your photo/upload
.
one way you could do this you can pass it via get variable. in your view:
$this->widget('bootstrap.widgets.TbFileUpload', array(
'url' => $this->createUrl("photo/upload", array( 'id='=> $model->id )),// I added to the uri
'model' => $model_photo,
'attribute' => 'picture',
'multiple' => true,
'options' => array(
'maxFileSize' => 2000000,
'acceptFileTypes' => 'js:/(\.|\/)(gif|jpe?g|png)$/i',
)));
then in your photo/controller
catch it:
$property_id = Yii::app()->request->getParam('id' , null); // or simply: $property_id = $_GET['id'];
then use it however you need.
cheers