/** * Одна картинка может принадлежать нескольким объектам. Промежуточная таблица images_of_object * * @return \yii\db\ActiveQuery */ public function getImagesOfObjects() { return $this->hasMany(ImagesOfObject::className(), ['id_image' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getImagesOfObjects() { return $this->hasMany(ImagesOfObject::className(), ['object_id' => 'id', 'label' => 'images_label']); }
public function deleteImage() { /* @var $modelImages \common\models\Images */ /* @var $modelImagesOfObject \common\models\ImagesOfObject */ $paramsImageDeleteData = Json::decode($this->imageData); $modelImages = Images::findOne($paramsImageDeleteData['image_id']); //d([$paramsImageDeleteData]); $modelImagesOfObject = ImagesOfObject::findOne(['image_id' => $paramsImageDeleteData['image_id'], 'object_id' => $paramsImageDeleteData['object_id']]); //dd($modelImagesOfObject); $transaction = Yii::$app->db->beginTransaction(); try { if ($modelImagesOfObject->delete()) { if ($modelImages->delete()) { if ($this->deleteImageFile($modelImages->path) && $this->deleteImageFile($modelImages->path_small_image)) { $transaction->commit(); } } } } catch (Exception $e) { $transaction->rollBack(); } }