【问题标题】:regarding passed id to save data in the database using symfony framework关于通过 id 使用 symfony 框架将数据保存在数据库中
【发布时间】:2013-09-02 05:25:25
【问题描述】:

我是 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')-&gt;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;

    }
}

【问题讨论】:

    标签: php jquery symfony


    【解决方案1】:

    为什么插入需要ID。

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

    $gallery = new CompanyGallery();

    【讨论】:

    • 您好,是的!这就是我现在在这里编码的那个 y 这里是空的 $gallery_id = $gallery->getId(); echo "ASDFAS" .$gallery->getId();出口();在 echo $gallery-getId() 中它是空的..
    • 我编辑了公共函数执行然后添加新的 companyGallery() 但 id 为空....
    • 您能否检查您的数据库中是否存在记录。只需使用 $q = Doctrine_Core::getTable('CompanyGallery')->createQuery(); $companyGallery = $q->execute();
    • 我将把代码放在哪里?在你的?我的 companyTable.class.php 中已经有一个查询
    • 嗨刚刚在上面编辑了我在上面添加了 companyTable.class.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    相关资源
    最近更新 更多