如何从表中获取值并在视图中显示它?

时间:2014-11-14 08:02:27

标签: php database yii

我是PHP框架的新手(使用Yii框架)。如何在MySQL中获取值然后在视图中显示它?我对定义模型,控制器以及如何使用它感到困惑。

型号:

public $name;
public $info;
$product = product::find()->orderBy('name')->all();

public function tablename()
{
   return 'productdata';
}

控制器:

public function actionView($id)
{
    $this->render('view',array(
        'model'=>$this->loadModel($id),
    ));
}

public function LoadModel($id){
     $model=productdata::model()->find($id);
     return $model;
}

1 个答案:

答案 0 :(得分:1)

您是否尝试过任何教程?

[models/MyModel.php]

class MyModel extends CActiveRecord {
    public function rules() {
        return ['id, name, value', 'safe'];
    }
}

[controllers/MyController.php]

class MyController extends CController {
   public function actionView($id) {
      $model = MyModel::model()->findByPk($id);
      $attributes = Yii::app()->request->getParam('MyModel');

      if ($attributes) {
         if ($model->save()) {
             $this->redirect('myController/admin');
         } else {
             throw new CHttpException(500, 'Model not saved. Use echo CActiveForm::validat($model);');
         }
      }

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

[views/MyController/view.php]

<?php echo $model->name; ?>