Model
<?php
namespace app\models;
use Yii;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
/**
* This is the model class for table "pesan".
*
* @property int $id
* @property string $barang
* @property string $jumlah
* @property string $pemesan
* @property string $alamat
* @property string $nohp
* @property string $status
* @property string $tanggal
* @property string $gambar
*/
class Pesan extends \yii\db\ActiveRecord
{
public $gambarFile; //
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'pesan';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['tanggal'], 'safe'],
[['barang', 'jumlah', 'pemesan', 'alamat', 'nohp', 'status'], 'string', 'max' => 255],
[['gambarFile'], 'file',
'extensions' => 'jpg,png,jpeg',
'maxSize'=>'256000000',
'skipOnEmpty'=>true,
],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'barang' => 'Barang',
'jumlah' => 'Jumlah',
'pemesan' => 'Pemesan',
'alamat' => 'Alamat',
'nohp' => 'Nohp',
'status' => 'Status',
'tanggal' => 'Tanggal',
'gambarFile' => 'Upload Gambar',
];
}
public function behaviors(){
return [
TimestampBehavior::className(),
BlameableBehavior::className()
];
}
}
Database field
CREATE TABLE `pesan` (
`id` int(11) NOT NULL,
`barang` varchar(255) DEFAULT NULL,
`jumlah` varchar(255) DEFAULT NULL,
`pemesan` varchar(255) DEFAULT NULL,
`alamat` varchar(255) DEFAULT NULL,
`nohp` varchar(255) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`tanggal` date DEFAULT NULL,
`gambar` varchar(255) DEFAULT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`created_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
View
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Barang;
use kartik\select2\Select2;
use kartik\date\DatePicker;
$ar_barang = ArrayHelper::map(Barang::find()->asArray()->all(), 'nama', 'nama');
/* @var $this yii\web\View */
/* @var $model app\models\Pesan */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="pesan-form">
<?php $form = ActiveForm::begin(
['options' => ['ectype' => 'multipart/form-data']]
); ?>
<?php // $form->field($model, 'barang')->textInput(['maxlength' => true])
?>
<?= $form->field($model, 'barang')->widget(Select2::classname(), [
'data' => $ar_barang,
'language' => 'id',
'options' => ['placeholder' => '...Pilih Barang ...'],
'pluginOptions' => [
'allowClear' => true
],
])->label('Barang');
?>
<?= $form->field($model, 'jumlah')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'pemesan')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'alamat')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'nohp')->textInput(['maxlength' => true]) ?>
<?php // $form->field($model, 'status')->textInput(['maxlength' => true])
?>
<?php // $form->field($model, 'tanggal')->textInput()
?>
<?= $form->field($model, 'tanggal')->widget(DatePicker::classname(), [
'language' => 'id',
'options' => ['placeholder' => 'Pilih Tanggal : '],
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'todayHiglight' => true,
'autoclose' => true,
]
]);
?>
<?= $form->field($model, 'gambarFile')->fileInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
view.php
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use app\models\User;
/* @var $this yii\web\View */
/* @var $model app\models\Barang */
$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Pesan', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<?php
$username_created_by = $model->created_by;
if($user=User::findIdentity($model->created_by)){
$username_created_by=$user->username;
}
$username_updated_by = $model->updated_by;
if($user=User::findIdentity($model->updated_by)){
$username_updated_by=$user->username;
}
?>
<div class="barang-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<div class="row">
<div class="col-md-7">
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
// 'gambar',
[
'attribute' => 'created_by',
'value' => $username_created_by,
],
[
'attribute' => 'updated_by',
'value' => $username_updated_by,
],
[
'attribute' => 'created_at',
'format' => ['date', 'php:d-m-Y H:i:s']
],
[
'attribute' => 'updated_at',
'format' => ['date', 'php:d-m-Y H:i:s']
],
],
]) ?>
</div>
<div class="col-md-5">
<center>
<?php
if(!empty($model->gambar)){
?>
<img src="<?= yii::$app->request->baseUrl; ?>
/imgpesan/<?= $model->gambar; ?>" width="50%"/>
<?php
}
else{
?>
<img src="<?= yii::$app->request->baseUrl; ?>
/imgpesan/noimg.png" width="50%"/>
<?php
}
?>
</center>
</div>
</div>
</div>
Controller
<?php
namespace app\controllers;
use Yii;
use app\models\Pesan;
use app\models\PesanSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
* PesanController implements the CRUD actions for Pesan model.
*/
class PesanController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Pesan models.
* @return mixed
*/
public function actionIndex()
{
$x = Yii::$app->user->identity->id;
$searchModel = new PesanSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['created_by'=>$x]); // seleksi data berdasarkan user
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Pesan model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Pesan model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Pesan();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// awal
$model->gambarFile = UploadedFile::getInstance($model, 'gambarFile');
if($model->validate()&& !empty($model->gambarFile)){
// simpan nama file dgn id dan extensi
$nama = $model->id.'.'.$model->gambarFile->extension;
// simpan nama file dgn id dan pada gambar model
$model->gambar = $nama;
// simpan ke dalam Buku
$model->save();
// simpan ke dalam folder img di web/img jgn lupa buat folder
$model->gambarFile->saveAs('imgpesan/'.$nama);
}else{
$model->save();
}
// buka view berdasarkan id
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Pesan model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->gambarFile = UploadedFile::getInstance($model, 'gambarFile');
if($model->validate()&& !empty($model->gambarFile)){
// simpan nama file dgn id dan extensi
$nama = $model->id.'.'.$model->gambarFile->extension;
// simpan nama file dgn id dan pada gambar model
$model->gambar = $nama;
// simpan ke dalam Buku
$model->save();
// simpan ke dalam folder img di web/img jgn lupa buat folder
$model->gambarFile->saveAs('imgpesan/'.$nama);
}else{
$model->save();
}
// buka view berdasarkan id
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Deletes an existing Pesan model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
// $this->findModel($id)->delete();
$model = $this->findModel($id);
unlink('imgpesan/'.$model->gambar);
$model->delete();
return $this->redirect(['index']);
}
public function actionCoba()
{
$x = Yii::$app->user->identity->id;
return $x;
}
/**
* Finds the Pesan model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Pesan the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Pesan::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
0 Response to "tambah gambar yii2"
Posting Komentar