0

我是 symfony 框架的新手,我在这里制作了一个 jquery ajax 表单,它运行良好,它还将图像保存到上传文件夹我的问题是它不会将文件名保存到数据库中。我如何在不使用 symfony 表单的情况下传递 id。这是我在 indexSuccess.php 中的代码

 <script>
    $(document).ready(function(){
        $("#loading").hide();
        $('#send_form').ajaxForm({  
            target: '.result',
            beforeSubmit: validate,
            success: function(data) {
              $(".result").html(data);
              $(".result").show();
            }      
        }); 

    });

      function validate(){ 
        var photoValue = $('input[name=files]').fieldValue();  
        if (!photoValue[0]){ 
            alert('Please upload a picture of you.'); 
            return false;
        }    
      }

    </script>

    <div id="sf_admin_content">
      <h2>Company Gallery</h2>
      <br />
      <form id="send_form" action="<?php echo url_for('companyGallery/add'); ?>" method="post">
        <table  id="tbl" class="company" cellspacing="0" style="width:500px;">
          <tr>
            <td><input type="file" name="files" /></td>
          </tr> 
          <tr>
            <td><input type="submit" value="Upload" class="btn btn-success" /></td>
          </tr>
        </table>

      </form>
      <br />
      <div id="loading" >
        <img alt="loading" src="/images/preloader_transparent.gif" />
      </div>
      <div class="result">
      <table  id="tbl" class="company" cellspacing="0" style="width:250px;">
        <thead>
        <tr>
          <th>Id</th>
          <th>Photo</th>
        </tr>
        </thead>
        <tbody>
          <?php foreach($companyGallery as $x => $gallery): ?>
          <tr id="company<?php echo $gallery->getId(); ?>" class="<?php echo ($x&1) ? 'even':'odd'; ?>">
            <td align="right"><?php echo $gallery->getId(); ?></td>
            <td><a href="" target="_blank"><img alt="" src="/uploads/companyGallery/<?php echo $gallery->getImageFull(); ?>" /></a></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
      </div>

在下面的 symfony 代码中

<?php

  class addAction extends sfAction{ 
    private function uploadPhoto($id,$file){
      //echo "test";
      if(!$id){
        $dir = sfConfig::get('app_tempCompGallery_img_dir');
      }else{
        $dir = sfConfig::get('app_companyGallery_dir');
      }

      $full = sfConfig::get('app_companyGallery_full');
      $small = sfConfig::get('app_companyGallery_small');
      $dir = sfConfig::get('app_companyGallery_dir');
      $dir_150 = sfConfig::get('app_companyGallery_dir_150');
      $file['ext'] = getFileExtension($file['name']);
      if(!$id){
        $fileName = uniqid(). '.'.$file['ext'];    
      }else{
        $fileName = $id.'.'.$file['ext'];    
      }
      $imagePath = $dir.$fileName;

      $image_full = encode('CompanyGallery-'.$id).'.'.$fileName;
      $image_small = encode('CompanyGallery-'.$id).'.'.$fileName;
      $image_full_path = $dir.$image_full;
      $image_small_path = $dir_150.$image_small;
      rename($file['tmp_name'],$image_full_path);

      $image = new myImageManipulator($image_full_path);
      $image->resample2($full, $full);
      $image->crop(0, 0, $full, $full);
      $image->save($image_full_path);

      $image = new myImageManipulator($image_full_path);
      $image->resample2($small, $small);
      $image->crop(0, 0, $small, $small);
      $image->save($image_small_path);


      //$companyGallery = $this->getUser()->getAttribute('companyGallery');
      //$this->saveData($companyGallery);
      echo "test345";
      $gallery = Doctrine_Core::getTable('CompanyGallery')->find($id);
      //var_dump($gallery);exit();
      $gallery->setImageFull($image_full);
      $gallery->setImageSmall($image_small);
      $gallery->save();      
    }

    public function execute($request){
      //echo "test macky 123";
      //exit();
      $gallery = new CompanyGallery();
      print_r($gallery);
      $gallery_id = $gallery->getId();
      echo "ASDFAS" .$gallery->getId(); exit();
      $file = $this->request->getFiles();
      print_r($file);
      $file = $file['files'];
      if($file && $file['error'] == 0){
        $this->uploadPhoto($gallery_id,$file);
      }
    }
}

正如您在$gallery = Doctrine_Core::getTable('CompanyGallery')->find($id); 我如何在此代码中传递 id 中注意到的那样?请帮助..以便它将文件名保存到数据库中

这是我在 lib/model 文件夹中的 companyTable.class.php

class CompanyGalleryTable extends Doctrine_Table
{
    /**
     * Returns an instance of this class.
     *
     * @return object CompanyGalleryTable
     */
    public static function getInstance()
    {
        return Doctrine_Core::getTable('CompanyGallery');
    }

    public function getGalleryList($param=array()){
      $q = Doctrine_Query::create()
         ->from('CompanyGallery c')
         ->orderBy('c.updated_at DESC');

      $pager =  new sfDoctrinePager('CompanyGallery',10);
      $pager->setQuery($q);
      $pager->setPage($param['curPage']);
      $pager->init();
      return $pager;

    }
}
4

1 回答 1

1

为什么你需要 Id 来插入。

您应该只创建 CompanyGallery 类的对象,它将记录保存到数据库中。

$画廊=新公司画廊();

于 2013-09-02T05:41:43.887 回答