CakePHP:$ this-> model-> find('all')崩溃所有页面。怎么回事?

时间:2014-02-19 04:32:54

标签: php cakephp

我是CakePHP的新手,这只是我学习的第二个PHP框架。 (Codeigniter是第一个)

我在cakephp官方网站上关注CakePHP博客教程,一切正常。我喜欢CakePHP自动生成find('all')方法的想法。所以我决定通过制作我自己的模型再试一次,并在同一个Posts控制器的另一个方法上调用它。 (在教程之后创建的那个)但随后它返回以下错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 152861 bytes) in D:\XAMPP\htdocs\cakephpTesting\lib\cake\Log\Engine\FileLog.php on line 134

或者有时当我再次重新加载页面时,它会发出另一条消息:

Warning (4096): Argument 1 passed to View::__construct() must be an instance of Controller, array given [CORE\cake\view\View.php, line 320]

Fatal Error
Error: Call to undefined method View::find()    
File: D:\XAMPP\htdocs\cakephpTesting\app\Controller\PostsController.php 
Line: 10

这是我在PostsController.php中添加到控制器的代码:

public function listView() {
    $this->loadModel('View');
    $this->View->find('all');
}

这是新模型中的代码(/app/model/View.php

<?php
    class View extends AppModel {
    }
?>

新表只是教程的直接副本,除了表名现在称为“视图”。

我创建了另一个/app/view/Post/list_view.ctp,它是一个空白文件。

我正在使用官方网站建议的Cakephp 2.4.5。

我错过了什么吗?为什么它会创建不同的错误消息?

2 个答案:

答案 0 :(得分:6)

看起来像命名冲突,您已经定义了一个名为View的类,该类已在[CORE\cake\view\View.php]中定义,并且是CakePHP运行所必需的。

将您的文件名更改为MyView.php,将您的班级名称更改为MyView,看看是否能解决您的问题。

答案 1 :(得分:1)

使用$ this-&gt; View-&gt; find('all')是如此可怕的想法; 因为如果数据库数据很大,你就无法获取该数据。这是关于php.ini配置。 所以我建议你采取一些限制的数据:

$this->Model->find('all', array(
   'limit' => 10// or what you want
));

或使用某些条件来优化您的请求:

$this->Model->find('all', array(
   'conditions' => array(
      //some conditions here etc...
   ),
   'limit' => 10// all time you need know what you need take on db, not just all
));

如果您想在视图中使用所有数据,请使用paginator。 “所有时间”尝试最大限度地优化您的代码!