获取错误未定义属性:stdClass :: $ id

时间:2014-05-07 22:21:00

标签: codeigniter

每当我尝试使用以下代码行添加新页面时,我的CMS都会出现问题

<?php echo form_open_multipart('admin/page/edit/'. $page->id); ?>
它给了我错误 遇到PHP错误

严重性:注意

消息:未定义属性:stdClass :: $ id

文件名:page / edit.php

行号:5

我的编辑功能是执行add&amp; amp;更新功能

public function edit($id = NULL) {
        //Fetch a page or set new one
        if ($id) {
            $this->data['page'] = $this->page_m->get($id);
            count($this->data['page']) || $this->data['errors'][] = 'Page Could not be found';
        } else {
            $this->data['page'] = $this->page_m->get_new();
        }
        $id == NULL || $this->data['page'] = $this->page_m->get($id);
        //Pages for dropdown
        $this->data['pages_no_parents'] = $this->page_m->get_no_parents();
        //dump($this->data['pages_no_parents']);
        //Setup form
        $rules = $this->page_m->rules;

        $this->form_validation->set_rules($rules);
        //Process the form
        if ($this->form_validation->run() == TRUE) {

            $data = $this->page_m->array_from_post(array(
                'title',
                'slug',
                'order',
                'body',
                'template',
                'parent_id',
                'filename'
            ));

            /*             * ***********WORKING FOR IMAGE UPLOAD AND SAVE PATH TO DATABASE*************** */
            if (!empty($_FILES['filename'])) {
                $fdata = $this->do_upload('filename');           /// you are passing the parameter here
                $data['filename'] = base_url() . 'uploads/' . $fdata;
            }
            $this->page_m->save($data, $id);
           // echo '<pre>' . $this->db->last_query() . '</pre>';
            redirect('admin/page');
        }
        //Load the view    
        $this->data['subview'] = 'admin/page/edit';
        $this->load->view('admin/_layout_main', $this->data);
    }
    public function do_upload($field_name) {                        // but not retriveing here do this
        $field_name = 'filename';
        $config = array(
            'allowed_types' => '*',
            'max_size' => '1024',
            'max_width' => '1024',
            'max_height' => '768',
            'upload_path' => './uploads/'
        );
        $this->load->library('upload');
        $this->upload->initialize($config);

        if (!$this->upload->do_upload($field_name)) {
            echo $this->upload->display_errors();
            die();
            $this->data['error'] = array('error' => $this->upload->display_errors());
            //$this->data['subview'] = 'admin/page/edit';
            //$this->load->view('admin/_layout_main', $this->data);
        } else {
            $fInfo = $this->upload->data();
            //return $fInfo['file_path'].$fInfo['file_name'];
            //     $this->filename = $fInfo;
            return $fInfo['file_name'];
        }
    }

1 个答案:

答案 0 :(得分:1)

<?php echo form_open_multipart('admin/page/edit/'. ((isset($page->id)) ? $page->id : '')); ?>

正如我在评论中提到的,如果你正在创建一个新记录(我假设:)你的页面对象还没有id,所以你只需要做一个快速检查以确保它存在,如果没有输出一个空字符串。