1

I'm trying to use the findOn from within the class that I want to search. Is this possible or is there a better way?

class CmsSettings extends ActiveRecord
{

   public static function tableName()
   {
       return 'cms_settings';
   }

   //not working
   public static function run(){

       $results = CmsSettings::findOn(1):

       return $results;

   }

   // not working
   public static function check(){

       $results = CmsSettings::findOn(1):

       if($results->somesetting){
          return true;
       }

   }
}
2
  • 1
    self or static Commented Jun 18, 2018 at 8:33
  • What do you mean by "not working"? Commented Jun 18, 2018 at 9:36

2 Answers 2

2

You should probably use static::findOne(1). By using self or CmsSettings you are just hardcoding returned type, which makes this class less flexible and will give you unexpected results on inheritance. For example if you create child model which extends your class:

class CmsSettings extends ActiveRecord {

    public static function run() {
        $results = CmsSettings::findOne(1);

        return $results;
    }

    // ...
}

class ChildCmsSettings extends CmsSettings {

}

You expect that ChildCmsSettings::run() will return instance of ChildCmsSettings. Wrong - you will get CmsSettings. But if you write this method with using static:

public static function run() {
    $results = static::findOne(1);

    return $results;
}

You will get instance of class which you're used for call run() - ChildCmsSettings.

Sign up to request clarification or add additional context in comments.

Comments

1

Use self

Refer findOne()

class CmsSettings extends ActiveRecord
{
   public static function tableName()
   {
      return 'cms_settings';
   }

   public static function run() 
   {
      $results = self::findOne(1);
      return $results;
   }

   public static function check() 
   {
      $results = self::findOne(1);

      if ($results->somesetting) {
         return true;
      }

   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.