CakePHP retrieving data from model using find(). D

2019-02-19 06:53发布

问题:

I have the same exact code on my production and development servers but I seem to get different array keys when using find('first') to retrieve data.

Development:

Array
(
    [Page] => Array
        (
            [title] => Welcome!
            [keywords] => keyword 1 keyword 2 keyword 3
        )

)

Production:

 Array
(
    [0] => Array
        (
            [title] => Welcome!
            [keywords] => keyword 1 keyword 2 keyword 3
        )

)

Code in AppController:

//get page title and keywords
        $currentPage = $this->Page->find('first', 
            array(
                'conditions' => array('Page.name' => $this->params['controller']),
                'fields' => array('Page.title', 'Page.keywords')
            ));


        pr($currentPage);

回答1:

I remember this one... :P I had the same problem before

It's caused either by the php version or the mysql version. Check if your server passes the minimum requirements for your cake version

requirements for cakephp2.0

requirements for cakephp1.3

you can check them using a phpinfo();

Hope this helps



回答2:

use $this->set(compact('currentPage',$currentPage));