Yii 1.1: mod-active-record

Yii ActiveRecord with "ifModified then ..." logic and dependencies clearing
2 followers

Class ModActiveRecord is the base class for classes representing relational data. It provides methods to check if attributes were modified and clearing of relations than depends on modified attributes

Usage

Put the class ModActiveRecord into application/protected/components folder.

Change parent of your ActiveRecords classes from CActiveRecord to ModActiveRecord.

Write additional beforeSave() logic if you need to do something if attributes of instatnce were modified.

See samples at gist.github.com

/**
 * This is the model class for table "book".
 *
 * The followings are the available columns in table 'book':
 * @property string $book_id
 * @property string $title
 * @property string $author_id
 *
 * The followings are the available model relations:
 * @property Author $author
 */
class Book extends ModActiveRecord {
 
    // ...
 
    /**
     * Define relation with Author class by author_id attribute
     */
    public function relations()
    {
        return array(
            'author' => array(self::BELONGS_TO, 'Author', 'author_id'),
        );
    }
 
    /**
     * Do something before save() when author_id attribute is modified
     */
    public function beforeSave()
    {
        $authorModified = $this->getModified('author_id');
        if ($authorModified)
        {
            $booksCollection = new BooksCollectionCache;
            if ($authorModified['old'])
                echo "Say 'Goodbye!' to author #", $authorModified['old'], "\n";
            if ($authorModified['new'])
                echo "Say 'Hello!' to author #", $authorModified['new'], "\n";
        }
        return true;
    }
}
 
 
// ---
 
// 1. get old and new attribute values
$book = new Book;
$book->author_id = 1;
var_export($book->getIsModified('author_id')); // --> true
var_export($book->getModified('author_id')); // --> array('old'=>null, 'new'=>1);
 
// 2. clear dependencies
$book = new Book;
$book->author_id = 1;
echo $book->author->name; // --> Jack London
$book->author_id = 5;
echo $book->author->name; // --> Mark Twain 
 
// 3. do something before save if attributes were modified
$book->save(); // @see Book::beforeSave()
// --> Say "Hello" to author #5
// note: all isModified marks will be cleared after save()
var_export($book->getIsModified('author_id')); // --> false
var_export($book->getModified('author_id')); // --> null
 
// 4. reset modified marks
$book = new Book;
$book->author_id = 1;
$book->title = 'Tom Soyer';
var_export($book->getIsModified('author_id')); // --> true
var_export($book->getIsModified('title')); // --> true
$book->resetModified('author_id'); // forget about modified author_id
var_export($book->getIsModified('author_id')); // --> false
var_export($book->getModified('author_id')); // --> null
 
$book->resetModified(); // forget about any modified attribute
var_export($book->getIsModified()); // --> false
var_export($book->getModified('author_id')); // --> null
var_export($book->getModified('title')); // --> null

Resources

Total 1 comment

#17970 report it
copist at 2014/08/19 09:33am
Should I move this to wiki or leave as extension?

It is only one component in this extension. May be http://www.yiiframework.com/wiki/ is better place for it?

Leave a comment

Please to leave your comment.

Create extension