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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝