Coder Social home page Coder Social logo

terr / linkable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from x0bandeira/linkable

44.0 3.0 9.0 465 KB

Linkable plugin for CakePHP, with LinkableBehavior for data mining on deep relations between models.

Home Page: http://rafaelbandeira3.wordpress.com/2008/11/16/linkable-behavior-taking-it-easy-in-your-db/

PHP 100.00%

linkable's Introduction

Linkable Plugin

CakePHP plugin, PHP 5

Introduction

Linkable is a lightweight approach for data mining on deep relations between models. Joins tables based on model relations to easily enable right to left find operations.

Requirements

  • CakePHP 1.2.x or 1.3.x
  • PHP 5

Installation

  1. Download the latest release for your version of CakePHP or clone the Github repository

  2. Place the files in a directory called 'linkable' inside the app/plugins directory of your CakePHP project.

  3. Add the LinkableBehavior to a model or your AppModel:

    var $actsAs = array('Linkable.Linkable');

Usage

Use it as a option to a find call. For example, getting a Post record with their associated (belongsTo) author User record:

$this->Post->find('first', array(
	'link'	=> array(
		'User'
	)
));

This returns a Post record with it's associated User data. However, this isn't much different from what you can do Containable, and with the same amount of queries. Things start to change when linking hasMany or hasAndBelongsToMany associations.

Because Linkable uses joins instead of seperate queries to get associated models, it is possible to apply conditions that operate from right to left (Tag -> Post) on hasMany and hasAndBelongsToMany associations.

For example, finding all posts with a specific tag (hasAndBelongsToMany assocation):

$this->Post->find('all', array(
	'conditions'	=> array(
		'Tag.name'	=> 'CakePHP'
	),
	'link'	=> array(
		'Tag'
	)
));

But what if you would still like all associated tags for the posts, while still applying the condition from the previous example? Fortunately, Linkable works well together with Containable. This example also shows some of the options Linkable has:

$this->Post->find('all', array(
	'conditions'	=> array(
		'TagFilter.name'	=> 'CakePHP'
	),
	'link'	=> array(
		'PostsTag'	=> array(
			'TagFilter'	=> array(
				'class'			=> 'Tag',
				'conditions'	=> 'TagFilter.id = PostsTag.tag_id',	// Join condition (LEFT JOIN x ON ...)
				'fields'		=> array(
					'TagFilter.id'
				)
			)
		)
	),
    'contain'	=> array(
		'Tag'
	)
));

If you're thinking: yeesh, that is a lot of code, then I agree with you ;). Linkable's automagical handling of associations with non-standard names has room for improvement. Please, feel free to contribute to the project via GitHub.

Pagination

As a last example, pagination. This will find and paginate all posts with the tag 'CakePHP':

$this->paginate = array(
    'fields'    => array(
        'title'
    ),
    'conditions'	=> array(
		'Tag.name'	=> 'CakePHP'
	),
	'link'	=> array(
		'Tag'
	)
    'limit' => 10
);

$this->paginate('Post');

Notes

When fetching data in right to left operations, meaning in "one to many" relations (hasMany, hasAndBelongsToMany), it should be used in the opposite direction ("many to one"), i.e:

To fetch all Users assigned to a Project:

$this->Project->find('all', array('link' => 'User', 'conditions' => 'project_id = 1'));

This won't produce the desired result as only a single user will be returned.

$this->User->find('all', array('link' => 'Project', 'conditions' => 'project_id = 1'));

This will fetch all users related to the specified project in one query.

Authors

License

Licensed under The MIT License Redistributions of files must retain the above copyright notice.

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.