dousha4804 2016-09-12 06:10
浏览 53
已采纳

Yii中控制器中的呼叫模型方法

Hello Friends I am new to Yii2 Framework.

i am writing following code as specified in mention filename still....

While i am calling model method then does not showing output showing 500 error in console

Is Any Mistake in code then please mention here ..

SiteController.php

<?php

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Site;

class SiteController extends CController
{
    // Define an action. actionIndex is a default action
    public function actionIndex()
    {   
        $data =  Site::models()->getData();    
        $this->render('view',array(
        'data'=>$data,
        ));

        // $this->render('view');
        }
  }

Site.php - Model

<?php
namespace app\models;

use yii\db\ActiveRecord;

    class Site extends ActiveRecord
    {
        public function getData() {
              $connection = Yii::app()->db;
              $command = $connection->createCommand("Select * from site");
              return $caterow = $command->queryAll();
        }
        public static function tableName()
        {
            return 'site';
        }
    }
  • 写回答

3条回答 默认 最新

  • dongzhuang1923 2016-09-12 09:02
    关注

    hello friends, First Thanks to all to help me now i am sharing this code this is complete now work in mycase.

    i had sorted out this problem with following code. Try this one now its completely work

    main.php

        <?php
    
    // This is the main config file
    // You should avoid placing configuration everywhere else
    return array(
        // We have to enable Yii to load all the files we created
        'import'=>array(
            'application.models.*',
            'application.views.*',
                    'application.components.*'
        ),
    
        // We set up SQLite database connection. It's a simple database
        // which does not require a separae server
        'components'=>array(
            'db'=>array(
                'class'=>'CDbConnection',
                'connectionString'=>'mysql:host=localhost;dbname=student',
                'username'=>'root',
                'password'=>'',
                'emulatePrepare'=>true,
                ),
        )
    );
    

    SiteController.php

    <?php
    
    class SiteController extends CController
    {
        /**
         * Lists all models.
         */
        public function actionIndex()
        {
                    $s = new Site();
                    $a = $s->getData();
            $this->render('view',array('a'=>$a));
        }
    
    }
    

    Site.php - Model

    <?php
    
    class Site extends CActiveRecord
    {
        /**
         * Returns the static model of the specified AR class.
         * @return static the static model class
         */
        public static function model()
        {
            return parent::model('Site');
        }
    
        /**
         * @return string the associated database table name
         */
        public function tableName()
        {
            return 'site';
        }
    
            public function getData(){
                return $result =  Site::model()->find();
            }
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动