Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:36:15+00:00 2026-06-10T16:36:15+00:00

For some models, we have soft deletion implemented using a valid boolean in MySQL.

  • 0

For some models, we have soft deletion implemented using a valid boolean in MySQL.

In the class, the scopes method is defined as follows:

public function scopes() {
    return array(
        'valid'=>array(
            'condition'=>"t.valid=1",
        )
    );
}

This is so that when we load a model we can call the scope to make it include only valid (not deleted) models alongside the other find criteria, or whatever it happens to be.

This isn’t very DRY and I am wondering if there is an alternative way of achieving the same thing, that could perhaps be applied to an interface, the abstract Model class that all models derive from, or, if using 5.4, a trait.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:36:16+00:00Added an answer on June 10, 2026 at 4:36 pm

    Yii has a feature called Behaviors
    that is similar to php 5.4 traits but works with earlier versions too.

    SoftDeleteBehavior.php:

    class SoftDeleteBehavior extends CActiveRecordBehavior {
        public $deleteAttribute = 'valid';
        public $deletedValue = 0;
    
        public function beforeDelete($event) {
            if ($this->deleteAttribute !== null) {
                $this->getOwner()->{$this->deleteAttribute} = $this->deletedValue;
                $this->getOwner()->update(array($this->deleteAttribute));
    
                // prevent real deletion of record from database
                $event->isValid = false;
            }
        }
    
        /**
         * Default scope to be applied to active record's default scope.
         * ActiveRecord must call this from our own default scope.
         * @return array the scope to be applied to default scope
         */
        public function defaultScope() {
            return array(
                'condition' => $this->getOwner()->getTableAlias(false,false).'.'.$this->deleteAttribute
                    . ' <> '.var_export($this->deletedValue, true),
            );
        }
    }
    

    Then i have this class to apply deafultscope from behaviors:
    ActiveRecord.php (i ofcourse have more methods in this class, downside is that you need to call parent method if you need to extend the method):

    class ActiveRecord extends CActiveRecord {
    
        public function defaultScope() {
            $scope = new CDbCriteria();
    
            foreach ($this->behaviors() as $name => $value) {
                $behavior = $this->asa($name);
                if ($behavior->enabled && method_exists($behavior,'defaultScope')) {
                    $scope->mergeWith($behavior->defaultScope());
                }
            }
    
    
    
            return $scope;
        }
    }
    

    And then you use it in your Models:

    class MyModel extends ActiveRecord {
        public function behaviors() {
            return array(
                'SoftDeleteBehavior' => array(
                    'class' => 'application.components.behaviors.SoftDeleteBehavior',
                ),
            );
        }
    }
    

    PROTIP: you can specify your own ActiveRecord class when you generate models with gii

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have some models setup as follows: public class Form { public int FormId
I have defined some models like this (Entity Framework Code-First): public class A {
I have some models witch are using Doctrine nestedset feature. I want to add
I've encountered some problems with MongoID. I have three models: require 'mongoid' class Configuration
I have some Models in my app like so: public class Enquiry { [Key]
I have some models like these: class Alpha < ActiveRecord::Base has_many :items end class
If you have some models: class Teacher(models.Model): name = models.CharField(max_length=50) class Student(models.Model): age =
I have some models, connected with GenericForeignKey : Class Main(models.Model) filed_1 = models.CharField(max_length=20) object_id
I have some models as follows: Product: id name price Order: id user created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.