payment->number != '') { return $this->payment; } $this->trySaving(); return $this->payment; } private function trySaving() { $x = 1; do { try { $this->payment->number = $this->getNextPaymentNumber($this->payment->client, $this->payment); $this->payment->saveQuietly(); $this->completed = false; } catch (QueryException $e) { $x++; if ($x > 50) { $this->completed = false; } } } while ($this->completed); } }