josegonzalez/cakephp-entity

This package is abandoned and no longer maintained. No replacement package was suggested.

Use model objects/classes instead of arrays in your CakePHP application

Installs: 29 523

Dependents: 0

Suggesters: 0

Security: 0

Stars: 30

Watchers: 6

Forks: 18

Open Issues: 1

Type:cakephp-plugin

3.0.2 2015-07-06 19:28 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:17:47 UTC


README

Build Status Coverage Status Total Downloads Latest Stable Version

CakePHP Entity Plugin

Background

  • find() now returns an array of objects instead of arrays of arrays.
  • 100% compatible with the standard Model.
  • Open source. Available on GitHub. MIT Lisense.
  • CakePHP 2.4+, PHP 5.4+

Originally: https://github.com/kanshin/CakeEntity

Requirements

  • CakePHP 2.4
  • PHP 5.3
  • Patience

Installation

[Using Composer]

Add the plugin to your project's composer.json - something like this:

{
	"require": {
		"josegonzalez/cakephp-entity": "dev-master"
	}
}

Because this plugin has the type cakephp-plugin set in it's own composer.json, composer knows to install it inside your /Plugins directory, rather than in the usual vendors file. It is recommended that you add /Plugins/Entity to your .gitignore file. (Why? read this.)

[Manual]

[GIT Submodule]

In your app directory type:

git submodule add -b master git://github.com/josegonzalez/cakephp-entity.git Plugin/Entity
git submodule init
git submodule update

[GIT Clone]

In your Plugin directory type:

git clone -b master git://github.com/josegonzalez/cakephp-entity.git Entity

[Composer]

Add the following to your composer.json:

"cakephp-entity": "1.0.0"

And then run the composer update command to install the dependency.

Enable plugin

In 2.0 you need to enable the plugin in your app/Config/bootstrap.php file:

CakePlugin::load('Entity');

If you are already using CakePlugin::loadAll();, then this is not necessary.

Usage

CakeEntity doesn't change anything with your current the installation. You'll have to enable the functionality by indicating it be used. This is for compatibility reasons.

Use Table as the super class of models where you'd like to activate the plugin.

<?php
App::uses('Table', 'Entity.ORM');

class Post extends Table {

}
?>

Then, in the options of the find, specify entity => true:

<?php
$entity = $this->Post->find('first', array('entity' => true));
?>

You may also set the $entity property on your model to true to return entities:

<?php
$this->Post->entity = true; 
$entity = $this->Post->find('first');
?>

Now the $result includes the array of objects (entities).

Entity class

The Entity class is the default class used as the result of objects. If there is a class with the model's name + 'Entity', that class is used instead. (i.e. For model "Post", the class "PostEntity" is used)

<?php
App::uses('Table', 'Entity.ORM');
App::uses('PostEntity', 'Model/Entity');

class Post extends Table {
}
?>
<?php
App::uses('Entity', 'Model/Entity');

class PostEntity extends Entity {
    // Your custom logic here
}
?>

Array access for Entity object

Entity's property's can be accessed using array syntax:

echo $post['title']; // == $post->title

Array access can also be used with Smarty:

Hello, my name is {$post.author.name|h}.

Array access introduces two important features:

  • Access control for security
  • Cache for performance

For more information

Introducing CakeEntity (PHP study in Tokyo 10/1/2011)

License

Copyright (c) 2012 Jose Diaz-Gonzalez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.