Related models are not being removed (relations many to many)

Hi. I have model with relations many to many and I can't delete releted models. Here is code:

class GroupImages extends Model { public $id;

public function initialize()
{          
    $this->setSource('group_images');
    $this->hasManyToMany('id', 'ImagesOfGroup', 'group_image_id', 'image_id', 'Images', 'id', ['alias' => 'ImagesOfGroup']);        
}

public function setImages($arImages = [])
{
    $this->ImagesOfGroup->delete();
}

}

class ImagesOfGroup extends Model { public $imageid; public $groupimage_id;

public function initialize()
{
    $this->setSource('images_of_group');
    $this->belongsTo('image_id', 'Images', 'id');
    $this->belongsTo('group_image_id', 'GroupImages', 'id');
}

}

I'm trying this $this->ImagesOfGroup->delete() but it is not working. Method delete returns true but when looking through logs there is no sql for delete records.

I think you should traversing a resultset with a foreach to delete each row, because your ImagesOfGroup contains many records but not the single one.

public function setImages($arImages = [])
{
    foreach($this->ImagesOfGroup as $row)
    {
        $row->delete();
    }
}

See more.



321

Thank you for your answer! It is not working too :( I don't understand why your code and my example working with other relations and is not working with many to many.