Welcome Guest, Not a member yet? Register   Sign In
Model does not load
#1

[eluser]boytun[/eluser]
Hello
In my application I use the hmvc Strucure to create a widget system.
So in my module, and excatly in the controller, I create the frontend (methode edit) and the backend(methode index) of my widget.

this is the code of the controller wich located in the module folder(widg_html):
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Widg_html extends MX_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('mdl_widg_html_page');
  $this->load->model('mdl_widg_html_widget');
  $this->load->model('mdl_widg_html');
}

public function index()
{
  $this->load->model('mdl_widg_html');
  $data['widget_body']=$this->mdl_widg_html->get();
  $this->load->view('display',$data);
   //echo base_url();
}


public function edit($id = NULL )
{
  $this->load->helper('form');
  $this->load->library('form_validation');
  $this->load->library('session');
  
  if($id){
   $this->data['widget']=$this->mdl_widg_html_widget->get($id);
   $this->data['widget_body']=$this->mdl_widg_html->get($id);
   count($this->data['widget']) || $this->data['error'][]="cette widget n'existe pas";
  }
  
  $this->data['values']=$this->mdl_widg_html_page->get();
  $this->data['iswidget']='yes';
  $this->data['subview']='backend';
  $this->data['meta_title']="CMS Hypermedia";

  $rules1=$this->mdl_widg_html_widget->rules;
  $rules2=$this->mdl_widg_html_page->rules;
  $rules3=$this->mdl_widg_html->rules;

  $this->form_validation->set_rules($rules1);
  $this->form_validation->set_rules($rules2);
  $this->form_validation->set_rules($rules3);

  if ($this->form_validation->run() == TRUE)
  {
   $data1=$this->mdl_widg_html_widget->array_from_post(array('position'));
   $data2=$this->mdl_widg_html->array_from_post(array('body'));
  
   $this->mdl_widg_html_widget->save($data1, $id);
   $this->mdl_widg_html->save($data2, $id);
   redirect('admin/widget/');
  }
$this->load->view('admin/layout_main', $this->data);
}

}

/* End of file nofun.php */
/* Location: ./application/modules/nofun/controllers/nofun.php */


the strange problem is, that when I use the edit methode everything (mdl_widg_html also)work fun, but when I want to display the widget in the mvc view(I use index methode wich I load the same model mdl_widg_html) , an error came:

Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined property: Widg_html::$mdl_widg_html

Filename: controllers/widg_html.php

Line Number: 16


Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\application\modules\widg_html\controllers\widg_html.php on line 15

Please, Help me :/
#2

[eluser]TheFuzzy0ne[/eluser]
I don't understand why you would be getting that error. CodeIgniter should tell you if it was unable to load a model, which it doesn't seem to be. Please could you try to load a non-existent model, to ensure it really will tell you when it fails to load a model?

Code:
$this->load->model('non_existent_model');
#3

[eluser]boytun[/eluser]
I get this :

An Error Was Encountered
Unable to locate the model you have specified: non_existent_model
#4

[eluser]boytun[/eluser]
this my model file(mdl_widg_html)

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mdl_widg_html extends MY_Model {
protected $timestamps=TRUE;
public $table_name='widg_html';
protected $primary_key='id';
protected $primary_filter='intval';
protected $order_by='id desc';
public $rules=array(
  'body'=>array('field'=>'body', 'label'=>'Body', 'rules'=>'trim|required')
  );

public function __construct()
{
  parent::__construct();
    
}


}
#5

[eluser]TheFuzzy0ne[/eluser]
I'm sorry, but I have absolutely no idea what's going on there. I can only assume you might be unsetting it somewhere. Does it make any difference if you autoload the models from ./application/config/autoload.php? I'm not suggesting that as a solution, I'm just curious as to where the problem lies.
#6

[eluser]boytun[/eluser]
when I excute the module directly from the url : localhost/widg_html , it works fun.
Very strange.
#7

[eluser]TheFuzzy0ne[/eluser]
Sorry, I think I misunderstood the problem.

If http://localhost/widg_html works, what URL are you using when it doesn't work?
#8

[eluser]boytun[/eluser]
I just call the module like this in the mvc view "display.php":

<?php echo Modules::run('widg_html') ; ?>

so the URL should be localhost
#9

[eluser]TheFuzzy0ne[/eluser]
I'm not familiar with HMVC, although it's something I've been meaning to look into for quite a while now. There may be a simple solution to your problem, but if there is, I don't know what it is.

For now, I'd suggest you add the following to your MX_Controller base class:
Code:
function __get($prop)
{    
    if (property_exists(get_instance(), $prop))
    {
        return get_instance()->$prop;
    }
        
    throw new Exception('Call to undefined property: ' . get_class( $this ) . '::$' . $prop);
}

That might fix your problem. If the requested property can't be found in the current object, the it should pass it back from the CodeIgniter super object if it exists there. If it doesn't, then you'll see an exception. It's up to you whether or not you'd rather use show_error() instead of an exception.
#10

[eluser]boytun[/eluser]
I really appreciate your effort,but the same error




Theme © iAndrew 2016 - Forum software by © MyBB