Nanne Huiges Fri Aug 19 03:55:38 -0400 2011

Subject: Read Only

I have a model for a table that should only be read, as it is filled automatically (using triggers). I know I can make an association readonly, and a finder can specify this too, but I am looking for a way to add something to the class that makes all instances readonly.

The class is empty(-ish), meaning it only has 1 `belongs to` and nothing more, so there's absolutely nothing special going on. The only thing I could come up with was to override the is_readonly() function to return 'true' alwasy. Is that even allowed?

So my question: is there a way to say to a Model (class, as opposed to instance or association) is readonly?

Clay vanSchalkwijk Wed Sep 07 18:27:44 -0400 2011

Ideally it would be really easy with a $before_save filter of 'readonly', but unfortunately it verifies reading before it process the before_* callbacks. Create a function inside your model that will return false (or throw an exception) and add it to the before_save callback which will prevent updates and inserts from going through.

1 class Order extends ActiveRecord\Model {
2   static $before_save = array('can_write'); # new OR updated records
4   public function can_write()
5   {
6     return false;
7   )
8 }

Nanne Huiges Thu Sep 08 02:14:50 -0400 2011

That's probably the closest one can come to a 'neat' sollution. Although I would change that functionname to "can write" or something, because now you have a "read_only" function that returns false, which means it is read only. A bit confusing :)

Clay vanSchalkwijk Sun Sep 11 17:46:00 -0400 2011

I went back through the model and there was a read only patch a while ago, you can just do this in your model instead of the callback hack above.

1 class Order extends ActiveRecord\Model {
2   static $readonly = true;
3 }

Documentation really needs to be updated ;)

Nanne Huiges Tue Sep 13 03:51:57 -0400 2011

That looks good! Currently we use 1.0, -and rudely crossposting -, do you have any idea what version would be a good one to use that has these new patches?

I'm not really that comfortable just using a nightly build, as if there's a buggy commit between the one that is proven to work and the current one....well, you get what I mean :)

Clay vanSchalkwijk Tue Sep 13 10:45:12 -0400 2011

There are about 150 or so commits since 1.0 and a good chunk of that were new features. All patches require that a test be submitted with them so nightly is probably your best bet until we can do more formal 1.1 release.

For now, you should fork over what is up there and catch your AR up to the current nightly and test it against your application. All tests are passing so everything you're using in 1.0 should not be breaking by pulling down the latest nightly. That build is a daily snapshot of the master branch so there is QA put into it before it even makes it onto that branch.

Nanne Huiges Wed Sep 21 10:18:07 -0400 2011

Hmm, I'm currently testing the latest "nightly" from 13 september, but that readonly function doesn't seem to work: it's either not in that release or its not working like I expect.

I can both change a certain found object, and I can make new ones and save those.

Any thoughts on that?

Kien La Thu Sep 22 00:32:49 -0400 2011

There is actually no "static $readonly = true".

Your original method of overriding is_readonly() would be the best way to do this and yes is allowed.

Nanne Huiges Thu Sep 22 05:32:21 -0400 2011

Ah, Thanks!

@clay: Curious as to where the 'static $readonly' came from? is that a commit that was in a seperate branch?

Clay vanSchalkwijk Tue Sep 27 14:22:38 -0400 2011

It's $__readonly, the variable is there and it checks against it prior to running a query... I just need to see if it will get stamped over when creating a connection / model is instantiated. I will test it out and let you know.