AddonsOperatingNoteAd.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace addons\OperatingNote\common\models;
  3. use Yii;
  4. use yii\db\Exception;
  5. /**
  6. * This is the model class for table "{{%addons_operating_note_ad}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id 商城id
  10. * @property int $sort 排序
  11. * @property string $title 广告标题
  12. * @property string $img 广告图
  13. * @property string $jump_href 跳转链接
  14. * @property int $status -1是删除,0是禁用,1是启用
  15. * @property int $created_at
  16. * @property int $updated_at
  17. */
  18. class AddonsOperatingNoteAd extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_operating_note_ad}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'sort'], 'required'],
  34. [['mall_id', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  35. [['title'], 'string', 'max' => 50],
  36. [['img'], 'string', 'max' => 255],
  37. [[ 'jump_href'], 'string', 'max' => 5000],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城id',
  48. 'sort' => '排序',
  49. 'title' => '广告标题',
  50. 'img' => '广告图',
  51. 'jump_href' => '跳转链接',
  52. 'status' => '-1是删除,0是禁用,1是启用',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. ];
  56. }
  57. /**
  58. * @throws \yii\db\Exception
  59. */
  60. public static function setData(array $params, string $scenarios = 'create')
  61. {
  62. $transaction = \Yii::$app->db->beginTransaction();
  63. try {
  64. if ($scenarios == 'update') {
  65. $model = self::findOne(['id' => $params['id']]);
  66. } else {
  67. $model = new self();
  68. }
  69. if (!$model->load($params, '') || false === $model->validate()) {
  70. $errors = $model->errors;
  71. $error_str = '';
  72. foreach ($errors as $error) {
  73. $error_str .= implode(',', $error) . ',';
  74. }
  75. $error_str = trim($error_str, ',');
  76. throw new \Exception($error_str);
  77. }
  78. if (!$model->save()) throw new Exception($model->getErrorMessage());
  79. $transaction->commit();
  80. return true;
  81. } catch (Exception $e) {
  82. $transaction->rollBack();
  83. throw new Exception($e->getErrorMessage());
  84. }
  85. }
  86. }