kamilwylegala / cakephp2-php8

CakePHP 2 fork that supports PHP 8
122 stars 59 forks source link

Deprecated (8192): Automatic conversion of false to array is deprecated [CORE\Cake\Model\Model.php, line 1232] #72

Closed pbriongos closed 4 months ago

pbriongos commented 4 months ago

On updating row in a model:

Deprecated (8192): Automatic conversion of false to array is deprecated [CORE\Cake\Model\Model.php, line 1232]

imagen

Trace:

Model::set() - CORE\Cake\Model\Model.php, line 1232
Model::_doSave() - CORE\Cake\Model\Model.php, line 1809
Model::save() - CORE\Cake\Model\Model.php, line 1761
ConfigsController::procesaColaEmail() - APP\Controller\ConfigsController.php, line 2033
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE\Cake\Controller\Controller.php, line 500
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 193
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
[main] - APP\webroot\index.php, line 137
pbarabe commented 4 months ago

Hi again @pbriongos!

I'm having trouble duplicating this error. I can see how it could be triggered if $this->data were false (per the code you highlighted). However, since the Model class's $data property is instantiated as an array (on Model.php line 91), ordinarily $this->data should always be an array, and so shouldn't trigger the "false to array" deprecation error unless the Model's $data property was altered from an external source. Since the $data property is public, that's hypothetically possible, but it seems out of the ordinary to me.

Are you somehow altering your model object's $data property in your application's APP\Controller\ConfigsController.php before saving?

@kamilwylegala What do you think? It would be easy to implement a fix similar to the I18n false-to-array fix that was just merged, but I'm not sure this is so universal.

kamilwylegala commented 4 months ago

@pbarabe I agree.

@pbriongos It's interesting what's in:

ConfigsController::procesaColaEmail() - APP\Controller\ConfigsController.php

Or how your model is defined.

pbriongos commented 4 months ago

Hi, the model is special because it's a datasource that I created for MongoDB. I'm just updating a row based on an ID. ConfigsController:

App::import('Model', 'Email'); $email_model = new Email; $email_model->id=$last_id_email; $email_model->save(array("view_vars_json"=>null,"body"=>$content,"bcc"=>implode(";",$emails_bcc)));

Maybe the datasource is doing incorrect things whit $this->data.

Anyways, the deprecated notice can be avoided writing this in Model.php:

imagen

pbarabe commented 4 months ago

Hi @pbriongos!

Your code is a bit out of the ordinary for the way model data is usually saved/updated in CakePHP 2, and I think that's why you're encountering this error. The conventional approach, per CakePHP documentation, would be more like this:

<?php
App::uses('AppController', 'Controller');

class EmailsController extends AppController
{
  // Explicit declaration of Model class(es) used by this Controller
  public $uses = ['Email'];

  // ... other class properties and methods

  public function edit($id = null) {
    if (!$this->Email->exists($id)) {
      throw new NotFoundException(__('Invalid email'));
    }

    // If HTTP request is POST or PUT, try to update the requested record
    if ($this->request->is(array('post', 'put'))) {

      // Standard usage of Model CRUD methods, ie. $this->ModelName->save($data);
      if ($this->Email->save($this->request->data)) {
        $this->Flash->success(__('The email has been saved.'));

        return $this->redirect(array('action' => 'index'));
      } else {
        $this->Flash->error(__('The email could not be saved. Please, try again.'));
      }

    // If HTTP request is not POST or PUT, find the requested record
    } else {
      $options = array('conditions' => array('Email.' . $this->Email->primaryKey => $id));
      $this->request->data = $this->Email->find('first', $options);
    }
  }

  // ... more class methods
}

Also, I hope you don't mind me saying, but if this is a new project you're working on, I'd strongly recommend considering starting with a more up-to-date version of CakePHP (ie. 4.x or 5.x).

cakephp2-php8 is really intended to provide backward compatibility for established projects built on CakePHP 2.x that cannot easily be upgraded.

I hope that's all helpful!

pbriongos commented 4 months ago

@pbarabe I'm developing with CAKE from nearly 15 years ago, I'm working in a 5000 employees enterprise and the project is in CAKE 2 (started in 2015), we have nearly 500 controllers and a lot of improvements to CAKE, so I have "a bit" experience with CAKE. So I don't want to upgrade to 3.x or 4.x because it's impossible with this huge project, it's very established and works very well. So I need to be compatible with last versions of PHP, so I need to be updated with this topic.

I repaired this DEPRECATED notice as I said with just a line, so if you want to take it ok. I need the mongo datasource, so this is what it is.

aokazu commented 4 months ago

Hello,

Deprecated (8192): Automatic conversion of false to array is deprecated [CORE\Cake\Model\Model.php, line 1232]

Upon investigation, I found that this warning is triggered due to the automatic conversion of false to an array. To address this, I suggest modifying the code in Vendor/cakephp/cakephp/lib/Cake/Model/Model.php at line 1982.

Currently, the code is:

$this->data = false;

I propose changing it to:

$this->data = array();

This change will prevent the deprecated warning by directly assigning an empty array instead of false. This solution worked effectively in my project and should resolve the issue for others as well.

Thank you for your efforts in maintaining CakePHP compatibility with PHP 8. I hope this suggestion is helpful.

Best regards

pbarabe commented 4 months ago

Hi @pbriongos

Apologies, I'd gotten the impression this was all new to you and was intending to to helpful. I see I was mistaken. All good, I hope.

Both your suggested change and that of @aokazu could be helpful. I'll defer to @kamilwylegala since he is the maintainer.

kamilwylegala commented 4 months ago

Thank you guys for feedback.

Fixed here: https://github.com/kamilwylegala/cakephp2-php8/pull/73 Per @aokazu recommendation.

aokazu commented 4 months ago

I'm glad I could help. This repository has been extremely helpful. Thank you for your continued support.