[英]cakePHP - Model, Controller without convention/ member returns null
I have a Problem with cakePHP. 我的cakePHP有问题。
This is my Model: 这是我的模型:
<?php
class Veranstaltung extends AppModel
{
var $name = 'Veranstaltung';
var $useTable = 'veranstaltungen';
}
?>
and this is my Controller: 这是我的控制器:
<?php
class VeranstaltungenController extends AppController
{
var $name = 'Veranstaltungen';
public function index()
{
$this->set('veranstaltungen',$this->Veranstaltung->find('all'));
}
}
?>
but $this->Veranstaltung
Returns null. 但是
$this->Veranstaltung
返回null。
How do I solve this? 我该如何解决?
You forget to include your model in controller. 您忘记将模型包括在控制器中。 Do do this add
$uses
field in your controller which is type of array
: 为此,在控制器中添加
$uses
字段,该字段是array
类型:
class VeranstaltungenController extends AppController
{
public $uses = array('Veranstaltung');
public function index()
{
$this->set('veranstaltungen',$this->Veranstaltung->find('all'));
}
}
If there still be a problem it could be required to use 如果仍然有问题,可能需要使用
App::uses('Veranstaltung', 'Model');
before your controller class definition. 在您的控制器类定义之前。
And please forget about var
keyboard, because it's only for backward compatibility with PHP4. 并且请忘记
var
键盘,因为它仅是为了与PHP4向后兼容。
In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword
var
in property declarations instead of (or in addition to)public
,protected
, orprivate
.为了保持与PHP 4的向后兼容性,PHP 5仍将在属性声明中接受关键字
var
的使用,而不是(或附加于)public
,protected
或private
。 However, var is no longer required.但是,不再需要var。 In versions of PHP from 5.0 to 5.1.3, the use of
var
was considered deprecated and would issue anE_STRICT
warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning.在PHP从5.0到5.1.3的版本中,不建议使用
var
,并且会发出E_STRICT
警告,但是自PHP 5.1.3起,它不再被弃用,因此不会发出警告。
You don't need var $name = 'Veranstaltungen';
您不需要
var $name = 'Veranstaltungen';
in your controller. 在您的控制器中。
Also make sure you have configured your database properly? 还要确保已正确配置数据库?
Then try this: 然后试试这个:
$this->loadModel("Veranstaltungen");
$this->set('veranstaltungen',$this->Veranstaltung->find('all'));
<?php
class VeranstaltungenController extends AppController
var $uses = array('Veranstaltungen');
public function index()
{
$this->set('veranstaltungen',$this->Veranstaltung->find('all'));
}
}
?>
Try in your controller 尝试使用您的控制器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.