model() public static méthode

public static model ( null | string $className = __CLASS__ )
$className null | string
 public function loadModel($id)
 {
     if (($model = ProductImage::model()->findByPk($id)) === null) {
         throw new CHttpException(404, 'Страница не найдена');
     }
     return $model;
 }
 public function actionDeleteImage()
 {
     if (Yii::app()->getRequest()->getIsPostRequest() && Yii::app()->getRequest()->getIsAjaxRequest()) {
         $id = (int) Yii::app()->getRequest()->getPost('id');
         $model = ProductImage::model()->findByPk($id);
         if (null !== $model) {
             $model->delete();
             Yii::app()->ajax->success();
         }
     }
     throw new CHttpException(404);
 }
Exemple #3
0
 public function afterDelete()
 {
     // delete dependencies
     ProductAttribute::model()->deleteAll("product_id={$this->cacheId}");
     ProductDescription::model()->deleteAll("product_id={$this->cacheId}");
     ProductDiscount::model()->deleteAll("product_id={$this->cacheId}");
     ProductFilter::model()->deleteAll("product_id={$this->cacheId}");
     ProductImage::model()->deleteAll("product_id={$this->cacheId}");
     ProductOption::model()->deleteAll("product_id={$this->cacheId}");
     ProductOptionValue::model()->deleteAll("product_id={$this->cacheId}");
     ProductRelated::model()->deleteAll("product_id={$this->cacheId}");
     ProductRelated::model()->deleteAll("related_id={$this->cacheId}");
     ProductReward::model()->deleteAll("product_id={$this->cacheId}");
     ProductSpecial::model()->deleteAll("product_id={$this->cacheId}");
     ProductToCategory::model()->deleteAll("product_id={$this->cacheId}");
     ProductToDownload::model()->deleteAll("product_id={$this->cacheId}");
     ProductToLayout::model()->deleteAll("product_id={$this->cacheId}");
     ProductToStore::model()->deleteAll("product_id={$this->cacheId}");
     Review::model()->deleteAll("product_id={$this->cacheId}");
     UrlAlias::model()->deleteAll("query='product_id={$this->cacheId}'");
     parent::afterDelete();
 }
 public function actionDeleteImage()
 {
     $productImageId = Yii::app()->request->getQuery('product_image_id');
     $model = ProductImage::model()->findByPk($productImageId);
     if (empty($model) == false && $model->delete()) {
         echo 1;
     } else {
         echo 0;
     }
 }
 public function actionDeleteAll($id)
 {
     $arrIdNew = explode(",", $id);
     for ($i = 0; $i < count($arrIdNew); $i++) {
         $model = $this->loadModel($arrIdNew[$i]);
         $path = "/../upload/images/";
         $name = $model->image;
         $this->unlink($path, $name);
         if (!empty($model)) {
             $criteria = new CDBCriteria();
             $criteria->addCondition("product_id = {$arrIdNew[$i]}");
             $criteria->select = "id";
             $arrImage = ProductImage::model()->findALl($criteria);
             foreach ($arrImage as $ProductImage) {
                 $modelImage = ProductImage::model()->find($ProductImage->id);
                 $path = "/../upload/images/";
                 $name = $modelImage->image;
                 $this->unlink($path, $name);
                 $modelImage->delete();
             }
         }
         $model->delete();
     }
     Yii::app()->user->setFlash('success', translate('Xóa thành công.'));
     $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
 }
 public function actionDelphoto($id)
 {
     $photo = ProductImage::model()->findByPk($id);
     if ($photo) {
         $photo->delete();
     }
 }
 public function actionDeleteAll($id)
 {
     $arrIdNew = explode(",", $id);
     for ($i = 0; $i < count($arrIdNew); $i++) {
         // Delete all news of category new
         $model = $this->loadModel($arrIdNew[$i]);
         if (!empty($model)) {
             $criteria = new CDBCriteria();
             $criteria->addCondition("product_category_id = {$arrIdNew[$i]}");
             $criteria->select = "id";
             $arrProduct = Product::model()->findALl($criteria);
             foreach ($arrProduct as $product) {
                 $modelProduct = product::model()->find($product->id);
                 // delete all images
                 if (!empty($modelProduct)) {
                     $criteria = new CDBCriteria();
                     $criteria->addCondition("product_id = {$modelProduct->id}");
                     $criteria->select = "id";
                     $arrImage = ProductImage::model()->findALl($criteria);
                     foreach ($arrImage as $ProductImage) {
                         $modelImage = ProductImage::model()->find($ProductImage->id);
                         $path = "/../upload/images/";
                         $name = $modelImage->image;
                         $this->unlink($path, $name);
                         $modelImage->delete();
                     }
                 }
                 $modelProduct->delete();
             }
         }
         // Delete category new
         $model->delete();
     }
     if (!isset($_GET['ajax'])) {
         $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
     }
 }
 /**
  * 删除商品图库中的图片
  */
 public function actionDeleteImage()
 {
     try {
         $status = 200;
         $message = '删除成功!';
         $imageId = intval($_REQUEST['id']);
         if (empty($imageId)) {
             throw new exception('图片id为空,刷新后重试!');
         }
         $info = ProductImage::model()->findByPk($imageId);
         if (empty($info)) {
             throw new exception('图片记录不存在,请刷新后再重试!');
         }
         $imageConfig = Yii::app()->params['image']['product'];
         foreach ($imageConfig['sizes'] as $row) {
             $path = pathinfo($info->img);
             $temp = explode('.', $path['basename']);
             $tempPath = $path['dirname'] . '/' . $temp[0] . '-' . $row . '.' . $temp[1];
             @unlink(ROOT_PATH . $tempPath);
         }
         @unlink(ROOT_PATH . $info->img);
         $flag = ProductImage::model()->deleteByPk($imageId);
         if (empty($flag)) {
             throw new exception('删除失败,刷新后重试!');
         }
     } catch (exception $e) {
         $status = 300;
         $message = '删除失败!【' . $e->getMessage() . '】';
     }
     $result['status'] = $status;
     $result['message'] = $message;
     $result['id'] = $imageId;
     $this->displayJson($result);
 }
 /**
  * Returns the main image for the localization, another localization if none exists, or null if the product has absolutely no image.
  * @return ProductImage main image for localization
  */
 public function getMainImage()
 {
     if ($this->image_id) {
         $productimage = ProductImage::model()->findByPk($this->image_id);
         if ($productimage) {
             return $productimage;
         }
     }
     $params = array('order' => 'position', 'condition' => 'product_id=:product_id', 'params' => array(':product_id' => $this->product_id));
     $productimage = ProductImage::model()->find($params);
     return $productimage;
 }
Exemple #10
0
 /**
  * 获取多个商品的所有图片的(带不同尺寸路径的图片的合集)
  * @param $product_ids
  * @return array|string
  */
 public function getProductImagesByProductIds($product_ids)
 {
     if (empty($product_ids)) {
         return '';
     }
     // $criteria = new CDbCriteria;
     // $criteria->select = '*';
     // $criteria->addInCondition('product_id',$product_ids);
     // $list = ProductImage::model()->findAll($criteria);
     // 已上构造方法也可用
     $list = ProductImage::model()->findAllByAttributes(array('product_id' => $product_ids));
     $data = array();
     if (!empty($list)) {
         foreach ($list as $row) {
             if (isset($data[$row->product_id])) {
                 continue;
             }
             $data[$row->product_id]['images'] = $this->getImageSizesByImageName($row->img);
         }
     }
     return $data;
 }