advancedrelationsbehavior This behavior helps me to update and delete models with HAS_ONE, HAS_MANY and MANY_MANY relations.

  1. Requirements
  2. Usage
  3. Advanced usage example
  4. Update

This behavior helps me to update and delete models with HAS_ONE, HAS_MANY and MANY_MANY relations.

Just give it a try)

Requirements

Yii 1.0.2 or above

Usage

To use this extension, just copy behavior class file to your /components/ directory, add this behavior to each model you would like to extend with new possibilities:

/**
 * This is the model class for table "blog_category".
 *
 * The followings are the available columns in table 'blog_category':
 * @property integer $id
 * @property string $parent_id
 * @property integer $sort
 */
class BlogCategory extends CActiveRecord
{
	...
	public function behaviors()
	{
		return array(
			'AdvancedRelationsBehavior' => array(
				'class' => 'AdvancedRelationsBehavior',
				'relations' => array(
					// HAS_MANY relations
					'categories',
					// MANY_MANY relations
					'posts',
				),
			),
		);
	}
	...
}

/**
 * This is the model class for table "blog_post_to_category".
 *
 * The followings are the available columns in table 'blog_post_to_category':
 * @property integer $post_id
 * @property integer $category_id
 * @property integer $sort
 */
class BlogPostToCategory extends CActiveRecord
{
	...
}

/**
 * This is the model class for table "blog_post".
 *
 * The followings are the available columns in table 'blog_post':
 * @property integer $id
 */
class BlogPost extends CActiveRecord
{
	...
	public function behaviors()
	{
		return array(
			'AdvancedRelationsBehavior' => array(
				'class' => 'AdvancedRelationsBehavior',
				'autoUpdate' => false,	// disable automatic update of related records
				'relations' => array(
					// MANY_MANY relations
					'categories',
					'tags', // same structure as `blog_post_to_category`
				),
			),
		);
	}
	...
}

After you've attached this behavior to models, you can use it. For example, to attach category model with PK 3 to categories with PK 1 and 2 now you can use code like this:

$model = BlogCategory::model()->findByPk(3);
$model->categories = array(1, 2);
$model->save();

Or to move all posts to category with PK 1:

$model = BlogCategory::model()->findByPk(1);
$model->posts = BlogPost::model()->findAll();
$model->save();

Also you can use manual update of relations. In the BlogPost model we disable auto-update, so we must call update method updateRelated() to update relations. For example, let's create BlogPost and attach it to all categories:

$model = new BlogPost;
$model->categories = BlogCategory::model()->findAll();
// update only `categories` relation, `tags` relation is ignored
if($model->save())
	$model->updateRelations('categories');

If sort order attribute was specified in the configuration, behavior will update it before save model. To disable this feature, just assign NULL as attribute name.

Advanced usage example

Note: saveWithRelated() and deleteWithRelated() methods added in version 1.0.5

/**
 * Task: build this categories tree, add "Welcome" post to each "Posts" sub-category
 * 	- News
 *		- Business
 * 			- Posts
 *		- Sci/Tech
 * 			- Posts
 * 		- Entertainment
 * 			- Posts
 * 		- Sports
 * 			- Posts
 * 		- Health
 * 			- Posts
 */

// the root category model
$rootCategory = new BlogCategory;
$rootCategory->title = "News";

// we cannot make direct modifications to the CActiveRecord relations,
// so we need to make a temporary buffer for this
$categories = array();

foreach(array(
	'Business',
	'Sci/Tech',
	'Entertainment',
	'Sports',
	'Health'
	) as $categoryTitle)
{
	$category = new BlogCategory;
	$category->title = $categoryTitle;

	// add sub-category
	$postsCategory = new BlogCategory;
	$postsCategory->title = "Posts";

	// add "Welcome" post to the "Posts"
	$post = new BlogPost;
	$post->title = "Welcome";
	$postsCategory->posts = $post;

	// save "Posts" category with "Welcome" post
	$postsCategory->saveWithRelated('posts');

	$category->categories = $postsCategory;

	$categories[] = $category;
}

$rootCategory->categories = $categories;

// save the categories tree
$rootCategory->saveWithRelated('categories');

Update

Version 1.0.9 saveWithRelated was fixed (update related models even if no changes in the owner model attributes)

Version 1.0.8 update doxygen comments for getRelations and updateRelated methods

Version 1.0.7 restore HAS_ONE relation data format after saving

Version 1.0.6 added support for both types of HAS_ONE and HAS_MANY relations

Version 1.0.5 added saveWithRelated() and deleteWithRelated() methods

Version 1.0.4 algorithm of updating HAS_MANY relations was fixed

Version 1.0.3 {{table}} parser RegExp fixed

Version 1.0.2 {{table}} parser RegExp updated

Version 1.0.1 HAS_MANY related records will be removed before saving.

9 0
15 followers
1 192 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Database
Developed by: Alex Jay
Created on: Mar 17, 2011
Last updated: 10 years ago

Downloads

show all

Related Extensions