Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Error on update
  • when it was a insert it works, when im updating it show an exception my code:
    elseif(isset($_POST['edit']))
      {
       $val = Validation::factory('noticia');
       $val->set_message('required',  'O campo <strong>:label</strong> é necessário.');
       $val->set_message('min_length', 'O campo <strong>:label</strong> tem que conter no mínimo <strong>:param:1</strong> caracteres.');
       $val->set_message('max_length', 'O campo <strong>:label</strong> não pode conter mais que <strong>:param:1</strong> caracteres.');
       $val->add_field('edit_id',   'ID',    'required');
       $val->add_field('titulo',   'Titulo',    'required|min_length[3]|max_length[255]');
       $val->add_field('pimg',   'Imagem principal', 'min_length[3]'); 
       $val->add_field('descricao',  'Descrição',   'min_length[3]'); 
       $val->add_field('texto',   'Texto',    'required|min_length[5]'); 
       //$val->add_field('inhome',   'Mostrar na página principal', NULL); 
       // run validation on just post
       if ($val->run())
       {
        $entry = Model_Noticias::find(Input::post('edit_id'));
        $entry->username = Auth::get_screen_name();
        $entry->titulo = Input::post('titulo');
        $entry = Input::post('pimg');
        if($img)
        {
         $arr = explode('/', $img);
         $arr = array_reverse($arr);
         $img = $arr[0];
        }
        $entry->pimg = $img;
        $entry->descricao = Input::post('descricao');
        $entry->msg = Input::post('texto');
        $entry->inhome = Input::post('inhome');
        $entry->save();
       }
       else
       {
        foreach($val->errors() as $error)
         $data['errors'] .= $error.'<br />';
        $data['isedit'] = true;
        $data['edit'] = Model_Noticias::find(Input::post('edit_id'));
       }
      }
    

    Error:
    ErrorException [ Error ]: Call to a member function save() on a non-object
    108                $entry->save();
    
  • I thought you couldn't use Input::post('edit_id') in that way because the return value is mixed? Shouldn't it be $edit_id = Input::post('edit_id'); Then $entry = Model_Noticias::find($edit_id);
  • Phil Foulston wrote on Friday 29th of July 2011:
    I thought you couldn't use Input::post('edit_id') in that way because the return value is mixed? Shouldn't it be $edit_id = Input::post('edit_id'); Then $entry = Model_Noticias::find($edit_id);

    no, i try that way still not working, also if it works for delete and insert why not to update? mixed or not it should work
  • I was trying to think of a reason why the object is not created when you do: $entry = Model_Noticias::find(Input::post('edit_id')); You could try a print_r() and see what $entry contains after this line to check that the object is being created and there is not a problem in your model.
  • Or turn on Profiling in config and db or use last_query() to check your query is working as expected.
  • Guys, to both of you, this is something I keep telling people: read the error message! Truly, write it down on a pastie and stick it on the top of your screen. I can't drive this point home enough as it'll save you hours of work if you just read it and understand what it says.
    ErrorException [ Error ]: Call to a member function save() on a non-object
    108 $entry->save();

    This is incredibly strange if the line #108 is the same $entry->save() as you posted in the excerpt. Why is it strange (and even pretty much impossible)? Because you've used the $entry variable as an object in about 10 lines before you ever called save() on it. Thus PHP is telling you that you are calling a method on a non-object, but you've just used it as an object over 10 times before it started complaining..? That should tell you the problem is most likely somewhere else, but you didn't quote the filename, the lines around the problem line or if there were previous errors reported - so I can't tell. Also, when the error reported is that your code is calling a method on a non-object - it most likely has little/nothing to do with the Orm itself. PHP errors aren't nonsense, though they might sometimes show you stuff you don't immediately get, and in a few rare PHP has pretty stupid error messages if you know how much clearer it could be said. But the one you got was pretty clear: "Call to a member function save() on a non-object", you should figure out first how it's possible that a variable you've used many times as an object suddenly isn't an object anymore - that's something the Orm can't actually do by assigning a value to an object property the line before.
  • And after posting I noticed this:
    $entry = Input::post('pimg');
    
    Thus you're overwriting the $entry object you created with a posted value. Still that couldn't cause your error as it would have gotten you into problems way before when you use $entry as an object 6 lines below this quoted statement. Which makes me believe the error is either somewhere else or you edited the code before posting/after getting the error.
  • Not having a good day today :( That's twice I missed the obvious or have been over thinking things today. Simply didn't spot $entry = Input::post('pimg'); either.
  • oh yes, my bad :( the result of working to late... thanks everybody :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion