Code Monkey home page Code Monkey logo

cadvancedarbehavior's Introduction

The CAdvancedArBehavior extension adds up some functionality to the default possibilites of yii´s ActiveRecord implementation. At the moment it is able to automatically save MANY_MANY relation objects when save()-ing an Object.

Changelog

  • Version 0.4 added 14.08.2011 by kirs

Relations will be destroyed after model deletion, duplications of many2many relations won't be saved.

  • Version 0.3 added 25.05.2011 by thyseus

added $ignoreRelations to ignore specified relations. The Behavior will take all found many2many relations by default. Specify exceptions in this array. fixes all the bugs and glitches found in the discussion

Installation

To use this extension, just copy this file to your extensions/ directory, add 'import' => 'application.extensions.CAdvancedArBehavior', [...] to your config/main.php and add this behavior to each model you would like to inherit the new possibilities.

public function behaviors(){
  return array( 'CAdvancedArBehavior' => array(
    'class' => 'application.extensions.CAdvancedArBehavior'));
}

Possibilities:

Better support of MANY_TO_MANY relations:

When we have defined a MANY_MANY relation in our relations() function, we are now able to add up instances of the foreign Model on the fly while saving our Model to the Database. Let´s assume the following Relation:

Post has: 'categories'=>array(self::MANY_MANY, 'Category', 'tbl_post_category(post_id, category_id)')

Category has: 'posts'=>array(self::MANY_MANY, 'Post', 'tbl_post_category(category_id, post_id)')

Now we can use the attribute 'categories' of our Post model to add up new rows to our MANY_MANY connection Table:

$post = new Post();
$post->categories = Category::model()->findAll();
$post->save();

This will save our new Post in the table Post, and in addition to this it updates our N:M-Table with every Category available in the Database.

We can further limit the Objects given to the attribute, and can also go the other Way around:

$category = new Category();
$category->posts = array(5, 6, 7, 10);
$category->save();

We can pass Object instances like in the first example, or a list of integers that representates the Primary key of the Foreign Table, so that the Posts with the id 5, 6, 7 and 10 get´s added up to our new Category.

Queries will be performed here, one for the Category-Model and four for the N:M-Table tbl_post_category. Note that this behavior could be tuned further in the future, so only one query get´s executed for the MANY_MANY Table.

We can also pass a single object or an single integer:

$category = new Category();
$category->posts = Post::model()->findByPk(12);
$category->posts = 12;
$category->save();

Credits

Originaly written by thesus.

In August 2011 extension was improved by Kirs for Navigator-edu.

cadvancedarbehavior's People

Contributors

kirs avatar pronskiy avatar

Stargazers

 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.