0

I'm trying to edit values in a database, and I'm getting the following error:

Call to a member function values() on a non-object

Controller

public function action_options() {

    $this->template->styles = array(
        'style' => 'style',
        'acp' => 'acp'
    );

    $options = ORM::factory('option')->find_all()->as_array('option_name','option_value');

    $this->template->content = View::factory('default/admin/options')
        ->bind('options', $options);
}

    public function action_savesettings() {

    $options = ORM::factory('option')->find_all()->as_array('option_name','option_name');

    $options->values($this->request->post());

    $errors = array();

    try {

        $options->save();

        $this->request->redirect('acp/options');

   } catch (ORM_Validation_Exception $ex) {

       $errors = $ex->errors('validation');

   }

    $this->template->content = View::factory('default/admin/options')
        ->bind('options', $options)
        ->bind('errors', $errors);
}

View

<?php $errors = isset($errors) ? $errors : array(); ?>
<?php echo Form::open('acp/savesettings'); ?>
Site Name: <?php echo Form::input('site_name', $options['site_name'], array('id' => 'acp-sitename')); ?><br />
<?php echo Form::submit('submit', 'Submit', array('id' => 'acp-submit')); ?>
<?php echo Form::close(); ?>

My table is like:

option_id   option_name     option_value

I'm not sure how to approach this, since I'm accessing and editing the values with $options[''].

markerpower
  • 345
  • 1
  • 4
  • 13

2 Answers2

2

You are trying to set values not on Model class instance but on array. You can see that by var_dump($options); before setting values on it.

values() method is the method of ORM class.

Also to get ORM class instance you should call find() method, nor find_all().

find() returns ORM class instance and loads ONE RECORD from database into it. Then you can assign values to it by calling values().

find_all() returns Database_Result class instance - it's a collection of found records. In Kohana you can treat them as an array.

Here is what you need write

$option = ORM::factory('option')->find();

$option->values($this->request->post());

Note that you change ONLY ONE database record. To change more than one record at one database request you will need to call this code for every record or use DB:update() method to create query.

biakaveron
  • 5,493
  • 1
  • 16
  • 20
s.webbandit
  • 16,332
  • 16
  • 58
  • 82
0

As I can see, You want to modify site settings. So, your code should looks like this:

// search for changing option 
$option = ORM::factory('option')->where('option_name', '=', 'site_name')->find();
// set new value
$option->set('option_value', $this->request->post('site_name'));
// update value, handle validation errors, etc
try {...}

If you want to update a few records, you should use Query Builder or DB::query() with a special multiple query.

Community
  • 1
  • 1
biakaveron
  • 5,493
  • 1
  • 16
  • 20