/** * Sets the slug, checking for duplicates and appending a number to make it unique if they exist. */ public function setSlug($slug, $i = 0) { $preExisting = SchoolQuery::create(); if ($this->getId()) { $preExisting->where('School.Id != ?', $this->getId()); } $preExisting->filterBySlug($i ? $slug . $i : $slug); if ($preExisting->findOne()) { $this->setSlug($slug, $i + 1); } else { parent::setSlug($i ? $slug . $i : $slug)->save(); } return $this; }