Showing posts with label cakephp. Show all posts
Showing posts with label cakephp. Show all posts

April 3, 2021

Call to undefined method Cake\Mailer\AbstractTransport::_headersToString()

Upgrading CakePHP3 to CakePHP4, getting this error:
Call to undefined method Cake\Mailer\AbstractTransport::_headersToString()
The method was moved to src/Mailer/Message.php in ce8c7e8. Instead, call getHeadersString() providing an array of headers you need.
$headerNames = [
    'bcc',
    'cc',
    'from',
    'readReceipt',
    'replyTo',
    'returnPath',
    'sender',
    'subject',
    'to',
];
// get all the headers
$headers = $email->getHeadersString($headerNames);

April 1, 2021

upgrade cakephp 3 to 4 using rector

The website you're upgrading must be on the latest CakePHP3. In your composer.json make sure you have "cakephp/cakephp": "^3" and run php composer.phar upgrade. You will also need to have a compatible phpunit installed: php composer.phar require --dev phpunit/phpunit:"^5.7|^6.0" Next, unlock your composer.json requirements:
    "require": {

        "cakephp/authentication": ">1.2",
        "cakephp/cakephp": "^3",
        "cakephp/migrations": ">3.0",
        "cakephp/plugin-installer": ">1.0",
        "friendsofcake/bootstrap-ui": ">1.0",
        "friendsofcake/crud": ">5.0",
        "friendsofcake/crud-view": ">0.14.0",
    },
    "require-dev": {
        "cakephp/bake": ">1",
        "cakephp/cakephp-codesniffer": ">3",
        "cakephp/debug_kit": ">3",
    },
This will let composer pick the right dependency versions as it upgrades without giving you numerous errors such as below.
Problem 1 - cakephp/authentication is locked to version 1.4.2 and an update of this package was not requested Problem 2 - friendsofcake/bootstrap-ui is locked to version 1.4.3 and an update of this package was not requested. - cakephp/migrations is locked to version dev-cake3 and an update of this package was not requested. - Conclusion: don't install cakephp/cakephp 4.0.0 (conflict analysis result) etc.
And here's how to actually upgrade.
cd /var/www
git clone git://github.com/cakephp/upgrade
cd /var/www/upgrade
git checkout master
php ../composer.phar install --no-dev
bin/cake upgrade file_rename locales /var/www/app3
bin/cake upgrade file_rename templates /var/www/app3
bin/cake upgrade rector --rules phpunit80 /var/www/app3/tests
bin/cake upgrade rector --rules cakephp40 /var/www/app3/src
cd /var/www/app3
php ../composer.phar require --dev --update-with-dependencies "phpunit/phpunit:^8.0"
php ../composer.phar require --update-with-dependencies "cakephp/cakephp:4.0.*"
php ../composer.phar require --dev cakephp/debug_kit:"^4.0"
You are likely to run into errors such as below:
PHP Deprecated: Use Cake\Error\ConsoleErrorHandler instead of Cake\Console\ConsoleErrorHandler. - /var/www/app3/vendor/composer/ClassLoader.php, line: 478 Deprecated: Use Cake\Error\ConsoleErrorHandler instead of Cake\Console\ConsoleErrorHandler. - /var/www/app3/vendor/composer/ClassLoader.php, line: 478
As well as…
The view for UsersController::login() was not found. Template file `Users/login.php` could not be found. The following paths were searched: - `/var/www/app3/src/Template/Users/login.php` - `/var/www/app3/vendor/cakephp/cakephp/templates/Users/login.php`
Update your config/ files remaining from CakePHP3 using their latest CakePHP4 counterparts.
PHP Fatal error: Declaration of App\Shell\ConsoleShell::getOptionParser() must be compatible with Cake\Console\Shell::getOptionParser(): Cake\Console\ConsoleOptionParser in /var/www/app3/src/Shell/ConsoleShell.php on line 25
An error message like the above actually guides you on what needs to be updated. If you need to see the full namespaced path of the class it wants, just search the CakePHP CookBook.
A route matching "array ( 'prefix' => 'profile', 'controller' => 'settings', 'action' => 'index', 'plugin' => NULL, '_ext' => NULL, )" could not be found.
If using $this->Html->link, make sure your prefix and controller names start with an uppercase/capital letter.

December 17, 2020

find-and-replace-cakephp-upgrade

grep -inFR "::config()" src config
grep -inFR "::config(" src config
echo "::config(" > /tmp/pattern
echo "::setConfig(" > /tmp/replacement
find src config \( -name "*.php" -o -name "*.ctp" \) | xargs -n 1 sed -i -e "s|$(cat /tmp/pattern)|$(cat /tmp/replacement)|g"

April 16, 2019

cakephp 3 query where date now()

        $query = $this->find('all', [
            // …
        ]);
        $query->where([
            'DATE(created)' => $query->func()->date([$query->func()->now()]),
        ]);

April 4, 2019

cakephp 3 multiple levels deep prefix

Router::prefix('api', function ($routes) {
    $routes->prefix('users', function ($routes) {
        $routes->fallbacks(DashedRoute::class);
    });
});

namespace App\Controller\Api\Users;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;

class UsersController extends AppController
{

    public function index()
    {
        // ...
    }

}

August 15, 2018

cakephp debug_kit table doesnt exist

Warning (512): Fixture creation for "panels" failed "SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes" [CORE/src/TestSuite/Fixture/TestFixture.php, line 313] Warning (512): Fixture creation for "panels" failed "SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes" [CORE/src/TestSuite/Fixture/TestFixture.php, line 313] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cakephp_debug_kit.panels' doesn't exist
Solved by changing the database/schema collation from utf8mb4 to utf8/utf8_unicode_ci

July 9, 2018

Controlling CakePHP FriendsOfCake/bootstrap-ui checkbox/radio wrappers

CakePHP may render the inputs based on your global $this->Form preferences. This will be a problem if you just need a clean checkbox to place in a table, and have the whole row be it's label for example.

To solve, you will need to use templates. Add the following to your form options:

    'templates' => [
        'checkboxContainer' => '{{content}}',
        'checkboxFormGroup' => '{{label}}{{error}}{{help}}',
        'checkboxWrapper' => '{{label}}',
    ],
Use debug($this->Form->getTemplates()); to get a list of all available templates.

February 25, 2017

cakephp bootstrap ui validation error messages not showing

I needed my CakePHP 3 application to automatically show the validation errors next to the coresponding FriendsOfCake/bootstrap-ui inputs. Here's what was the key point: you have to pass the corresponding model table instance, that contains those errors, into the $this->Form->create() call.

For example:

// in your controller
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
    $user->set(array(
        // ... set you data to save
    ));
    $this->Users->save($user);
    debug($user->errors); // to confirm
}
$this->set('user', $user);

// in your .ctp file
$this->Form->create($user);

source

January 7, 2017

cakephp frozentime diff

Cake\I18n\FrozenTime is an extension to PHP's default DateTime, so if you need to calculate difference between two dates, for example, in seconds, do this:

$entity->modified->getTimestamp() - $entity->created->getTimestamp()

December 31, 2016

cakephp missing view ctp file exists

If CakePHP shows you

Missing View: the view was not found. Confirm you have created the file in one of the following paths

even though the view file exists, check the following:

  • permissions on the folder where the file is. some editors/ftp clients create the folders with “000” permissions. make sure the folder is readable
  • confirm the file itself is readable (i.e. has permissions like 0644)
  • this is the least obvious one – if you have a beforeFilter() in your controller that throws that error, make sure it has the parent::beforeFilter() in it