Danu Kuri-Yoki
Danu Kuri-Yoki

Reputation: 5

Interesting CakePHP failure ! Why?

Using the cakephp 2.2.4 stable version i make an advanced blog application.

-- The models are : Post, User, View.

The failure have different ways of ending. Sometimes it ends with a fatal error : Call to undefined method View::find(), sometimes get an error that script is trying to allocate some bytes, some times chrome show a ERR_CONNECTION_RESET.

View model :

class View extends AppModel {
public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
}

Action i am trying to access:

public function top_today($type = null) {


                $posts = $this->paginate('Post', array('Post.created_time BETWEEN ? AND?' => array(date('Y-m-d'),date('Y-m-d', strtotime("+1 day")))));

                if(!empty($posts)){
                    App::import('Model', 'Rating');
                    App::import('Model', 'View');
                    $rating = new Rating();
                    $view = new View();


                    $i=0;
                        foreach($posts as $post){
                            $plus = $rating->find('count', array('conditions'=>array('Rating.object_type' => 'post', 'Rating.object_id' => $post['Post']['id'], 'Rating.value' => 'plus')));
                            $minus = $rating->find('count', array('conditions'=>array('Rating.object_type' => 'post', 'Rating.object_id' => $post['Post']['id'], 'Rating.value' => 'minus')));
                            $posts[$i]['Post']['rating'] = $plus-$minus;

                            $views = $view->find('count', array('conditions'=>array('View.object_type' => 'post', 'View.object_id' => $post['Post']['id'])));
                            $posts[$i]['Post']['views'] = $views;
                            $i++;
                        }

                }
                $this->set('posts',$posts);


    }

When i don't count the views all goes well, but is the same algorithm as the ratings one !

The View table has the columns : id (A_I) user_id object_id object_type created_time.

Also the error log writes tons of lines:

2012-12-28 22:13:31 Error: Fatal Error (256): [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'render' at line 1

0 D:\xampp\htdocs\lib\Cake\Model\Datasource\DboSource.php(459): PDOStatement-execute(Array)

1 D:\xampp\htdocs\lib\Cake\Model\Datasource\DboSource.php(425): DboSource->_execute('render', Array)

2 D:\xampp\htdocs\lib\Cake\Model\Datasource\DboSource.php(669): DboSource->execute('render', Array, Array)

3 D:\xampp\htdocs\lib\Cake\Model\Datasource\DboSource.php(611): DboSource->fetchAll('render', Array, Array)

4 D:\xampp\htdocs\lib\Cake\Model\Model.php(788): DboSource->query('render', Array, Object(View))

5 D:\xampp\htdocs\lib\Cake\Error\ExceptionRenderer.php(300): Model->__call('render', Array)

6 D:\xampp\htdocs\lib\Cake\Error\ExceptionRenderer.php(300): View->render('error500', 'error')

7 D:\xampp\htdocs\lib\Cake\Error\ExceptionRenderer.php(281): ExceptionRenderer->_outputMessageSafe('error500')

8 D:\xampp\htdocs\lib\Cake\Error\ExceptionRenderer.php(195): ExceptionRenderer->_outputMessage('fatalError')

9 [internal function]: ExceptionRenderer->_cakeError(Object(FatalErrorException))

10 D:\xampp\htdocs\lib\Cake\Error\ExceptionRenderer.php(173): call_user_func_array(Array, Array)

11 D:\xampp\htdocs\lib\Cake\Error\ErrorHandler.php(126): ExceptionRenderer->render()

12 [internal function]: ErrorHandler::handleException(Object(FatalErrorException))

Repeats up to 100 times {

13 D:\xampp\htdocs\lib\Cake\Error\ErrorHandler.php(211): call_user_func('ErrorHandler::h...', Object(FatalErrorException))

14 D:\xampp\htdocs\lib\Cake\Error\ErrorHandler.php(161): ErrorHandler::handleFatalError(256, '[PDOException] ...', 'D:\xampp\htdocs...', 135)

15 [internal function]: ErrorHandler::handleError(256, '[PDOException] ...', 'D:\xampp\htdocs...', 135, Array)

16 D:\xampp\htdocs\lib\Cake\Error\ErrorHandler.php(135): trigger_error('[PDOException] ...', 256)

}

I am stucked on this problem for 2 days.

P.S. : i am on xampp, tried PHP 5.3.8.1 and 5.4.1.

Upvotes: 0

Views: 1042

Answers (1)

dhofstet
dhofstet

Reputation: 9964

The problem is probably the name of your View model because CakePHP already contains a View class. Renaming your model (and the corresponding table) should fix the problem.

Upvotes: 2

Related Questions