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 数据结构中的数组地址问题
  • ¥15 LD衰减怎么根据纵坐标计算横坐标的值
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置
  • ¥15 excel 日常使用中出现问题
  • ¥20 pdusession建立失败
  • ¥15 为什么mqtt接收不到数据?