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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵