0

嗨,我坚持了 4 天,我已经阅读了大量相关文章,但似乎没有任何效果。首先我会解释我的想法,给你一些代码,然后我会告诉我我尝试了什么。主意。我有 2 个单独的数据库表“项目”和“图片”。在项目表中,我放置了有关项目的信息(id、标题、发布日期、模板等),我需要为该项目分配一些图片,所以在图片表中,我有与这两者相关的“project_id”列。在我的控制器中,我有函数 project_picture_proccess($id) 这个 $id 根据我点击的项目而改变,这就是所有相关图片的获取方式。问题是我无法将当前项目 $id 传递给上传模型(这是用于设置当前项目的上传路径)。这是我的控制器:

   class project extends Admin_Controller{
public function __construct(){
    parent::__construct();
    $this->load->model('project_m');
    $this->load->model('project_pictures_m');

   //here is index() function and some other (not related to my problem)

public function project_picture_proccess($id = NULL){
    //$id variable is from current project im editing
    //and changes according to project id

    $this->load->model('upload_m');

    if ($id) {
        //search for directory in upload path with current project slug name
        //and if there isnt any it will create it and add proper rights
        $this->project_m->search_and_create_dir($id);

        //function get_images is fetching all images that current project has
        $this->data['project_images'] = $this->project_m->get_images($id);

        //this function is setting upload config (setting upload path
        //based on directory created in above)-its working var_dump'ed it
        $this->upload_m->set_upload_config($id);

    }


    if($this->input->post('upload')){
        //do_multi_upload works on multiple images just fine
        //but i cant make it to get outcome of set_upload_config function
        //so i can set upload_path very manualy and everything i upload is in mess
        $this->upload_m->do_multi_upload();

        //redirection function does not work because it is not getting 
        //any project_m data and i have lost all project_m related data in my view
        //its title of project im editing and its id
        redirect('admin/project/project_picture_proccess');
    }


    $this->data['error'] = array('error' => '');
    $this->data['project'] = $this->project_m->get($id);

    //function get_images is fetching all unsorted images uploaded in 
    //the path created above for further proccessing
    $this->data['unsorted_img'] = $this->upload_m->get_unsorted();

    $this->data['subview'] = 'admin/project/picture_proccess';

    $this->load->view('admin/main', $this->data);
}

这是我的模型:

class Upload_m extends MY_model{

protected $pic_path;
protected $_primary_filter = 'intval';
protected $_table_name = 'projects';

function Upload_m(){

    parent::__construct();

    $this->pic_path = realpath(APPPATH . '../img/');

 function get_slug($id = NULL){
    if($id !=NULL){
        $filter = $this->_primary_filter;
        $id = $filter($id);

        $result = mysql_query('SELECT slug FROM ' . $this->_table_name . ' WHERE id=' . $id . ' limit 1');
        $data = mysql_fetch_row($result);

        $name = array_shift($data); //array - need to be string

        return $name;

    }else return;
}
 function set_upload_config($id){
    if($id == NULL) return;
    $slug = $this->get_slug($id);
    $upload_config = array(
        'allowed_types' => 'jpg|gif|jpeg|png|bmp',
        'upload_path' => $this->pic_path . $slug . '/',
        'max_size' => 0
    );
    return $upload_config;
}
 function do_multi_upload(){
    $this->load->library('upload');        

    $this->upload->initialize($this->set_upload_config());
    if($this->upload->do_multi_upload('userfile')){

        var_dump($this->upload->get_multi_upload_data());
        return;
    }else echo "Error at uploading";      
}

这是我尝试过的:

  1. 在 if($id) 范围内移动if($this->input->post...)方法,没有任何内容传递给视图,注释传递给模型

  2. 像 stackoverflow 上的每一个解决方案一样尝试:这些非常常见的提示是$this->load->model('upload_m', $id)<- 在 var_dump($id) 测试时返回 nullfunction do_something($id){var_dump($id); die;}

  3. 我已经制作了全局变量并试图通过 get_class_vars 获取它们

  4. 我已经重写系统/核心/库/模型函数来接受变量do_multi_upload($field<-this is default, $var=NULL<-optional)

  5. 我试图将这些变量放在 __construct() 函数中,例如 __construct($var)

  6. 我试图将该 $id 的名称更改为 $var 并将其放入其中if($this->input->post('upload') && $var === $id)并尝试在模型中使用 $var。这种方法没有奏效

我太绝望了,这花了我太多时间。我的目标是将$id传递给上传函数以设置正确的上传路径并将数据保存到数据库url(取自upload_path),project_id->取自传递的$id。任何帮助将不胜感激,即使它建议如何改变我的结构,以便我仍然可以实现我的目标。

4

0 回答 0