Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
how to update via ORM with multiple where conditions?
  • from the document.
    $entry = Model_Article::find(4);
    $entry->title = 'My first edit';
    $entry->author = 'Total n00b';
    $entry->save();


    but i want to update with these conditions (no PK)
    account_id = $account_id
    field_name = 'someFieldName';

    example:
    $query = Model_AF::query()->where('account_id', $account_id)->where('field_name', $field_name)->get_one();
    $query->field_value = $field_value;
    $query->save();


    from the example it seems to be work some time but when i update multiple rows, it does not change or changed but all rows are same value.

    How to update via ORM with multiple "where" conditions?
  • HarroHarro
    Accepted Answer
    Remember, an ORM is an Object mapper, there is one object for every row in your database table.

    If you want to update a single entry:

    // get_one() returns a single model object, or null
    $object = Model_AF::query()->where('account_id', $account_id)->get_one();
    if ($object)
    {

        $object->field_value = $field_value;
        $
    object->save();
    }

    If you want to update multiple entries:

    // get() returns an array of model objects, or an empty array
    $objects = Model_AF::query()->where('field_name', $field_name)->get();
    foreach ($objects as $object)
    {

        $object->field_value = $field_value;
        $
    object->save();
    }

    The number of where() conditions is not relevant, you can make the most complex query if you want. It's the result type that counts.

    If you have complex multiple updates, and you don't really need ORM objects, it's better to run a DB::update() query. Much less complicated, and much faster then looping over an object array.

Howdy, Stranger!

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

In this Discussion