1

Yiiの外部アクションから親コントローラに値を渡す方法は?

例: 外部アクションは次のようになります。

<?php
 class uploadAction extends CAction
{
/**
* Runs the action.
* This method is invoked by the controller owning this action.
*/
public function run()
{
    .....

        $fileName=$result['filename'];//GETTING FILE NAME

        $this->controller->image = $fileName; // this line does not work!!  

}
  }

親コントローラーで画像の値を取得しようとすると、何も返されません!! どんな助けでも感謝します。

更新:ファイルをアップロードするために拡張機能を使用しています.. eajaxupload .

長い形式があります..多くのフィールドがあり、そのうちの1つは画像です。フォーム全体を送信する前に、その画像を A​​jax でアップロードしたいと考えています。もちろん、ユーザーが作成ボタンをクリックした後..コントローラーは、すべてのフィールドとイメージ名をデータベースに保存する必要があります。景色 ..

<div class="elem">
            <?php echo $form->labelEx($model,'username'); ?>
            <?php echo $form->textField($model,'username',array('class'=>'inputbox grid-11-12','maxlength'=>45)); ?>
            <?php echo $form->error($model,'username'); ?>
        </div>

        <div class="elem">
            <?php echo $form->labelEx($model,'password1'); ?>
            <?php echo $form->passwordField($model,'password1',array('class'=>'inputbox grid-11-12')); ?>
            <?php echo $form->error($model,'password1'); ?>
        </div>

        <div class="elem">
            <?php echo $form->labelEx($model,'password2'); ?>
            <?php echo $form->passwordField($model,'password2',array('class'=>'inputbox grid-11-12')); ?>
            <?php echo $form->error($model,'password2'); ?>
        </div>

        <div class="elem">
            <?php echo $form->labelEx($model,'email'); ?>
            <?php echo $form->textField($model,'email',array('class'=>'inputbox grid-11-12','maxlength'=>45)); ?>
            <?php echo $form->error($model,'email'); ?>
        </div>

        <div class="elem">
            <label for="content">User Image:</label>
        <?php $this->widget('ext.EAjaxUpload.EAjaxUpload',
            array(
            'id'=>'uploadFile',
            'config'=>array(
                   'action'=>Yii::app()->request->baseUrl .'/backend.php/user/upload',
                   'allowedExtensions'=>array("jpg"),//array("jpg","jpeg","gif","exe","mov" and etc...
                   'sizeLimit'=>3*1024*1024,// maximum file size in bytes
                   'minSizeLimit'=>50*1024,// minimum file size in bytes
                   'multiple'=>false,
                   //'onComplete'=>Yii::app()->request->baseUrl .'/backend.php/user/saveStuff/?fn='. "js:function(id, fileName, responseJSON){ alert(fileName); }",
                   //'messages'=>array(
                   //                  'typeError'=>"{file} has invalid extension. Only {extensions} are allowed.",
                   //                  'sizeError'=>"{file} is too large, maximum file size is {sizeLimit}.",
                   //                  'minSizeError'=>"{file} is too small, minimum file size is {minSizeLimit}.",
                   //                  'emptyError'=>"{file} is empty, please select files again without it.",
                   //                  'onLeave'=>"The files are being uploaded, if you leave now the upload will be cancelled."
                   //                 ),
                   //'showMessage'=>"js:function(message){ alert(message); }"
          )
            )); ?>
        </div>

これはコントローラーです..

    <?php

    class UserController extends Controller
    {
/**
 * @var string the default layout for the views. Defaults to '//layouts/column2',     meaning
 * using two-column layout. See 'protected/views/layouts/column2.php'.
 */
public $layout='//layouts/column1';
public $image;

.......


public function actions()
{
    return array(
        'upload' => array(
        'class' => 'ext.actions.uploadAction',
        ),
        );
}

........


public function actionCreate()
{
    $model=new User;
    $profile=new UserProfile;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];
        $profile->attributes=$_POST['UserProfile'];

        if(!$this->saveUser($model, $profile))
            Yii::app()->user->setFlash('error', 'Not Saved :)!');

    }

    $this->render('create',array(
        'model'=>$model,
        'profile'=>$profile,
    ));
}

public function saveUser($model, $profile)
{
    $userValid  = $model->validate();
    $profileValid  = $profile->validate();
    $valid = $userValid && $profileValid;
    if($valid)
    {
        $model->save(false);
        $profile->user_id = $model->id;
        $profile->image = !is_null($this->image)?  $this->image : null; // name of image file which uploaded 
        $profile->save(false);

        Yii::app()->user->setFlash('success', 'Saved :)!');
        $this->redirect(array('index'));
        return true;
    }
    return false;
}

}

外部アクションの詳細は次のとおりです。

 <?php
 class uploadAction extends CAction
 {
/**
* Runs the action.
* This method is invoked by the controller owning this action.
*/
public function run()
{
    Yii::import("ext.EAjaxUpload.qqFileUploader");
        // make the directory to store the pic:
        $folder=Yii::getPathOfAlias('webroot') .'/images/' . $this->controller->id . '/';// folder for uploaded files
        if(!is_dir($folder))
        {
           mkdir($folder);
           chmod($folder, 0755); 
           // the default implementation makes it under 777 permission, which you could possibly change 
            //recursively before deployment, but here's less of a headache in case you don't
        }

        $allowedExtensions = array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
        $sizeLimit = 10 * 1024 * 1024;// maximum file size in bytes
        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
        $result = $uploader->handleUpload($folder);
        $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);

        $fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
        $fileName=$result['filename'];//GETTING FILE NAME

        $this->controller->image = $fileName; // ??????

        echo $return;// it's array



}
}
4

2 に答える 2

0

コールバックを使用して、これと同様のことを行いました。

したがって、コントローラーの actions() 構成には、次の行があります。

'onSuccessCallback' => 'myCallback'

私のアクションクラスでは、 run() の最後に次のものがあります:

call_user_func( array($this->getController(),$this->onSuccessCallback), $fileName);

私が持っているコントローラーで

function myCallback($fileName)
{
this->image = $fileName;
}

次に、コントローラーから次の方法でプロパティにアクセスできます

$this->画像

于 2012-10-06T15:52:44.790 に答える
0

これを行う:

$this->controller->image = $fileName; // this line does not work!!  
$this->controller->renderText($this->controller->image);

これで $fileName が表示されるはずです。

コントローラーのイメージ プロパティにアクセスしようとする場所はどこですか?

リクエスト間で値は保持されません。

于 2012-04-24T07:14:26.210 に答える