Code Monkey home page Code Monkey logo

Comments (4)

jpfuentes2 avatar jpfuentes2 commented on September 26, 2024
  1. You could achieve this effect in an after_construct callback if you wanted to keep the logic inside of your model and you're going to have an object.

    class Post
    {
      static $has_one = array('original_relationship')
      static $after_construct = array('set_has_one');
    
      public function set_has_one()
      {
        // change 
        self::$has_one = array('new_relationship');
        Table::clear_cache();
       }
    }
    
  2. Clear table cached and change Post's relationships:

    Table::clear_cache();
    Post::$has_one = array('new_relationship')
    

The main point here is that you need to call Table::clear_cache() because AR caches information about your table so that it doesn't load it every time (e.g. table fields, relationships, callbacks) a model is instantiated. This is not a good idea, however, because clearing the cache will mean it will need to re-cache any model you use again.

Could you instead define all the relationships with different attribute names and then dynamically determine which one to use? If i'm not understanding your problem correct -- please illustrate with some code.

On the totally different topic: I'm not sure what you mean. What do you mean having getters working exactly the same as setters?

from php-activerecord.

JoN1oP avatar JoN1oP commented on September 26, 2024

Thanks ! I tried it and it works like a charm :)
It could be useful to be able to clear the cache for a given model/class :
public static function clear_cache($model_class_name = NULL)
{
if($model_class_name !== NULL and array_key_exists($model_class_name, self::$cache)) {
unset(self::$cache[$model_class_name]);
} else {
self::$cache = array();
}
}

About the totally different topic, I mean, having getters work like setters do. See code below :
class My_Model extends ActiveRecord\Model {
static $getters = array('some_field');
static $setters = array('some_field');

    public function set_some_field($value) {
        $this->assign_attribute('some_field', serialize($value));
    }

    public function get_some_field() {
        return (($this->some_field !== NULL) ? unserialize($this->some_field) : array());
    }
}

from php-activerecord.

jpfuentes2 avatar jpfuentes2 commented on September 26, 2024

Yeah, after I wrote the comment I figured it wouldn't hurt anything to add an argument to the clear_cache method. I see what you mean about getters. I'm going to work on implementing that now.

from php-activerecord.

jpfuentes2 avatar jpfuentes2 commented on September 26, 2024

I setup clearing cache by a specific class and added getters. Thanks for your input.

from php-activerecord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.