Giter Site home page Giter Site logo

cakephp-eager-loader's Introduction

Software License Build Status Coverage Status Scrutinizer

EagerLoader Plugin for CakePHP 2.x

An eager loading beahavior plugin for CakePHP 2.x which is highly compatible to the Containable behavior but generates better queries.

Requirements

  • CakePHP 2.x
  • PHP 5.3+

Installation

See the How to Install Plugins in the CakePHP documentation for general help.

  • Put the EagerLoader directory into your plugin directory or install the plugin with Composer from the directory where your composer.json file is located:
php composer.phar require chinpei215/cakephp-eager-loader
  • Load the plugin in your app/Config/bootstrap.php file:
CakePlugin::load('EagerLoader');
class Post extends AppModel {
    public $actsAs = array('EagerLoader.EagerLoader');
}

Usage

$Comment->find('first', [
	'contain' => [
		'Article.User.Profile',
		'User.Profile',
	]
]);

EagerLoaderBehavior has a high compatibility with ContainableBehavior, but generates better queries. In the above example, only 2 queries will be executed such as the following:

SELECT
	Comment.id, ...
FROM
	comments AS Comment
	LEFT JOIN articles AS Article ON (Comment.article_id = Article.id)
	LEFT JOIN users AS User ON (Article.user_id = User.id)
	LEFT JOIN profiles AS Profile ON (User.id = Profile.user_id)
WHERE
	1 = 1
SELECT
	User.id, ...
FROM
	users AS User
	LEFT JOIN profiles AS Profile ON (User.id = Profile.user_id)
WHERE
	User.id IN (1, 2, 3)

If using ContainableBehavior, how many queries are executed? 10 or more?

Incompatibility problems

EagerLoaderBehavior returns almost same results as ContainableBehavior, however you might encounter incompatibility problems between the 2 behaviors. For example EagerLoaderBehavior::contain() is not implemented yet.

Then disabling EagerLoaderBehavior on the fly, you can use ContainableBehavior::contain() instead:

$Comment->Behaviors->disable('EagerLoader');
$Comment->Behaviors->load('Containable');
$Comment->contain('Article');
$result = $Comment->find('first');

For your information, EagerLoaderBehavior can be coexistent with ContainableBehavior.

$actsAs = [
	'EagerLoader.EagerLoader', // Requires higher priority than Containable
	'Containable'
]

Using this way, you need not to call load('Containable') in the above example.

cakephp-eager-loader's People

Contributors

chinpei215 avatar ravage84 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.