Update relation records

Hi. I have a problem with update in relation tables. I can’t understand how to do this. Can someone help me? For example I have tables items, categories, items_categories.

For example, here part of code:

$sql = 'SELECT * FROM items WHERE id='. $id;

$item = Items::findBySql($sql)->one();

print_r($item->category); // will get all categories related to this item

if ($item->load(Yii::$app->request->post())) {

	if ($item->validate()) {

		$item->save(); // item data was saved

                // how to do update of categories?


} else {

	return $this->render('item', [

		'item' => $item



Thank you for help.