One-To-One Relations

I have a model I am trying to link to users so that each user can only have one item in the database, I have setup a one-to-one in the relations() function within the model, however instead of throwing a clean looking error when testing that only one item per user can be created it spits an ugly SQL error along with a stack trace. Am I missing something from the process of establishing the one-to-one relationship or is this how it will always react when it encounters nn attempt to create a second entry?

The rule is being enforced by constraints in the database rather than in Yii. I’m assuming you’re seeing a duplicate primary (or unique) key error based on your description.

If this is the case, you could avoid the situation in Yii by adding a unique validator to the second model:




    public function rules()

    {

        return array(

            ...

            array('id', 'unique', 'message'=>'You can only have one blah blah...'),

            ...

        );

    }