将变量从控制器传递到模型函数

时间:2013-09-15 12:33:41

标签: php codeigniter model controller

嗨我现在坚持了4天,我已经阅读了大量的相关文章,但似乎什么都没有用。 首先,我将解释我的想法,给你一些代码,然后我会告诉我我尝试过的。 理念。我有2个单独的数据库表'项目'和'图片'。在项目表中,我放置了关于项目的信息(id,title,pubdate,template等),我需要为该项目分配一些图片,所以在图片表上我有“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上的每个解决方案:these非常常见的提示是$this->load->model('upload_m', $id)< - 在function do_something($id){var_dump($id); die;}

  3. 上测试var_dump($ id)时返回null
  4. 我制作了全局变量并试图通过get_class_vars获取它们

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

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

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

  8. 我非常绝望,这花费了我太多的时间。我的目标是将$ id传递给上传函数以设置正确的上传路径并将数据保存到数据库URL(取自upload_path),project_id - &gt;取自传递的$ id。任何帮助将被赞赏,即使它的建议如何改变我的结构,所以我仍然可以实现我的目标。

0 个答案:

没有答案