• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best method for sending form variables to a model ?

#11
[eluser]Jilani Jidni[/eluser]
Hi Bobby

you have not write which function is called non-object. I think you should check this line
Code:
$this->model->add($cat);

you can set value one model by get set. following code will give you some idea

Code:
class Test_model()
{
        private $name;
    
    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function saveName()
    {
       $cname = $this->getName();
       // save name
    }
}


class Test_controller()
{
     function callModel()
     {
          $this->load->model('Test_model');
          $this->Test_model->setName('bob');
          $this->Test_model->saveName();
      }

}


[quote author="BobbyB" date="1227657911"]Hi folks,
I am trying to pass variables to a model, and tried the above solutions.
But I must be doing sth wrong.

In my controller I got:
Code:
class Freebies extends Controller {

    function Freebies()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {

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

$cat = $this->input->get_post('category', TRUE);

$this->model->add($cat);

$data['freebies'] = $this->Get_freebies->getselect();

$this->load->view('freebies_view', $data);
}
}

It says "Fatal error: Call to a member function on a non-object..."
I am trying to pass a "post" variable from a form to the controller and finally to the model.

Can I pass variables to only a single function(getselect()Wink in a model or will they be passed to all the functions in the model?

Thaks in advance![/quote]

#12
[eluser]BobbyB[/eluser]
Thanks to both of you!
The following line throws the error:
Code:
$this->model->add($cat);

I tried to implement your solutions but did not manage to get it working.
I also tried passing the variable like this:

Code:
//Controller:
$this->load->model('Get_freebies');
$data = array(
    'category' => $this->input->get_post('category', TRUE)
);
$data['freebies'] = $this->Get_freebies->getselect($data);
But this gives me "Undefined variable: data" error in the model.

Mmmhhh?
Can't be that hard to just pass some data to the model can it? Smile


UPDATE:

Got it working now:
Code:
//Controller:
$category = $this->input->get_post('category', TRUE);
$this->load->model('Get_freebies');
$data['freebies'] = $this->Get_freebies->getselect($category);

//Model:
function getselect($category)//I forgot to add "$category to the function
{
...
$this->db->where('category',$category);
...
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.