【问题标题】:Activeform data submit in database using Yii2, Not working使用 Yii2 在数据库中提交 Activeform 数据,不工作
【发布时间】:2017-08-06 01:53:32
【问题描述】:

我是 yii2 框架的新手,当我提交我的 Activeform 数据以保存在数据库中时显示错误

未找到 (#404)。

此 Activeform 在不同的模型视图中提及。我的表格在产品页面上,我只想就该产品对客户进行评论并保存在数据库中。

查看文件:detail.php:

    <?php
    use yii\helpers\Html;
    use yii\widgets\DetailView;
    use yii\widgets\ActiveForm;

    /* @var $this yii\web\View */
    /* @var $model app\models\Product */

    $name = $result->name;

         $price = $result->price;
         $offerrate = 8;
         $offer = ($price / 100) * $offerrate;
         $offerprice = number_format($price - $offer, 2);

         $text = $result->Description;
         $list = explode(",", $text);
    ?>

    <div id="page-content" class="home-page">
    <div class="container">
        <div class="row">
            <div class="col-lg-5">
                <center>
                <?= Html::img(Yii::$app->urlManagerBackend->BaseUrl.'/'.$result->image, ['alt'=>'myImage','width'=>'300','height'=>'300', 'class' => 'img-responsive']) ?>
                <br/>
            <?= Html::button('ADD TO CART', ['class' => 'btn btn-success', 'id'=>'addcart']) ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <?= Html::button('BUY NOW', ['class' => 'btn btn-danger', 'id'=>'buynow']) ?>
            </center>
            </div>
            <div class="col-lg-7">
            <?php echo "<h5 style='color:#009933'>".$name."</h5>"?><br/>
            <?php
                echo "<ul>";
                foreach ($list as $lists)
                {
                    echo "<li class='liststyle'>".$lists."</li>";
                }
                echo "</ul>";
            ?><br/>
            <?php echo "<h6 style='color:#009933'>MobileShop Offer Price Rs: ".$offerprice."</h6>" ?>
            <?= Html::button('Rate and Review product', ['class' => 'btn btn-default', 'id'=>'review', 'data-toggle' => 'collapse', 'data-target' => '#demo']) ?>
            <div id="demo" class="collapse">

     <?php $form = ActiveForm::begin(['id' => 'reviewForm', 'action' => Yii::$app->urlManager->createUrl(['TblFeedback/create'])]); ?>
                        <?= $form->field($feedback, 'cust_name')->textInput() ?>
                        <?= $form->field($feedback, 'feedback')->textArea(['rows' => '2', 'cols' => '10'])?>
                        <?= $form->field($feedback, 'rating')->dropDownlist(['1' => '*', '2' => '* *', '3' => '* * *', '4' => '* * * *', '5' => '* * * * *']) ?>    
                            <div class="form-group">
                                <?= Html::submitButton('Save', ['id' => 'savebtn', 'class' => 'btn btn-default']) ?>
                           </div>
            <?php ActiveForm::end(); ?>
    </div>

    </div>
    </div>
    </div>
    </div>

控制器文件:TblFeedbackController.php

    <?php

    namespace frontend\controllers;

    use Yii;
    use app\models\TblFeedback;
    use frontend\models\Product;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;

    class TblFeedbackController extends \yii\web\Controller
    {
        public function behaviors()
        {
            return [
            'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
            'delete' => ['POST'],
            ],
            ],
            ];
        }

        public function actionIndex()
        {

            return $this->render('index');
        }


        public function actionCreate()
        {
            $feedback = new TblFeedback();

            if ($feedback->load(Yii::$app->request->post()) && $feedback->validate())
            {
                var_dump($feedback);
                die();
                $feedback->save();
                Yii::$app->session->setFlash('success', '<h5>Thanks for review!</h5>');
                return $this->redirect(['index']);
            }
            else {
                return $this->render('index',
                    ['feedback' => $feedback,
                 ]);
                Yii::$app->session->setFlash('error', 'Error Found!');
            }
        }
    }

模型文件:TblFeedback.php

    <?php

    namespace frontend\models;

    use Yii;

    /**
     * This is the model class for table "tbl_feedback".
     *
     * @property integer $id
     * @property integer $product_id
     * @property integer $cust_id
     * @property string $cust_name
     * @property integer $rating
     * @property string $feedback
     *
     * @property Product $product
     * @property User $cust
     */
    class TblFeedback extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'tbl_feedback';
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['product_id', 'cust_id', 'cust_name', 'rating', 'feedback'], 'required'],
                [['product_id', 'cust_id', 'rating'], 'integer'],
                [['rating'], 'integer', 'min' => 1, 'max' => 5],
                [['cust_name'], 'string', 'max' => 200],
                [['feedback'], 'string', 'max' => 1000],
                [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'id']],
                [['cust_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['cust_id' => 'id']],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id' => 'ID',
                'product_id' => 'Product ID',
                'cust_id' => 'Cust ID',
                'cust_name' => 'Enter Name:',
                'rating' => 'Rating',
                'feedback' => 'Description',
            ];
        }

        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProduct()
        {
            return $this->hasOne(Product::className(), ['id' => 'product_id']);
        }

        /**
         * @return \yii\db\ActiveQuery
         */
        public function getCust()
        {
            return $this->hasOne(User::className(), ['id' => 'cust_id']);
        }
    }

【问题讨论】:

    标签: yii2


    【解决方案1】:

    控制器动作的命名约定基于-分隔符,当部分为大写时,您应该使用

     <?php $form = ActiveForm::begin(['id' => 'reviewForm', 
             'action' => Yii::$app->urlManager->createUrl(['Tbl-feedback/create'])]); ?>
    

    命名约定,对于控制器来说,是以小写字母开头的。

    所以你应该使用

       class tblFeedbackController extends \yii\web\Controller
    

          <?php $form = ActiveForm::begin(['id' => 'reviewForm', 
             'action' => Yii::$app->urlManager->createUrl(['tbl-feedback/create'])]); ?>
    

    最后一个建议你可以使用 urlHelper

    use yii\helpers\Url;
    
          <?php $form = ActiveForm::begin(['id' => 'reviewForm', 
             'action' => Url::to(['tbl-feedback/create'])]); ?>
    

    【讨论】:

      猜你喜欢
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多