Manual AR saving and validation erros

Hello, I’m starter with yii, It is amazing but I have some problems hehe.

Here goes my logic:

Imagine an array where each element represents a parking slot.

I need to implement the following logic:

R1: One user cant have more than 4 slots

R2: One user cant have more than 3 groups of non-consecutive slots (for example can have the slots 2,3,9,15 because they are 3 groups g1=2,3 g2=9 and g3=15 but can have for example the 2,7,12,19 (4 groups)

Since the problem is a little more complex my final DB implementation is a table named park_group with:

[id (auto_increment), user_id, start,end]

And in the view the user select the group 1 by 1 choosing the start and end slot.

I have a validation rule in the model for checking the conditions R1 and R2 and work fine

Okey! now the problems ;):

I’m trying to fusion the groups when a new consecutive-to-other group is added. So with the DB:

[1,X,4,4]

When the record [2,X,5,5] is added I want the final status of the database to be:

[3,X,4,5]

So, in the Controller::actionCreate I’m iterate the existing groups (group2) making:




if (conecutive($group1,$group2)){


   $groupFusion=new ParkGroup;

   $groupFusion->start=min($group1->start,$group2->start);

   $groupFusion->end=max($group1->end,$group2->end);

   $group2->delete();

   $groupFusion->save();


}

The problem is that last save() call triggers the validation error R1 in cases like

FUSION: [_,X,2,3] & [_,X,4,5]

the exact error is that cant add the new group with size=4 cant be added because the user already have a group with size=2 (total 6 slots)

Is my approach wrong? or am I missing something?

Okey, my bad =( I was saving twice the same record. An admin delete this post please