🎯 ゴール

✅ Post 完全CRUD

✅ ログイン必須制御

✅ バリデーション

✅ エラー表示

✅ findByPk使用


🟥 ① Post モデル(ActiveRecord)

protected/models/Post.php

<?php

// Postテーブルと紐づくActiveRecordモデル
class Post extends CActiveRecord
{
    // モデルインスタンス取得(Yii1.1特有)
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    // DBテーブル名
    public function tableName()
    {
        return 'post';
    }

    // バリデーションルール定義
    public function rules()
    {
        return array(
            // titleとcontentは必須
            array('title, content', 'required'),

            // titleは最大255文字
            array('title', 'length', 'max'=>255),
        );
    }

    // ラベル定義
    public function attributeLabels()
    {
        return array(
            'title' => 'タイトル',
            'content' => '本文',
        );
    }
}

🟥 ② PostController(完全CRUD)

protected/controllers/PostController.php

<?php

class PostController extends Controller
{
    // ログインしていないとアクセス不可
    public function filters()
    {
        return array(
            'accessControl',
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'users'=>array('@'), // ログインユーザーのみ
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }

    // 一覧表示
    public function actionIndex()
    {
        // 全件取得
        $posts = Post::model()->findAll();

        $this->render('index', array(
            'posts'=>$posts
        ));
    }

    // 作成
    public function actionCreate()
    {
        $model = new Post;

        // POST送信されたら
        if(isset($_POST['Post']))
        {
            // 一括代入
            $model->attributes = $_POST['Post'];

            // 保存成功なら一覧へ
            if($model->save())
            {
                $this->redirect(array('index'));
            }
        }

        $this->render('create', array(
            'model'=>$model
        ));
    }

    // 更新
    public function actionUpdate($id)
    {
        // 主キーで1件取得
        $model = Post::model()->findByPk($id);

        if(!$model)
            throw new CHttpException(404,'Not Found');

        if(isset($_POST['Post']))
        {
            $model->attributes = $_POST['Post'];

            if($model->save())
            {
                $this->redirect(array('index'));
            }
        }

        $this->render('update', array(
            'model'=>$model
        ));
    }

    // 削除
    public function actionDelete($id)
    {
        $model = Post::model()->findByPk($id);

        if($model)
        {
            $model->delete();
        }

        $this->redirect(array('index'));
    }
}

🟥 ③ View


index.php

<h1>Post一覧</h1>

<a href="<?php echo $this->createUrl('create'); ?>">新規作成</a>

<?php foreach($posts as $post): ?>
    <div>
        <h3><?php echo CHtml::encode($post->title); ?></h3>
        <p><?php echo CHtml::encode($post->content); ?></p>

        <!-- 編集リンク -->
        <a href="<?php echo $this->createUrl('update', array('id'=>$post->id)); ?>">編集</a>

        <!-- 削除リンク -->
        <a href="<?php echo $this->createUrl('delete', array('id'=>$post->id)); ?>">削除</a>
    </div>
<?php endforeach; ?>