Hello. I created model with all necessary properties and relations. I also added such code to my model:

protected function beforeSave() {

        if (empty($this->id) && $existingModel = self::findFirst(array('title' => $this->title))) {

            $this->id = $existingModel->id;

        }

    }

Logic should be: if model with such title exists - update it else - insert new. But I'm receiving Exception: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

Can anybody help me fix this issue?