CakePHP Değişken boş olur

2 Cevap php
<?php
class UploadsController extends AppController {
var $name = 'Uploads';
var $components = array('Auth');
var $uses = array('Upload');

function beforeFilter() {
    $this->Auth->allow('*');
}

function upload($event) {
    App::import('Vendor', 'UploadedFiles', array('file' => 'UploadedFiles.php'));

    $user = $this->Auth->user('id');
    $this->set('user', $user);
    if(!$this->Auth->user()) { $this->Session->setFlash(__('Please login.', true)); }
    echo $user;
    echo $event;

    $vardir = date('d-m-Y');
    $dir = 'img/gallery/'.$vardir.'/';
    $thmbdir = 'img/gallery/'.$vardir.'/thumbnails/';    
    if(!is_dir($dir)) {
        mkdir($dir, 0777);
        mkdir($thmbdir, 0777);
    }
    $galleryPath = $dir;

    $absGalleryPath = realpath($galleryPath) . '/';
    $absThumbnailsPath = realpath($galleryPath . 'thumbnails/') . '/';

    //Iterate through uploaded data and save the original file, thumbnail, and description.
    while(($file = UploadedFiles::fetchNext()) !== null) {
      $fileName = $file->getSourceFile()->getSafeFileName($absGalleryPath);
      $file->getSourceFile()->save($absGalleryPath . '/' . $fileName);

      $thumbFileName = $file->getThumbnail(1)->getSafeFileName($absThumbnailsPath);
      $file->getThumbnail(1)->save($absThumbnailsPath . '/' . $thumbFileName);

        $this->Upload->create();
        $this->Upload->set(array(
            'name' => $absGalleryPath . $fileName,
            'width' => $file->getSourceFile()->getWidth(),
            'height' => $file->getSourceFile()->getHeight(),
            'description' => $file->getDescription(),
            'event_id' => $event,
            'user_id' => $user
          ));
        $this->Upload->save();
    }
  }
}

Check the last part of the code where I try to save to the database. It doesn't save because $event and $users become empty. But when I echo them (line 17 and 18), they do appear on the page with correct values. It seems that they are being 'erased' in the while loop... If I put some dummy data for 'event_id' => '123' and 'user_id' => '123', the scripts saves successfully. But if I use the variables, it doesn't save. What happens with my variables? Any idea?

Teşekkürler

2 Cevap

Ben senin modelinde bir hata olduğunu tahmin ediyorum. İlk dizi oluşturma ve bunu yazdırmayı deneyin ve doğru bir dizi oluşturur olmadığını görmek. Sonra modeli dizisi, yani uyduğundan emin olun. alan türleri alan türlerini sen giriş eşleşen. Bir dize, ve 123 bir tam sayıdır '123 ''123 yılından beri, 123 gibi aynı değildir'.

ben size değil zaten demek değil ama ben denemek istiyorum bir kaç şeyler düşünebilirsiniz.

  • echo $ user $ ve olay hattı hemen önce ve while döngüsü sonra, sorunlu satırı doğrulamak için.

  • Başka bir geçici değişkene değişkenleri ayarlayın:

$ T_user = $ user;

$ T_event = $ olay;

Daha sonra döngü içinde $ t_ değişkenleri kullanmayı deneyin.