I was able to solve the problem by changing
return $this->redirect(['view', 'id' => $model->id]);
to
return $this->render('view', [
'model' => $model,
'ghs'=>$ghs,
]);
I’m not sure if this is the best solution.
I have a related data attribute showing up as NULL, [color="#FF0000"](not set)[/color], when the controller redirects to view. The attribute loads fine in _form and is saved into the database prior to redirect.
There are two related data attributes but this only happens for ghs.ghs_contains.
What am I missing?
Model
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "base_products".
*
* @property integer $id
* @property integer $product_categoryID
* @property integer $product_sub_categoryID
* @property integer $product_directionsID
* @property integer $product_call_outID
* @property string $product_formula
* @property string $product_image
* @property string $product_name
* @property string $product_sub_name
* @property string $product_spanish
* @property string $product_bullets
* @property string $product_description
* @property integer $product_alt_label
* @property integer $product_green_seal
* @property integer $product_dfe
* @property string $call_out_object_style
*
* @property Category $productCategory
* @property Directions $productDirections
* @property SubCategory $productSubCategory
* @property CallOut $productCallOut
* @property CustomerProducts[] $customerProducts
* @property Ghs[] $ghs
*/
class BaseProducts extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'base_products';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['product_categoryID', 'product_directionsID', 'product_formula', 'product_image', 'product_name'], 'required'],
[['product_categoryID', 'product_sub_categoryID', 'product_directionsID', 'product_call_outID', 'product_alt_label', 'product_green_seal', 'product_dfe'], 'integer'],
[['product_bullets', 'product_description'], 'string'],
[['product_formula', 'call_out_object_style'], 'string', 'max' => 32],
[['product_image', 'product_name', 'product_sub_name', 'product_spanish'], 'string', 'max' => 255],
[['product_categoryID'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['product_categoryID' => 'id']],
[['product_directionsID'], 'exist', 'skipOnError' => true, 'targetClass' => Directions::className(), 'targetAttribute' => ['product_directionsID' => 'id']],
[['product_sub_categoryID'], 'exist', 'skipOnError' => true, 'targetClass' => SubCategory::className(), 'targetAttribute' => ['product_sub_categoryID' => 'id']],
[['product_call_outID'], 'exist', 'skipOnError' => true, 'targetClass' => CallOut::className(), 'targetAttribute' => ['product_call_outID' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'product_categoryID' => 'Category ID Number',
'product_sub_categoryID' => 'Sub Category ID Number',
'product_directionsID' => 'Directions ID Number',
'product_call_outID' => 'Call Out ID Number',
'product_formula' => 'Formula',
'product_image' => 'Image File',
'product_name' => 'Name',
'product_sub_name' => 'Sub Name',
'product_spanish' => 'Spanish Translation',
'product_bullets' => 'Bullets',
'product_description' => 'Description',
'product_alt_label' => 'Alternate Label (13.5 x 4.5)',
'product_green_seal' => 'Green Seal',
'product_dfe' => 'Safer Choice',
'call_out_object_style' => 'Call Out Object Style',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductCategory()
{
return $this->hasOne(Category::className(), ['id' => 'product_categoryID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductDirections()
{
return $this->hasOne(Directions::className(), ['id' => 'product_directionsID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductSubCategory()
{
return $this->hasOne(SubCategory::className(), ['id' => 'product_sub_categoryID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductCallOut()
{
return $this->hasOne(CallOut::className(), ['id' => 'product_call_outID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCustomerProducts()
{
return $this->hasMany(CustomerProducts::className(), ['customer_product_base_productID' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getGhs()
{
return $this->hasMany(Ghs::className(), ['ghs_productID' => 'id']);
}
}
Controller’s Update Function
public function actionUpdate($id)
{
//http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html
$model = $this->findModel($id);
$directions = Directions::findOne($model->product_directionsID);
$ghs = Ghs::findOne(['ghs_productID'=>$id]);
if ($model->load(Yii::$app->request->post()) && $directions->load(Yii::$app->request->post()) && $ghs->load(Yii::$app->request->post()) && $model->save() && $directions->save() && $ghs->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
'directions'=>$directions,
'ghs'=>$ghs,
]);
}
}
View File
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model app\models\BaseProducts */
$this->title = $model->product_name;
$this->params['breadcrumbs'][] = ['label' => 'Base Products', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="base-products-view">
<h1><?= Html::encode($this->title) ?></h1>
<h2>Update Complete</h2>
<p>
Please review the updated information for <?= Html::encode($this->title = $model->product_formula . ' ' . $model->product_name) ?> below.<br> Need to revise or make additional changes?
<?= Html::a('Click here', ['update', 'id' => $model->id]) ?>.
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'product_categoryID',
'product_sub_categoryID',
'product_directionsID',
'productDirections.product_directions:ntext',
'product_call_outID',
'product_formula',
'product_image',
'product_name',
'product_sub_name',
'product_spanish',
'product_bullets:ntext',
'product_description:ntext',
'product_alt_label',
'product_green_seal',
'product_dfe',
'call_out_object_style',
'ghs.ghs_contains:ntext',
],
]) ?>
</div>