无法找到与paginate兼容的对象。使用CakePHP

时间:2015-07-01 15:40:32

标签: php cakephp cakephp-3.0

我是PHP和CakePHP的新手。使用CakePhp连接数据库时发现问题。

以下是我的应用配置。 我在Bitnami WAMP堆栈5.4.40-0 我正在使用CakePhp 3.0.4来创建一个web mvc应用程序

在我的app.php文件中输入数据源

/**
 * Connection information used by the ORM to connect
 * to your application's datastores.
 * Drivers include Mysql Postgres Sqlite Sqlserver
 * See vendor\cakephp\cakephp\src\Database\Driver for complete list
 */
'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'nonstandard_port_number',
        'username' => 'test2',
        'password' => 'computer',
        'database' => 'jobs',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,

        /**
         * Set identifier quoting to true if you are using reserved words or
         * special characters in your table or column names. Enabling this
         * setting will result in queries built using the Query Builder having
         * identifiers quoted when creating SQL. It should be noted that this
         * decreases performance because each query needs to be traversed and
         * manipulated before being executed.
         */
        'quoteIdentifiers' => false,

        /**
         * During development, if using MySQL < 5.6, uncommenting the
         * following line could boost the speed at which schema metadata is
         * fetched from the database. It can also be set directly with the
         * mysql configuration directive 'innodb_stats_on_metadata = 0'
         * which is the recommended value in production environments
         */
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
    ],

我可以使用bake命令生成基本的锅炉板代码

但现在我在localhost中运行我的应用程序:8765 / myapp

我收到以下错误

2015-07-01 14:31:22 Error: [RuntimeException] Unable to locate an object compatible with paginate.

请求网址:/ myjobs

我的控制器代码

<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Jobs Controller
 *
 * @property \App\Model\Table\JobsTable $Jobs
 */
class MyJobsController extends AppController
{

    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Jobs']
        ];
        $this->set('jobs', $this->paginate($this->Jobs));
        $this->set('_serialize', ['jobs']);
    }

    /**
     * View method
     *
     * @param string|null $id Job id.
     * @return void
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function view($id = null)
    {
        $job = $this->Jobs->get($id, [
            'contain' => ['Jobs']
        ]);
        $this->set('job', $job);
        $this->set('_serialize', ['job']);
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $job = $this->Jobs->newEntity();
        if ($this->request->is('post')) {
            $job = $this->Jobs->patchEntity($job, $this->request->data);
            if ($this->Jobs->save($job)) {
                $this->Flash->success(__('The job has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The job could not be saved. Please, try again.'));
            }
        }
        $jobs = $this->Jobs->Jobs->find('list', ['limit' => 200]);
        $this->set(compact('job', 'jobs'));
        $this->set('_serialize', ['job']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Job id.
     * @return void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $job = $this->Jobs->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $job = $this->Jobs->patchEntity($job, $this->request->data);
            if ($this->Jobs->save($job)) {
                $this->Flash->success(__('The job has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The job could not be saved. Please, try again.'));
            }
        }
        $jobs = $this->Jobs->Jobs->find('list', ['limit' => 200]);
        $this->set(compact('job', 'jobs'));
        $this->set('_serialize', ['job']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Job id.
     * @return void Redirects to index.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $job = $this->Jobs->get($id);
        if ($this->Jobs->delete($job)) {
            $this->Flash->success(__('The job has been deleted.'));
        } else {
            $this->Flash->error(__('The job could not be deleted. Please, try again.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}

还想知道位于端口8765中的内部服务器位于何处。 ?我想使用eclipse在这个服务器上调试资源。

2 个答案:

答案 0 :(得分:5)

键入错误的网址时会发生此错误:

它应该是

之一
localhost:8765/my-app   
localhost:8765/myApp 
localhost:8765/my_app

但不是:localhost:8765 / myapp

答案 1 :(得分:-1)

我想我的桌面结构存在一些问题。

可能CakePHP在表中查找某些属性。 特别是作为主键的id的自动增量属性。

我根据书签表创建了我的表结构,并且它有效。

我有一个枚举字段,它没有通过CakePHP映射到对象。我想枚举是CakePHP的限制。

此致

Saurav