Question

I ran into strange situation with Yii ActiveRecord. I have model User (there are some relations, but no foreign keys). When I try to delete some rows from action:

    $cr = new CDbCriteria();
    $cr->addColumnCondition(array(
            'status' => User::USER_STATUS_DELETED
        ));
    $items = User::model()->findAll($cr);
    if(!empty($items)){
        foreach($items as $item){
            $item->delete();
        }
    }

There is no effect. Users are still there. By the way, I can delete them manually with phpmyadmin. More interesting thing - $item->delete() returns true. Where is the problem?

Was it helpful?

Solution

1) Make sure you do not have a function overwriting the delete function
2) if you have a relation, and it is improper created and you are trying to delete a user.id that is related to another record then the delete might fail. (but that should also fail in phpmyadmin)
3) I for example have a soft delete, I just replace the user.status with "deleted". I cannot do that if my model is not validating, so I have to make a ->save(false) to get around that (I actually do not do that but you get my point).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top