通过小部件向控制器发送值?

时间:2013-12-29 06:54:22

标签: yii yii-extensions yii-booster

我有一个属性模型视图。在这个视图中,有TbFileUpload小部件来上传属性的照片。我正在尝试将property_id从属性视图传递到照片/上传控制器但是未定义property_id错误。

这是我的属性视图:

<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',
))); ?>

照片/上传控制器     

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);
} 
}

有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您遇到了这个问题,因为您尚未将$property_id传递给photo/upload

你可以通过get变量传递它的一种方法。在你看来:

$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',
)));

然后在你的photo/controller抓住它:

$property_id = Yii::app()->request->getParam('id' , null); // or simply: $property_id = $_GET['id'];

然后根据需要使用它。

欢呼声