在控制器中调用操作后,全局变量更改为null

时间:2015-07-23 23:51:40

标签: cakephp cakephp-2.3

我想调用delete动作,但我的全局变量设置为null。 控制器:

App::uses('AppController', 'Controller');

class WhitelistController extends AppController
{
public $helpers = array('Html', 'Form');

private $deletes;


public function index()
{
    if ($this->request->is('post'))
    {

        $whitelist = $_POST['data']['whitelist']['stylecodes'];
        if($whitelist == "")
            return $this->Session->setFlash('You did not enter stylecodes. If you send this, it will delete all the shoes from the website.');


        $whitelist = preg_replace('/\s+/', '', $whitelist);
        $whitelist = explode(",", $whitelist);


        $url = 'http://fonda.vps.***********/product_import_json_all_published_products';
        $ch = curl_init($url);
        curl_setopt( $ch, CURLOPT_POST, 1);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
        $all = curl_exec($ch);
        $all = json_decode($all, true);
        print_r($all);

        $this->deletes = array_diff($all, $whitelist);

        if($this->deletes == $all)
            return $this->Session->setFlash('It seems you entered wrong data, what could delete all of the shoes from the website.');


        $this->deletes = array_values($this->deletes);
        pr($this->deletes);

        $this->set('delete', $this->deletes);
        $this->render('delete');
    }

}

public function delete()
{
    if($this->request->is('post'))
    {


    }else{

        $url = 'http://fonda.vps.*******/product_import_json_remove';
        $myvars = 'products=' . json_encode($this->deletes);
        print_r();

        pr($myvars);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        $response = json_decode($response, true);

        pr($response);





        $removed = count($response['removed']);
        $skipped = count($response['skipped']);

        $string = $removed . " shoe(s) removed and " . $skipped . " shoe(s) skipped.";
        $this->Session->setFlash($string);

        if (count($response['error']) != 0) {
            $string = "";
            echo " ";
            foreach ($response['error'] as $key => $value) {
                $string .= $key . ": " . $value;
                $string .= "\n";
            }
            $this->Session->setFlash('Error found in following shoe(s): ' . $string);
        }

        return $this->redirect(array( 'action' => 'index'));

    }
}


}
从视图中调用delete()后,

$ this-> deletes变为null。

我没有看到任何改变删除值的内容。

有什么问题?

1 个答案:

答案 0 :(得分:0)

我想我明白了。我删除了全局变量,并在第一个操作中将局部变量放入会话:

$this->Session->write('deletes', $delete);

我在第二个动作中读到了这个:

$delete = $this->Session->read('deletes');

所以我可以在第二个动作中使用$ delete。