Yii2 Basic สร้าง Comment (มือใหม่หัดเล่น Yii Framework)

wave
prajoub_dls
2,820
สังคมนักพัฒนาระบบ (Community)
Yii2 Basic สร้าง Comment (มือใหม่หัดเล่น Yii Framework)

อยากทราบวิธีเขียนโค๊ดการสร้าง Comment เวลาที่มีผู้เข้าชมเว็บไซต์อ่านบทความแล้วสามารถ Comment คำถามต่างๆหรือ ข้อความต่างๆในเพจนั้นได้

7 ความคิดเห็น

  • prajoub_dls

  • มานพ กองอุ่น

สมมติมี Model

Post (id, title, description)
Comment (id, post_id, description)

ใน PostController

public function actionView($id)
{
    $model = $this->findModel($id);
    $comment = new Comment();

    if($comment->load(Yii::$app->request->post())
    {
        $comment->post_id = $model->id;
        $comment->save(); //บันทึกความคิดเห็น
    }

    return $this->render('view', [
        'model' => $model,
        'comment' => $comment,
    ]);
}

 

ขอบคุณครับ

มันเป็นแบบนี้ต้องแก้อย่างไรครับ Comment ก็ Generator แล้วนะครับ

Error

Class 'app\controllers\Comment' not found

ยังไม่ได้ use มาครับต้อง use ก่อนครับ

ต้อง use มาที่ตัวไหนคับ  ผมยังไม่เข้าใจ เพิ่งลองเขียนเว็บด้วย yii เป็นครั้งแรก (^_^)

use app\models\Comment; ใน Controller ที่ Error ครับ

ลองทำความเข้าใจ MVC ก่อนนะครับ https://www.programmerthailand.com/course/view/31

ขอบคุณครับ