2

I have this code in my controller:

...
if (Model::validateMultiple($ttepk)) {
    $transaction = \Yii::$app->db->beginTransaction();
    try {
        foreach ($ttepk as $ttep) {
            $ttep->save(false);
            if (!$ttep->assignPs()) {
                throw new UserException('assignPs failed');
            }
        }
        $transaction->commit();
        return $this->redirect(['index']);
    } catch (Exception $ex) {
        $transaction->rollBack();
        throw $ex;
    }
}
...

in model:

...
public function assignPs() {
    foreach (...) {
        $ttepetk[...] = new Ttepet;
        $ttepetk[...]->ttepId = $this->id;
        ... // set other attributes
        }
    }

    if (Model::validateMultiple($ttepetk)) {
        foreach ($ttepetk as $ttepet) {
            $ttepet->save(false);
        }
        return true;
    } else {
        return false;
    }
}
...

Everything is working fine (no inserts are happening if any of the models fail validation), except that I would like to see the exact error, exactly by which Ttep (each Ttep is a model) and by which Ttepet (Ttep:Ttepet = 1:N) has the error happened, and what was that. Now I see the Exeption page only, and I don't know how to make the errors visible. Please point me to the right direction. Thanks!

user2511599
  • 796
  • 1
  • 13
  • 38

1 Answers1

0

You could iterate on each single model validating one by one and getting the errors when occurs ...

if (Model::validateMultiple($ttepetk)) {
    foreach ($ttepetk as $ttepet) {
        $ttepet->save(false);
    }
    return true;
} else {
  foreach($ttepetk as $model){
      if ($model->validate()) {
        // all inputs are valid
    } else {
      // validation failed: $errors is an array containing error messages
      $errors = $model->errors;
    }
  }
    return $errors;  
}

you can get the errors this way

  $myErrorResult = $ttep->assignPs();
  if (!$myErrorResult) {
          ......
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Okay, that's fine, but how to include transactions? – user2511599 Jan 28 '16 at 20:27
  • My attention was on the main function but your question have lead me to your controller code.. and you call a loadMultiple (validation) that is already passed ..this way how can throw the exception .. seems there are some issue with the logic.. so .. my answer is a suggestion for gettings errors like you asked in title .. and i think you should reevealuate your controller code in a way that don't repeat several time the same validation..i hope this is useful for you .. – ScaisEdge Jan 28 '16 at 20:44
  • I think I don't repeat the same validation... but I'm going to try to implement your idea somehow and see what can I achieve. Thx! – user2511599 Jan 29 '16 at 05:04
  • the first is `Model::validateMultiple($ttepk)`, and the second is `Model::validateMultiple($ttepetk)` so corresponding models, and not the same. – user2511599 Jan 29 '16 at 16:15
  • For the transaction you can return the $errors from the function assignPs and mange this result conforming to the Excecption you want throw – ScaisEdge Jan 29 '16 at 16:23
  • I hope this could be useful – ScaisEdge Jan 29 '16 at 16:23
  • I know it's a lame question, but how do I reach $errors in Controller...? – user2511599 Jan 30 '16 at 12:38
  • You can easly get the results form the function and then evaluate – ScaisEdge Jan 30 '16 at 12:48
  • Just like that? So simple? Awesome. Many thanks! I think I can go further now! – user2511599 Jan 30 '16 at 13:04
  • Can I ask one more small lame question? I get either 1 (true?) or an array back with errors. How is the nice way to handle it? Should I do: `if ($ttep->assignPs() <> 1) ...` ? That would look awkward to me. I'm sure there is a more sophisticated way. – user2511599 Jan 30 '16 at 13:17
  • In fact the nicest would be if I would return the objects with error and render the corresponding form of Model Ttepet. I'll give that a try. – user2511599 Jan 30 '16 at 13:55