Skip to content

Instantly share code, notes, and snippets.

@chronon
Last active January 7, 2021 09:38
Show Gist options
  • Save chronon/ed68c5843d3b6481ae9c8a50632b5d15 to your computer and use it in GitHub Desktop.
Save chronon/ed68c5843d3b6481ae9c8a50632b5d15 to your computer and use it in GitHub Desktop.
CakePHP 3 Entities in CakePHP 2 Projects

Run composer require cakephp/orm:^4.0

Create Model/Entity/ArticleEntity.php:

<?php
use Cake\ORM\Entity;

/**
 * Class: Article
 */
class ArticleEntity extends Entity {

    /**
     * _accessible
     *
     * @var array
     */
    protected $_accessible = [
        '*' => true,
    ];

    /**
     * _getBody
     *
     * @param string $body
     * @return string
     */
    public function _getBody($body) {
        return strtoupper($this->_fields['body']);
    }
}

To use:

App::uses('ArticleEntity', 'Model/Entity');
// ...
$article = $this->Article->find('first');
$article = new ArticleEntity($article['Article']);
// ...
echo $article->body;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment