We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Cache ORM magic finders and Models with Getters and Setters

Hi,

I've got my models setup with getters and setters, e.g.

<?php

  namespace Common\Coopunity\Models;

  use Phalcon\Mvc\Model;

  /**
   * Class Communnities
   * @package Common\Coopunity\Models
   * @author Andre Figueira <[email protected]>
   */
  class Communities extends Model
  {
      /**
       * @var int
       */
      protected $id;

      /**
       * @var string
       */
      protected $name;

      /**
       * @var string
       */
      protected $permalink;

      /**
       * @var string
       */
      protected $description;

      /**
       * @var string
       */
      protected $date;

      /**
       * @var \Common\Coopunity\Models\CommunitySettings
       */
      public $settings;

      protected static $cache = array();

      /**
       * @return array
       */
      public function columnMap()
      {
          return array(
              'id' => 'id',
              'name' => 'name',
              'permalink' => 'permalink',
              'description' => 'description',
              'date' => 'date',
          );
      }

      /**
       * Initialize
       */
      public function initialize()
      {
          $this->hasMany(
              'id',
              'Common\Coopunity\Models\CommunitiesUsers',
              'communitiesId'
          );

          $this->hasManyToMany(
              'id',
              'Common\Coopunity\Models\CommunitiesStream',
              'communitiesId', 'streamId',
              'Common\Coopunity\Models\Stream',
              'id',
              array(
                  'alias' => 'stream',
              )
          );
      }

      /**
       * @return int
       */
      public function getId()
      {
          return $this->id;
      }

      /**
       * @param int $id
       *
       * @return $this
       */
      public function setId($id)
      {
          $this->id = $id;

          return $this;
      }

      /**
       * @return string
       */
      public function getName()
      {
          return $this->name;
      }

      /**
       * @param string $name
       *
       * @return $this
       */
      public function setName($name)
      {
          $this->name = $name;

          return $this;
      }

      /**
       * @return string
       */
      public function getPermalink()
      {
          return $this->permalink;
      }

      /**
       * @param string $permalink
       *
       * @return $this
       */
      public function setPermalink($permalink)
      {
          $this->permalink = $permalink;

          return $this;
      }

      /**
       * @return string
       */
      public function getDescription()
      {
          return $this->description;
      }

      /**
       * @param string $description
       *
       * @return $this
       */
      public function setDescription($description)
      {
          $this->description = $description;

          return $this;
      }

      /**
       * @return string
       */
      public function getDate()
      {
          return $this->date;
      }

      /**
       * @param string $date
       *
       * @return $this
       */
      public function setDate($date)
      {
          $this->date = $date;

          return $this;
      }

      /**
       * Fetches an instance of CommunitySettings for a community
       *
       * @param $communitiesId
       * @return CommunitySettings
       * @throws Exception
       */
      public static function fetchCommunitiesSettings($communitiesId)
      {
          $community = new Communities();

          $settings = $community->getModelsManager()->createBuilder()
              ->columns(array(
                  'Common\Coopunity\Models\Settings.key as key',
                  'Common\Coopunity\Models\CommunitiesSettings.value as value',
              ))
              ->from('Common\Coopunity\Models\Settings')
              ->leftJoin('Common\Coopunity\Models\CommunitiesSettings', 'Common\Coopunity\Models\CommunitiesSettings.communitiesId = "' . $communitiesId . '"')
              ->where('Common\Coopunity\Models\Settings.id = Common\Coopunity\Models\CommunitiesSettings.settingsId')
              ->andWhere('Common\Coopunity\Models\Settings.type = :type:', array('type' => Settings::SETTING_TYPE_COMMUNITY))
              ->getQuery()
              ->execute();

          $communitySettings = new CommunitySettings();

          foreach ($settings as $setting) {
              $setterNameKey = ucfirst($setting->key);
              $setterName = 'set' . $setterNameKey;

              if (method_exists($communitySettings, $setterName)) {
                  $communitySettings->{$setterName}($setting->value);
              } else {
                  throw new Exception('Non existent settings setter: ' . $setterName);
              }
          }

          return $communitySettings;
      }

      /**
       * After the Communities class has been instanciated fetches the community settings
       *
       * @throws Exception
       */
      public function afterFetch()
      {
          $this->settings = Communities::fetchCommunitiesSettings($this->id);
      }

      /**
       * After a Communities save, it runs through and saves related community settings
       *
       * @throws Exception
       */
      public function afterSave()
      {
          $settings = Settings::find('type = "' . Settings::SETTING_TYPE_COMMUNITY . '"');

          foreach ($settings as $setting) {
              $communitiesSettings = CommunitiesSettings::findFirst('communitiesId = "' . $this->getId() . '" AND settingsId = "' . $setting->getId() . '"');

              if (!$communitiesSettings) {
                  $communitiesSettings = new CommunitiesSettings();
              }

              $key = ucfirst($setting->getKey());
              $getterName = 'get' . $key;

              if (is_object($this->settings)) {
                  $value = $this->settings->{$getterName}();

                  if ($value != null) {
                      $communitiesSettings
                          ->setCommunitiesId($this->getId())
                          ->setSettingsId($setting->getId())
                          ->setValue($value);
                      ;

                      if (!$communitiesSettings->save()) {
                          throw new Exception('Failed to save user settings');
                      }
                  }
              }
          }
      }
  }

How can I cache this, I have my modelCache setup using Redis, In volt, I access the vars using the getters of the model.

Thanks in advance

You want to cache the various getXYZ() methods? Why? They're not doing any work, so caching won't really save you any performance.

Not exactly what I asked, I said, I want to cache the results, I have getters and setters, the values (properties) are set as protected, what's the best way to cache with Phalcon.

You want to cache the various getXYZ() methods? Why? They're not doing any work, so caching won't really save you any performance.



125.8k
Accepted
answer

In your initial request, you asked "How can I cache this". Since you mentioned getters/setters, I thought that's what you meant. Now you're saying you want to cache the results and I have to admit I'm not sure exactly what you want to cache. Could you describe a situation where this ideal cache would be used & accessed?