I'm stuck at a problem where in I need to upload two different files (e.g. one of type jpg and the other of the type pdf/epub) along with the additional form data.
The form data should be uploaded to a database along with the path of the files and the files should be saved inside a directory.
Any help would be appreciated.
BooksController.php
<?php
namespace backend\controllers;
use backend\models\Books;
use Yii;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;
/**
* BooksController implements the CRUD actions for Books model.
*/
class BooksController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Books models.
* #return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Books::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Books model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Books model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Books();
$path = Yii::$app->basePath . '../../uploads/';
if (!is_dir($path)) {
mkdir($path);
}
if (Yii::$app->request->post()) {
$book_file = UploadedFile::getInstance($model, 'book');
$cover_file = UploadedFile::getInstance($model, 'cover');
$book_file->saveAs($path . $book_file->baseName . '.' . $book_file->extension);
$cover_file->saveAs($path . $book_file->baseName . '_' . $cover_file->baseName . '.' . $cover_file->extension);
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', ['model' => $model]);
}
/**
* Updates an existing Books model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Books model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Books model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Books the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Books::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Books.php (Model)
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "books".
*
* #property integer $id
* #property string $title
* #property string $subtitle
* #property string $description
* #property string $author
* #property string $isbn
* #property integer $page
* #property string $year
* #property string $publisher
* #property string $cover
* #property string $link
*/
class Books extends \yii\db\ActiveRecord
{
public $book;
public $cover;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'books';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['title', 'page', 'cover', 'book'], 'required'],
[['description'], 'string'],
[['isbn', 'page'], 'integer'],
['year', 'date'],
[['title', 'subtitle', 'author', 'publisher'], 'string', 'max' => 255],
['book', 'file', 'extensions' => 'pdf, epub', 'maxSize' => 1024 * 1024 * 1024],
['cover', 'file', 'extensions' => 'jpg, jpeg, png', 'maxSize' => 1024 * 1024 * 10]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'subtitle' => 'Subtitle',
'description' => 'Description',
'author' => 'Author',
'isbn' => 'Isbn',
'page' => 'Page',
'year' => 'Year',
'publisher' => 'Publisher',
'cover' => 'Cover',
'book' => 'Book'
];
}
}
_form.php (View)
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model backend\models\Books */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="books-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'subtitle')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'author')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'isbn')->textInput(['maxlength' => 13]) ?>
<?= $form->field($model, 'page')->textInput() ?>
<?= $form->field($model, 'year')->textInput() ?>
<?= $form->field($model, 'publisher')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'book')->fileInput() ?>
<?= $form->field($model, 'cover')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
What exactly doesn't work – saving file into filesystem, o saving model attribute? It looks like you are not calling $model->save() in your actionCreate. Also if model attribute is named file and it is used to handle uploaded file here will be a problem when saving model. Probably you want to save only file name / file path to DB. In this case you need additional attributes to handle those values.
You can use CMultiFileUpload widget. See example in docs. And do not forget to add multipart/form-data to your form:
$form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'htmlOptions' => array( 'enctype' => 'multipart/form-data' ),
));
And file can be processed „along with the additional form data“. Also see CUploadedFile.
Related
I am trying to implement tagged articles for my new small CMS written with yii2.
This is what i have tried https://forum.yiiframework.com/t/how-to-create-tags-for-posts-in-yii2/123890
Everything is working the tagging machanism is fetching data from the tag table but it is not saving data to the table tag_assign.
This is my form.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
//Using for Wysiwig editor
use dosamigos\ckeditor\CKEditor;
//Using for Tagging
use dosamigos\selectize\SelectizeTextInput;
/* #var $this yii\web\View */
/* #var $model common\models\Articles */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="articles-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'content')->widget(CKEditor::className(), [
'options' => ['height' => 800],
'preset' => 'basic',
'clientOptions' => ['height' => 400]
]) ?>
<?= $form->field($model, 'tags')->widget(SelectizeTextInput::className(), [
// calls an action that returns a JSON object with matched
// tags
'loadUrl' => ['tag/list'],
'options' => ['class' => 'form-control'],
'clientOptions' => [
'plugins' => ['remove_button'],
'valueField' => 'name',
'labelField' => 'name',
'searchField' => ['name'],
'create' => true,
],
])->hint('Use commas to separate tags') ?>
<?= $form->field($model, 'date')->textInput() ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
And this is my tag model
<?php
namespace common\models;
use Yii;
use dosamigos\taggable\Taggable;
/**
* This is the model class for table "tags".
*
* #property int $id
* #property string $frequency
* #property string $name
*/
class Tag extends \yii\db\ActiveRecord
{
/**
* {#inheritdoc}
*/
public static function tableName()
{
return 'tags';
}
/**
* {#inheritdoc}
*/
public function rules()
{
return [
[['frequency', 'name'], 'required'],
[['frequency'], 'string', 'max' => 500],
[['name'], 'string', 'max' => 250],
];
}
/**
* {#inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'frequency' => 'Frequency',
'name' => 'Name',
];
}
//For Tagging
public function behaviors() {
return [
[
'class' => Taggable::className(),
],
];
}
public function findAllByName($name)
{
return Tag::find()->where('name LIKE :query')
->addParams([':query'=>"%$name%"])
->all();
}
}
Tag controller.
<?php
namespace backend\controllers;
use Yii;
use common\models\Tag;
use common\models\searchTag;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;
/**
* TagController implements the CRUD actions for Tag model.
*/
class TagController extends Controller
{
/**
* {#inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Tag models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new searchTag();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Tag 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 Tag model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Tag();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
public function actionList($query)
{
$models = Tag::findAllByName($query);
$items = [];
foreach ($models as $model) {
$items[] = ['name' => $model->name];
}
// We know we can use ContentNegotiator filter
// this way is easier to show you here :)
Yii::$app->response->format = Response::FORMAT_JSON;
return $items;
}
/**
* Updates an existing Tag 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()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Tag 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();
return $this->redirect(['index']);
}
/**
* Finds the Tag model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Tag the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Tag::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
And the articles
<?php
namespace common\models;
use Yii;
//For Taggable
use dosamigos\taggable\Taggable;
/**
* This is the model class for table "articles".
*
* #property int $id
* #property string $title
* #property string $content
* #property string $tags
* #property string $date
*/
class Articles extends \yii\db\ActiveRecord
{
//For taggable
public function behaviors() {
return [
[
'class' => Taggable::className(),
],
];
}
/**
* {#inheritdoc}
*/
public static function tableName()
{
return 'articles';
}
/**
* {#inheritdoc}
*/
public function rules()
{
return [
[['title', 'content', 'tags', 'date'], 'required'],
[['content'], 'string'],
[['date'], 'safe'],
[['title', 'tags'], 'string', 'max' => 250],
];
}
/**
* {#inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'content' => 'Content',
'tags' => 'Tags',
'date' => 'Date',
];
}
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('tag_assign', ['article_id' => 'id']);
}
}
What I am missing? How can I trouble shoot at least what is going wrong?
I'm new in Yii 2, and I'm creating an inventory system for the library of my school, but I had a problem.
First, I have a relation like this:
I want to show all the adqs (a unique number for each book - that way we don't need to capture the book again for every copy) of a book in his view, but only the adqs related to the book id obviously at the bottom.
My View:
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\helpers\Url;
use yii\grid\GridView;
/* #var $this yii\web\View */
/* #var $model app\models\Libros */
/* #var $searchModel app\models\LibrosSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = $model->lib_titulo;
$this->params['breadcrumbs'][] = ['label' => 'Libros', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="libros-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Modificar', ['update', 'id' => $model->lib_id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Borrar', ['delete', 'id' => $model->lib_id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<div class="row" align="center">
<?php
if ($model->lib_image_web_filename!='') {
echo '<br /><p><img width="500" src="'.Url::to('#web/', true). '/uploads/libros/'.$model->lib_image_web_filename.'"></p>';
}
?>
</div>
<div class="row" style="
text-align: center;
font: normal normal bold 15px/1 'lato';
color: rgba(7,7,7,1);
text-align: center;
">
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'lib_id',
'lib_titulo',
'lib_autor',
//'lib_edicion',
'editorialNombre',
'ubicacionNombre',
'lib_isbn',
'lib_clasificacion',
'lib_seccion',
//'lib_image_src_filename',
//'lib_image_web_filename',
],
]) ?>
</div>
</div>
My Controller (LibrosController):
<?php
namespace backend\controllers;
use Yii;
use app\models\Libros;
use app\models\LibrosSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
* LibrosController implements the CRUD actions for Libros model.
*/
class LibrosController extends Controller
{
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Libros models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new LibrosSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Libros model.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionCreate()
{
$model = new Libros();
if ($model->load(Yii::$app->request->post())) {
$image = UploadedFile::getInstance($model, 'image');
if (!is_null($image)) {
$model->lib_image_src_filename = $image->name;
$ext = end((explode(".", $image->name)));
// generate a unique file name to prevent duplicate filenames
$model->lib_image_web_filename = Yii::$app->security->generateRandomString().".{$ext}";
// the path to save file, you can set an uploadPath
// in Yii::$app->params (as used in example below)
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/uploads/libros/';
$path = Yii::$app->params['uploadPath'] . $model->lib_image_web_filename;
$image->saveAs($path);
}
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->lib_id]);
} else {
var_dump ($model->getErrors()); die();
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Libros 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())) {
$image = UploadedFile::getInstance($model, 'image');
if (!is_null($image)) {
$model->lib_image_src_filename = $image->name;
$ext = explode(".", $image->name);
$ext_final = end($ext);
// generate a unique file name to prevent duplicate filenames
$model->lib_image_web_filename = Yii::$app->security->generateRandomString().".{$ext_final}";
// the path to save file, you can set an uploadPath
// in Yii::$app->params (as used in example below)
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/uploads/libros/';
$path = Yii::$app->params['uploadPath'] . $model->lib_image_web_filename;
$image->saveAs($path);
}
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->lib_id]);
} else {
var_dump ($model->getErrors()); die();
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Deletes an existing Libros 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();
return $this->redirect(['index']);
}
/**
* Finds the Libros model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Libros the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Libros::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
My Model (Libros.php):
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "libros".
*
* #property int $lib_id
* #property string $lib_titulo
* #property string $lib_autor
* #property string $lib_edicion
* #property int $lib_ubi
* #property int $lib_editorial_id
* #property string $lib_isbn
* #property string $lib_clasificacion
* #property string $lib_seccion
* #property string $lib_image_src_filename
* #property string $lib_image_web_filename
*
* #property Adq[] $adqs
* #property Editorial $libEditorial
* #property Ubicacion $libUbi
* #property LibrosCarreras[] $librosCarreras
* #property Carreras[] $licCarreras
*/
class Libros extends \yii\db\ActiveRecord
{
const PERMISSIONS_PRIVATE = 10;
const PERMISSIONS_PUBLIC = 20;
public $image;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'libros';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['lib_titulo', 'lib_autor', 'lib_ubi', 'lib_isbn', 'lib_clasificacion', 'lib_seccion', 'lib_image_src_filename', 'lib_image_web_filename'], 'required'],
[['lib_ubi', 'lib_editorial_id'], 'integer'],
[['lib_titulo'], 'string', 'max' => 120],
[['lib_autor', 'lib_clasificacion'], 'string', 'max' => 64],
//[['lib_edicion'], 'string', 'max' => 3],
[['lib_isbn'], 'string', 'max' => 32],
[['lib_seccion'], 'string', 'max' => 2],
[['lib_editorial_id'], 'exist', 'skipOnError' => true, 'targetClass' => Editorial::className(), 'targetAttribute' => ['lib_editorial_id' => 'edi_id']],
[['lib_ubi'], 'exist', 'skipOnError' => true, 'targetClass' => Ubicacion::className(), 'targetAttribute' => ['lib_ubi' => 'ubil_id']],
[['lib_image_src_filename', 'lib_image_web_filename'], 'string', 'max' => 100],
[['image'], 'safe'],
[['image'], 'file', 'extensions'=>'jpg, gif, png'],
[['image'], 'file', 'maxSize'=>'1000000'],
[['lib_image_src_filename', 'lib_image_web_filename'], 'string', 'max' => 255], ];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'lib_id' => 'ID',
'lib_titulo' => 'Titulo',
'lib_autor' => 'Autor',
//'lib_edicion' => 'Edicion',
'lib_editorial_id' => 'Editorial',
'lib_isbn' => 'ISBN',
'lib_clasificacion' => 'Clasificacion',
'lib_ubi' => 'Ubicacion',
'lib_seccion' => 'Seccion',
'image' => 'Captura',
'lib_image_src_filename' => Yii::t('app', 'Nombre de Archivo'),
'lib_image_web_filename' => Yii::t('app', 'Nombre del Directorio'),
'editorialNombre' => 'Editorial',
'ubicacionNombre' => 'Ubicacion',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getAdqs()
{
return $this->hasMany(Adq::className(), ['adq_libro_id' => 'lib_id']);
}
/**
* #return \yii\db\ActiveQuery
*/
/**
* #return \yii\db\ActiveQuery
*/
public function getLibrosCarreras()
{
return $this->hasMany(LibrosCarreras::className(), ['lic_libros_id' => 'lib_id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getEditorial0()
{
return $this->hasOne(Editorial::className(), ['edi_id' => 'lib_editorial_id']);
}
public function getEditorialNombre()
{
return $this->editorial0->edi_nombre;
}
public function getLibUbi()
{
return $this->hasOne(Ubicacion::className(), ['ubil_id' => 'lib_ubi']);
}
public function getUbicacionNombre()
{
return $this->libUbi->ubil_nombre; }
}
My Search Model (LibrosSearch.php):
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Libros;
/**
* LibrosSearch represents the model behind the search form of `app\models\Libros`.
*/
class LibrosSearch extends Libros
{
public $editorialNombre;
public $ubicacionNombre;
/**
* #inheritdoc
*/
public function rules()
{
return [
[['lib_id', 'lib_ubi', 'lib_editorial_id'], 'integer'],
[['lib_titulo', 'lib_autor', 'lib_isbn', 'lib_clasificacion', 'lib_seccion', 'lib_image_src_filename', 'lib_image_web_filename'], 'safe'],
[['editorialNombre'],'safe'],
[['ubicacionNombre'],'safe'],
];
}
/**
* #inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* #param array $params
*
* #return ActiveDataProvider
*/
public function search($params)
{
$query = Libros::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes'=>[
'editorialNombre'=>[
'asc'=>['editorial.edi_nombre'=>SORT_ASC],
'desc'=>['editorial.edi_nombre'=>SORT_DESC],
'label'=>'Editorial Nombre'
]
]
]);
$dataProvider->setSort([
'attributes'=>[
'ubicacionNombre'=>[
'asc'=>['ubicacion.ubil_nombre'=>SORT_ASC],
'desc'=>['ubicacion.ubil_nombre'=>SORT_DESC],
'label'=>'Ubicacion Nombre'
]
]
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'lib_id' => $this->lib_id,
'lib_ubi' => $this->lib_ubi,
'lib_editorial_id' => $this->lib_editorial_id,
]);
$query->andFilterWhere(['like', 'lib_titulo', $this->lib_titulo])
->andFilterWhere(['like', 'lib_autor', $this->lib_autor])
//->andFilterWhere(['like', 'lib_edicion', $this->lib_edicion])
->andFilterWhere(['like', 'lib_isbn', $this->lib_isbn])
->andFilterWhere(['like', 'lib_clasificacion', $this->lib_clasificacion])
->andFilterWhere(['like', 'lib_seccion', $this->lib_seccion])
->andFilterWhere(['like', 'lib_image_src_filename', $this->lib_image_src_filename])
->andFilterWhere(['like', 'lib_image_web_filename', $this->lib_image_web_filename]);
$query->joinWith(['editorial0'=>function($q)//creamos un nuevo filtro
{
$q->where('editorial.edi_nombre LIKE "%' . $this->editorialNombre . '%"');
}]);
$query->joinWith(['libUbi'=>function($q)//creamos un nuevo filtro
{
$q->where('ubicacion.ubil_nombre LIKE "%' . $this->ubicacionNombre . '%"');
}]);
return $dataProvider;
}
}
How can I accomplish this task?
[
'attribute' => 'adq_libro_id',
'value' => implode(', ', \yii\helpers\ArrayHelper::map($model->Adqs, 'id',
function ( $model )
{
return $model['adq'];
}
)),
'format' => 'raw'
],
try like this. Check the right name of attributes
In your model Libros.php you have this function.
public function getAdqs()
{
return $this->hasMany(Adq::className(), ['adq_libro_id' => 'lib_id']);
}
This means you can access the relations of Libros like:
// where $model is an instance of Libros
$model->adqs->property_name
Or as a function
$model->getAdqs();
I'm using yii2 advanced app and im stuck at a point where my form doesn't get submitted. It refreshes and stays on the same page. There are no errors shown too.
Here is the model code Countries.php
<?php
namespace backend\models\base;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
use mootensai\behaviors\UUIDBehavior;
/**
* This is the base model class for table "countries".
*
* #property integer $id
* #property string $sortname
* #property string $name
* #property integer $phonecode
* #property integer $created_at
* #property integer $updated_at
* #property integer $created_by
* #property integer $updated_by
* #property integer $deleted_at
* #property integer $deleted_by
*
* #property \backend\models\States[] $states
*/
class Countries extends \yii\db\ActiveRecord
{
use \mootensai\relation\RelationTrait;
private $_rt_softdelete;
private $_rt_softrestore;
public function __construct(){
parent::__construct();
$this->_rt_softdelete = [
'deleted_by' => \Yii::$app->user->id,
'deleted_at' => date('Y-m-d H:i:s'),
];
$this->_rt_softrestore = [
'deleted_by' => 0,
'deleted_at' => date('Y-m-d H:i:s'),
];
}
/**
* This function helps \mootensai\relation\RelationTrait runs faster
* #return array relation names of this model
*/
public function relationNames()
{
return [
'states'
];
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['sortname', 'name', 'phonecode'], 'required'],
[['phonecode', 'created_at', 'updated_at', 'created_by', 'updated_by', 'deleted_at', 'deleted_by'], 'integer'],
[['sortname'], 'string', 'max' => 3],
[['name'], 'string', 'max' => 150],
[['lock'], 'default', 'value' => '0'],
[['lock'], 'mootensai\components\OptimisticLockValidator']
];
}
/**
* #inheritdoc
*/
public static function tableName()
{
return 'countries';
}
/**
*
* #return string
* overwrite function optimisticLock
* return string name of field are used to stored optimistic lock
*
*/
public function optimisticLock() {
return 'lock';
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'sortname' => Yii::t('app', 'Sortname'),
'name' => Yii::t('app', 'Name'),
'phonecode' => Yii::t('app', 'Phonecode'),
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getStates()
{
return $this->hasMany(\backend\models\States::className(), ['country_id' => 'id']);
}
/**
* #inheritdoc
* #return array mixed
*/
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new \yii\db\Expression('NOW()'),
],
'blameable' => [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'created_by',
'updatedByAttribute' => 'updated_by',
],
'uuid' => [
'class' => UUIDBehavior::className(),
'column' => 'id',
],
];
}
/**
* #inheritdoc
* #return \backend\models\query\CountriesQuery the active query used by this AR class.
*/
public static function find()
{
$query = new \backend\models\query\CountriesQuery(get_called_class());
return $query->where(['countries.deleted_by' => 0]);
}
}
And the controller CountriesController.php
<?php
namespace backend\controllers;
use Yii;
use backend\models\Countries;
use backend\models\search\CountriesSearch;
use backend\controllers\BackendController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* CountriesController implements the CRUD actions for Countries model.
*/
class CountriesController extends BackendController
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['index', 'view', 'create', 'update', 'delete', 'pdf', 'save-as-new','add-states'],
'roles' => ['admin']
],
[
'allow' => false
]
]
]
];
}
/**
* Lists all Countries models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new CountriesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Countries model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
$providerStates = new \yii\data\ArrayDataProvider([
'allModels' => $model->states,
]);
return $this->render('view', [
'model' => $this->findModel($id),
'providerStates' => $providerStates,
]);
}
/**
* Creates a new Countries model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Countries();
if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Countries model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
if (Yii::$app->request->post('_asnew') == '1') {
$model = new Countries();
}else{
$model = $this->findModel($id);
}
if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Countries model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->deleteWithRelated();
return $this->redirect(['index']);
}
/**
*
* Export Countries information into PDF format.
* #param integer $id
* #return mixed
*/
public function actionPdf($id) {
$model = $this->findModel($id);
$providerStates = new \yii\data\ArrayDataProvider([
'allModels' => $model->states,
]);
$content = $this->renderAjax('_pdf', [
'model' => $model,
'providerStates' => $providerStates,
]);
$pdf = new \kartik\mpdf\Pdf([
'mode' => \kartik\mpdf\Pdf::MODE_CORE,
'format' => \kartik\mpdf\Pdf::FORMAT_A4,
'orientation' => \kartik\mpdf\Pdf::ORIENT_PORTRAIT,
'destination' => \kartik\mpdf\Pdf::DEST_BROWSER,
'content' => $content,
'cssFile' => '#vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => '.kv-heading-1{font-size:18px}',
'options' => ['title' => \Yii::$app->name],
'methods' => [
'SetHeader' => [\Yii::$app->name],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();
}
/**
* Creates a new Countries model by another data,
* so user don't need to input all field from scratch.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* #param mixed $id
* #return mixed
*/
public function actionSaveAsNew($id) {
$model = new Countries();
if (Yii::$app->request->post('_asnew') != '1') {
$model = $this->findModel($id);
}
if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('saveAsNew', [
'model' => $model,
]);
}
}
/**
* Finds the Countries model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Countries the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Countries::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
/**
* Action to load a tabular form grid
* for States
* #author Yohanes Candrajaya <moo.tensai#gmail.com>
* #author Jiwantoro Ndaru <jiwanndaru#gmail.com>
*
* #return mixed
*/
public function actionAddStates()
{
if (Yii::$app->request->isAjax) {
$row = Yii::$app->request->post('States');
if((Yii::$app->request->post('isNewRecord') && Yii::$app->request->post('_action') == 'load' && empty($row)) || Yii::$app->request->post('_action') == 'add')
$row[] = [];
return $this->renderAjax('_formStates', ['row' => $row]);
} else {
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
}
and the view file create.php
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model backend\models\Apps */
$this->title = Yii::t('app', 'Create Apps');
?>
<div class="uk-container uk-container-small uk-position-relative">
<div><!----> <div>
<h1 id="navbar" class="uk-h2 tm-heading-fragment">
Apps
</h1>
<!-- Start Breadcrumb -->
<ul class="uk-breadcrumb">
<li><?= Html::a('Admin', ['/'])?></li>
<li><?= Html::a('Apps', ['/apps'])?></li>
<li>Create</li>
</ul>
<!-- End Breadcrumb -->
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
and view _form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use alexeevdv\widget\SluggableInputWidget;
use dosamigos\ckeditor\CKEditor;
/* #var $this yii\web\View */
/* #var $model backend\models\Apps */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="uk-margin-auto">
<?php $form = ActiveForm::begin(); ?>
<?= $form->errorSummary($model); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true, 'placeholder' => 'Title']) ?>
<?= $form->field($model, 'slug')->widget(SluggableInputWidget::className(), [
'dependsOn' => 'title',
]); ?>
<?= $form->field($model, 'content')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'basic',
'clientOptions' => [
'filebrowserImageBrowseUrl' => yii\helpers\Url::to(['imagemanager/manager', 'view-mode'=>'iframe', 'select-type'=>'ckeditor']),
]
]);?>
<?= $form->field($model, 'video')->textInput(['maxlength' => true, 'placeholder' => 'Video']) ?>
<?= $form->field($model, 'category')->widget(\kartik\widgets\Select2::classname(), [
'data' => \yii\helpers\ArrayHelper::map(\backend\models\Categories::find()->orderBy('id')->asArray()->all(), 'id', 'name'),
'options' => ['placeholder' => Yii::t('app', 'Choose a category')],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'status')->textInput(['placeholder' => 'Status']) ?>
<div class="form-group">
<?php if(Yii::$app->controller->action->id != 'save-as-new'): ?>
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn uk-button uk-button-primary' : 'btn uk-button uk-button-primary']) ?>
<?php endif; ?>
<?php if(Yii::$app->controller->action->id != 'create'): ?>
<?= Html::submitButton(Yii::t('app', 'Save As New'), ['class' => 'btn uk-button uk-button-default', 'value' => '1', 'name' => '_asnew']) ?>
<?php endif; ?>
<?= Html::a(Yii::t('app', 'Cancel'), Yii::$app->request->referrer , ['class'=> 'btn uk-button uk-button-danger']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I've browsed few articles, but of no help. My form has a <?php ActiveForm::end(); ?> at the end of the form. I also tried to remove the select2 widget but i still get the same issue. Could someone help me out why this is happening?
Html output rendered of create.php
<div class="uk-container uk-container-small uk-position-relative">
<div><!----> <div>
<h1 id="navbar" class="uk-h2 tm-heading-fragment">
Countries
</h1>
<!-- Start Breadcrumb -->
<ul class="uk-breadcrumb">
<li>Admin</li>
<li>Countries</li>
<li>Create</li>
</ul>
<!-- End Breadcrumb -->
<div class="uk-margin-auto">
<form id="w0" action="/final/backend/en-us/countries/create" method="post">
<input type="hidden" name="_csrf" value="zIvM0awWY3XpcrF1kY6gFY00ghnL1cgwTYhaEqF7RO2hch4x5AznMwtfWxWp4D-YW9yJy5aiNupb0JnMSbi5qQ==">
<div class="error-summary" style="display:none"><p>Please fix the following errors:</p><ul></ul></div>
<div class="form-group field-countries-id">
<input type="text" id="countries-id" class="form-control" name="Countries[id]" style="display:none">
</div>
<div class="form-group field-countries-sortname required">
<label class="control-label" for="countries-sortname">Sortname</label>
<input type="text" id="countries-sortname" class="form-control" name="Countries[sortname]" maxlength="3" placeholder="Sortname" aria-required="true">
<div class="help-block"></div>
</div>
<div class="form-group field-countries-name required">
<label class="control-label" for="countries-name">Name</label>
<input type="text" id="countries-name" class="form-control" name="Countries[name]" maxlength="150" placeholder="Name" aria-required="true">
<div class="help-block"></div>
</div>
<div class="form-group field-countries-phonecode required">
<label class="control-label" for="countries-phonecode">Phonecode</label>
<input type="text" id="countries-phonecode" class="form-control" name="Countries[phonecode]" placeholder="Phonecode" aria-required="true">
<div class="help-block"></div>
</div>
<div id="w3-container" class=" tabs-above tab-align-left tabs-krajee"><ul id="w3" class="nav nav-tabs nav nav-tabs hidden-print" data-krajee-tabsx="tabsX_0b4b2adf" role="tablist"><li class="active"><i class="glyphicon glyphicon-book"></i> States</li></ul>
<div class="tab-content printable"><div class="h3 visible-print-block"><i class="glyphicon glyphicon-book"></i> States</div>
<div id="w3-tab0" class="tab-pane fade in active"><div class="form-group" id="add-states">
<div id="w1" class="grid-view hide-resize" data-krajee-grid="kvGridInit_7fee31f2"><div class="panel panel-default">
<div class="rc-handle-container" style="width: 628px;"><div class="rc-handle" style="left: 50px; height: 37px;"></div><div class="rc-handle" style="left: 496px; height: 37px;"></div></div><div id="w1-container" class="table-responsive kv-grid-container"><table class="kv-grid-table table table-hover kv-table-wrap"><thead>
<tr><th class="kv-align-center kv-align-middle" style="width: 7.96%;" data-col-seq="0">#</th><th class="kv-align-top kv-grid-hide" data-col-seq="1">Id</th><th class="kv-align-top" data-col-seq="2" style="width: 71.02%;">Name</th><th class="kv-align-middle" data-col-seq="3" style="width: 21.02%;"></th></tr>
</thead>
<tbody>
<tr><td colspan="4"><div class="empty">No results found.</div></td></tr>
</tbody></table></div>
<div class="kv-panel-after"><button type="button" class="btn btn-success kv-batch-create" onclick="addRowStates()"><i class="fa fa-plus"></i>Add States</button></div>
</div></div> </div>
</div>
</div></div> <div class="form-group">
<button type="submit" class="uk-button uk-button-primary">Create</button> <a class="uk-button uk-button-danger">Cancel</a> </div>
</form>
</div>
</div>
</div>
</div>
Changing loadAll() and saveAll() to load() and save() has solved the issue
<?php
namespace backend\controllers;
use Yii;
use backend\models\Countries;
use backend\models\search\CountriesSearch;
use backend\controllers\BackendController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* CountriesController implements the CRUD actions for Countries model.
*/
class CountriesController extends BackendController
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['index', 'view', 'create', 'update', 'delete', 'pdf', 'save-as-new','add-states'],
'roles' => ['admin']
],
[
'allow' => false
]
]
]
];
}
/**
* Lists all Countries models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new CountriesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Countries model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
$providerStates = new \yii\data\ArrayDataProvider([
'allModels' => $model->states,
]);
return $this->render('view', [
'model' => $this->findModel($id),
'providerStates' => $providerStates,
]);
}
/**
* Creates a new Countries model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Countries();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Countries model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
if (Yii::$app->request->post('_asnew') == '1') {
$model = new Countries();
}else{
$model = $this->findModel($id);
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Countries model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->deleteWithRelated();
return $this->redirect(['index']);
}
/**
*
* Export Countries information into PDF format.
* #param integer $id
* #return mixed
*/
public function actionPdf($id) {
$model = $this->findModel($id);
$providerStates = new \yii\data\ArrayDataProvider([
'allModels' => $model->states,
]);
$content = $this->renderAjax('_pdf', [
'model' => $model,
'providerStates' => $providerStates,
]);
$pdf = new \kartik\mpdf\Pdf([
'mode' => \kartik\mpdf\Pdf::MODE_CORE,
'format' => \kartik\mpdf\Pdf::FORMAT_A4,
'orientation' => \kartik\mpdf\Pdf::ORIENT_PORTRAIT,
'destination' => \kartik\mpdf\Pdf::DEST_BROWSER,
'content' => $content,
'cssFile' => '#vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => '.kv-heading-1{font-size:18px}',
'options' => ['title' => \Yii::$app->name],
'methods' => [
'SetHeader' => [\Yii::$app->name],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();
}
/**
* Creates a new Countries model by another data,
* so user don't need to input all field from scratch.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* #param mixed $id
* #return mixed
*/
public function actionSaveAsNew($id) {
$model = new Countries();
if (Yii::$app->request->post('_asnew') != '1') {
$model = $this->findModel($id);
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('saveAsNew', [
'model' => $model,
]);
}
}
/**
* Finds the Countries model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Countries the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Countries::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
/**
* Action to load a tabular form grid
* for States
* #author Yohanes Candrajaya <moo.tensai#gmail.com>
* #author Jiwantoro Ndaru <jiwanndaru#gmail.com>
*
* #return mixed
*/
public function actionAddStates()
{
if (Yii::$app->request->isAjax) {
$row = Yii::$app->request->post('States');
if((Yii::$app->request->post('isNewRecord') && Yii::$app->request->post('_action') == 'load' && empty($row)) || Yii::$app->request->post('_action') == 'add')
$row[] = [];
return $this->renderAjax('_formStates', ['row' => $row]);
} else {
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
}
Thank you #aendeerei for the quick help!
I am working on a Yii2 based project right now. Today when I was testing my application I noticed a very interesting bug in it. I have a Products table with it's model and controllers and views. (These are generated with gii)
When I list all the records in the index action it works fine. But here comes the bug. When I click on the edit or view action it alwasy renders the first record in the database. I was var_dump the result of the queries and always returnd the mentiond result. Only when I used createCommand got the proper result.
What do you think guys could be the problem with it?
The controller
<?php
namespace backend\controllers;
use Yii;
use app\models\Termek;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* TermekController implements the CRUD actions for Termek model.
*/
class TermekController extends Controller
{
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Termek models.
* #return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Termek::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Termek model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
return $this->render('view', [
'model' => $model,
]);
}
/**
* Creates a new Termek model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Termek();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Termek model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Termek model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Termek model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Termek the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Termek::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
The index.php view file
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* #var $this yii\web\View */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Termeks');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="termek-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('app', 'Create Termek'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'nev',
'szelesseg',
'magassag',
'egyeb:ntext',
// 'ar',
// 'termek_kategoria_id',
// 'torolt',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
Part of view.php view file:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'nev',
'szelesseg',
'magassag',
'egyeb:ntext',
'ar',
'termek_kategoria_id',
'torolt',
],
]) ?>
Model file:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "termek".
*
* #property integer $id
* #property string $nev
* #property double $szelesseg
* #property double $magassag
* #property string $egyeb
* #property string $ar
* #property integer $termek_kategoria_id
* #property string $torolt
*/
class Termek extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'termek';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['nev', 'termek_kategoria_id'], 'required'],
[['szelesseg', 'magassag'], 'number'],
[['egyeb'], 'string'],
[['ar', 'termek_kategoria_id'], 'integer'],
[['torolt'], 'safe'],
[['nev'], 'string', 'max' => 255],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'nev' => Yii::t('app', 'Nev'),
'szelesseg' => Yii::t('app', 'Szelesseg'),
'magassag' => Yii::t('app', 'Magassag'),
'egyeb' => Yii::t('app', 'Egyeb'),
'ar' => Yii::t('app', 'Ar'),
'termek_kategoria_id' => Yii::t('app', 'Termek Kategoria ID'),
'torolt' => Yii::t('app', 'Torolt'),
];
}
/**
* #inheritdoc
* #return \app\models\Query\TermekQuery the active query used by this AR class.
*/
public static function find()
{
return new \app\models\Query\TermekQuery(get_called_class());
}
}
Try modify the findModel function using find()->where instead of findOne
protected function findModel($id)
{
var_dump($id);
$model = Termek::find()->where(['id'=> $id])->one();
var_dump($model);
if (($model !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
I am not sure why but somehow the error solved. I had a function in TermekQuery that was supposed to query all the records that has the deleted field null. I removed it and now works fine.
I have a table production with 3 fields productname,batchno,qty. The data productname,batchno I get is from productbatch model. I can insert it clearly. Now I want to insert into table "bottle" with 3 fields bottlename, productname, qty. Productname has relation with bottlename. And the data comes from model productnames. I want to load bottlename whenever I select productname. I can populate the data and can see in firebug. What I want is to add a textinput in the production form and display the bottlename in the same form.
Production Controller
public function actionCreate()
{
$model = new Production();
$productname = new Productnames();
$bottle = new Bottle();
if ($model->load(Yii::$app->request->post()) && $productname->load(Yii::$app->request->post()))
{
$model->save();
//$bottle->attributes = $model->attributes;
$bottle->usedate = $model->productiondate;
$bottle->useqty = $model->prodqty;
$bottle->productname = $model->productname;
$bottle->bottlename = $productname->bottletype;
// $employee->emp_email = $model->emp_email;
// $employee->emp_mobile = $model->emp_mobile;
$bottle->save();
return $this->redirect(['create']);
} else {
return $this->render('create', [
'model' => $model,
'bottle' => $bottle,
]);
}
}
Production _form
<div class="production-form">
<?php $form = ActiveForm::begin(); ?>
<!--<?= Html::a('Select Product', ['/production/productbatch/index'], ['class'=>'btn btn-primary']) ?> -->
<?= $form->field($model, 'productiondate')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => false,
// modify template for custom rendering
//'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
<!-- echo CHtml::button("(+)",array('title'=>"Select Product",'onclick'=>'js:selectproductforproduction();')); -->
<?= $form->field($model, 'productname')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
'language' => 'en',
'options' => ['placeholder' => 'Select Product Name', 'id' => 'catid'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'batchno')->widget(DepDrop::classname(), [
'options'=>['id'=>'subcat-id'],
'pluginOptions'=>[
'depends'=>['catid'],
'placeholder'=>'Select BatchNo',
'url'=>Url::to(['/production/productbatch/subcat'])
]
]); ?>
<?= $form->field($model, 'prodqty')->textInput() ?>
<?= $form->field($productname, 'bottlename')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
The error I'm getting is -
Productnames Model -
<?php
namespace frontend\modules\production\models;
use Yii;
/**
* This is the model class for table "productnames".
*
* #property integer $productid
* #property string $company
* #property string $type
* #property string $productnames_productname
* #property integer $inventory
* #property string $unitrmcost
* #property string $unitlabelcost
* #property string $unitcartonecost
* #property string $productnames_labelname
* #property string $productnames_cartonename
* #property string $bottletype
* #property string $captype
*
* #property Productbatch[] $productbatches
* #property Production[] $productions
* #property Bottlename $bottletype0
* #property Capname $captype0
* #property Cartonename $productnamesCartonename
* #property Labelname $productnamesLabelname
* #property Productsales[] $productsales
*/
class Productnames extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'productnames';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['company', 'type', 'productnames_productname'], 'required'],
[['inventory'], 'integer'],
[['company'], 'string', 'max' => 40],
[['type', 'unitrmcost', 'unitlabelcost', 'unitcartonecost'], 'string', 'max' => 10],
[['productnames_productname', 'productnames_labelname', 'productnames_cartonename'], 'string', 'max' => 60],
[['bottletype', 'captype'], 'string', 'max' => 50],
[['productnames_productname'], 'unique']
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'productid' => 'Productid',
'company' => 'Company',
'type' => 'Type',
'productnames_productname' => 'Productnames Productname',
'inventory' => 'Inventory',
'unitrmcost' => 'Unitrmcost',
'unitlabelcost' => 'Unitlabelcost',
'unitcartonecost' => 'Unitcartonecost',
'productnames_labelname' => 'Productnames Labelname',
'productnames_cartonename' => 'Productnames Cartonename',
'bottletype' => 'Bottletype',
'captype' => 'Captype',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getProductbatches()
{
return $this->hasMany(Productbatch::className(), ['productname' => 'productnames_productname']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getProductions()
{
return $this->hasMany(Production::className(), ['productname' => 'productnames_productname']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getBottletype0()
{
return $this->hasOne(Bottlename::className(), ['bottlename' => 'bottletype']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getCaptype0()
{
return $this->hasOne(Capname::className(), ['capname' => 'captype']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getProductnamesCartonename()
{
return $this->hasOne(Cartonename::className(), ['cartone_name' => 'productnames_cartonename']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getProductnamesLabelname()
{
return $this->hasOne(Labelname::className(), ['label_name' => 'productnames_labelname']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getProductsales()
{
return $this->hasMany(Productsales::className(), ['productname' => 'productnames_productname']);
}
}
You forgot to render productname to create view, so pass it.
return $this->render('create', [
'model' => $model,
'bottle' => $bottle,
'productname' => $productname,
]);
Here's variants. Either you use two model instances(current and related) and pass them to view http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html or add virtual attribute to current model http://www.yiiframework.com/doc-2.0/guide-input-forms.html.
you have use $model = new Production(); in view ..so you have to define productname in production class .like--
public function attributeLabels()
{
return [
'productname' => 'Product Name',
];
}
then check it ...