Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Update/Save, Delete Confusion
  • Im trying to update, delete records but I keep getting errors. I am not sure why, but different trials give me different errors.

    So here, I am fetching some records to delete.

    $links = Model_Links::find('all', array('where'=>array(array('link_id', $set->id))));
    foreach($links as $l) {
    $item = Model_Links::find_one_by('id', $l->id);
    $item->delete();
    }
    I'd like to think I could just do
    $links->delete();

    but this gives me an error of delete not being a method of a non-object.
    The above gives me that same error.

    As far as saving data,
    how do I know if I am adding or saving data?

    In my action_edit method:

    $set = Model_Sets::find($id, array(
    'related'=> array('genre', 'manufacturers')
    ));
    if (Input::method() == 'POST') {
     if($set and $set->save()) {...}
    }

    what condition will be there to prevent a new record from being created vs saving?

    Just trying to understand these basic methods a little better.
    Thanks
    Rich

  • I believe Model_Links::find('all') would return an array of Model_Links.  Therefore $links is an array, not an object, and so $links->delete() does not exist.

    As for the edit vs. create funtionality in save(), there is something going on under the hood, I'm not 100% positive on what it is.  There's something in a model object when it's been fetched that denotes whether or not it's already been created.  My guess would be that it's linked to the primary key.
  • I think you can just do like this:

    $links = Model_Links::find('all', array('where'=>array(array('link_id', $set->id))));
    foreach($links as $l) {
    $item = Model_Links::find_one_by('id', $l->id);
    $l->delete();
    }
  • @ssiangge
    In that instance I get this error:

    Primary key cannot be changed. in /srv/www/cardprices.org/public_html/fuel/packages/orm/classes/model.php on line 1046

    According to the docs:
    Model::find() returns an array of instances (array of  instance variables?)
    ReturnsAn array of instances with the loaded result, null when none found.
    Does using ORM have anything to do with my issues here?
  • Try this:


    $links = Model_Links::query()->where('link_id', $set->id)->get();
    foreach ($links as $l)
    {
    $l->delete();
    }

    I am not sure I understand the 'saving vs inserting' problem. In your code you try to find something by id. When you cannot find anything, it will return null.

    But there is a solution for other cases: you can call the is_new() function.


    Model_Sets::forge()->is_new();

    This will return true.
  • @sagikazarmark

    I am still getting an error:

    Error - 2013-01-17 00:46:23 --> Error - Primary key cannot be changed. in /fuel/packages/orm/classes/model.php on line 1046

    Reviewing the model, this is being generated from the set() method.
    I wonder if my variable set is colliding here.
    ill try changing it.

  • HarroHarro
    Accepted Answer
    The most common cause of this error is either incorrect defined relations (you don't get it on the model itself, but on a related model) or a relation in which you use the PK as FK (for example on a has_one) which is not supported.

Howdy, Stranger!

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

In this Discussion