public function actionPost($userId, $friendId) { if ($userId === $friendId) { throw new Exception('You can not become a friend of yourself'); } $friendship = new Friendship(); $friendship->user_id = $userId; $friendship->friend_id = $friendId; if (!$friendship->validate()) { throw new Exception('Invalid data provided'); } $transaction = Yii::$app->db->beginTransaction(); try { // Mutuality update is handled in Friendship EVENT_BEFORE_INSERT if (!$friendship->save()) { throw new DbException('Could not create friendship'); } $transaction->commit(); } catch (Exception $exception) { $transaction->rollBack(); throw $exception; } $friendshipSummary = $friendship->jsonSummary; return $friendshipSummary; }