2017-08-04 184 views
1

如何在公寓的桌子上加載多張圖片並製作多對多的關係?使用Yii2上傳多張圖片

我有一個模型公寓:

public function getApartmentImages() 
{ 
    return $this->hasMany(ApartmentImages::className(), ['apartment_id' => 'id']); 
} 

public function getImages() 
{ 
    return $this->hasMany(Images::className(), ['id' => 'image_id']) 
     ->via('apartmentImages'); 
} 

模型的圖像:

public function getApartmentImages() 
{ 
    return $this->hasMany(ApartmentImages::className(), ['image_id' => 'id']); 
} 

/** 
* @return \yii\db\ActiveQuery 
*/ 
public function getApartments() 
{ 
    return $this->hasMany(Apartment::className(), ['id' => 'apartment_id']) 
     ->via('apartmentImages'); 
} 

型號ApartmentImages

public function getImage() 
{ 
    return $this->hasOne(Images::className(), ['id' => 'image_id']); 
} 

public function getApartment() 
{ 
    return $this->hasOne(Apartment::className(), ['id' => 'apartment_id']); 
} 

公寓有一個主圖像(佈局)和其他許多人。

我設法使佈局負荷:

public function actionCreate() 
{   
    $model = new Apartment(); 
    $model->load(Yii::$app->getRequest()->getBodyParams(), ''); 
    $layout = new Images(); 
    $layout->imageFile = UploadedFile::getInstanceByName('layout'); 
    if ($layout->upload()) { 
     $model->layout_id = $layout->id; 
    }   
    if ($model->validate()) { 
     if ($model->save()) { 
      $response = Yii::$app->getResponse(); 
      $response->setStatusCode(201); 
      $id = implode(',', array_values($model->getPrimaryKey(true))); 
      $response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $id], true)); 
     } elseif (!$model->hasErrors()) { 
      throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); 
     } 
    } 
    return $model; 
} 

回答

0

我充滿了所有發送的圖片數組,並在循環我上傳到網站,並使用

//INSERT INTO `apartment_images` (`apartment_id`, `image_id`) VALUES (...) 
$model->link('images', $images[0]); 

創建鏈接表中的一個條目。

public function actionCreate() 
{ 
    $model = new Apartment(); 
    $model->load(Yii::$app->getRequest()->getBodyParams(), ''); 
    $layout = new Images(); 
    $layout->imageFile = UploadedFile::getInstanceByName('layout'); 
    $layout->validate(); 
    if ($layout->upload()) { 
     $model->layout_id = $layout->id; 
    } 
    $count = count(UploadedFile::getInstancesByName('images'));//Get the number of images 
    $images = [new Images()];//First image required 
    $images[0]->imageFile = UploadedFile::getInstanceByName('images[0]'); 
    if(!$images[0]->validate()) return $images[0]->errors;//Check errors 
    if ($model->validate()){ 
     if ($model->save()) { 
      if ($images[0]->upload()) { 
       $model->link('images',$images[0]);//Binding many to many 
      } 
      for($i = 1; $i < $count; $i++) {//Check the rest and repeat again 
       $images[$i] = new Images(); 
       $images[$i]->imageFile = UploadedFile::getInstanceByName('images['.$i.']'); 
       if ($images[$i]->upload()) { 
        $model->link('images',$images[$i]); 
       } 
      } 
      $response = Yii::$app->getResponse(); 
      $response->setStatusCode(201); 
      $id = implode(',', array_values($model->getPrimaryKey(true))); 
      $response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $id], true)); 
     } elseif (!$model->hasErrors()) { 
      throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); 
     } 
    } 
    else return $model->errors; 
    return $model; 
}