朋友您好我是Yii2 Framework的新手。
我正在编写以下提及文件名中指定的代码....
虽然我正在调用模型方法,但未在控制台中显示 500错误的输出
代码中有任何错误,请在此提及..
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 - 模型
<?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';
}
}
答案 0 :(得分:1)
在没有models()
$data = Site::getData();
答案 1 :(得分:0)
SiteController.php:
$data = Site::getData();
Site.php(Model):确保该函数是静态的
public static function getData() {
return "anything";
}
答案 2 :(得分:0)
你好朋友,先谢谢大家的帮助 现在我正在分享这段代码,现在已经完成,现在可以在mycase中使用。
我用以下代码解决了这个问题。 现在试试这个完全正常的工作
<强> 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 - 模型
<?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();
}
}