spadefoot / kohana-orm-leap

An ORM module for the Kohana PHP framework that is designed to work with all major databases.
http://spadefoot.github.io/kohana-orm-leap/
100 stars 25 forks source link

Modify value automatically, using callback, like ORM's filter() #55

Open taai opened 12 years ago

taai commented 12 years ago

In Kohana_ORM is possible to run a callback function that modifies the value when setting it on __set(). This is the same as using adaptors in Leap. But I think that it's not worth to create a custom adaptor class wich calls another class, instead of calling that another class in first place.

Example is the password hashing with Auth::instance()->hash('password1') - with Kohana_ORM you can define the callback in the Model like this:

<?php
public function filters()
{
    return array(
        'password' => array(
            array(array(Auth::instance(), 'hash'))
        )
    );
}

And you can define any callback functions there - even from the same Model.

As I have said earlyer, I find Leap potentialy very powerful, but it lacks of functionality that has in Kohana_ORM, and Leap should have all the functionality that has Kohana_ORM to be able to compete with it.

I have spent days trying to set everything up with Leap, but I'm stuck with all these functionalities missing from Kohana_ORM...

I hope that you will agree to implement all these things.

bluesnowman commented 12 years ago

Yes, I will be happy to get this added. This actually has been something that was in development but because we hadn't finished implementing it, I removed them because some were getting confused (thinking that it was implemented). I will totally agree with you as to making this apart of the project.

We are very thankful for you feedback.... It is very helpful in letting us know what is important to others and what things to focus on next.

taai commented 12 years ago

To do this I have to know how exactly the structure is built. Can you tell me if I'm right or not?!

There are 4 ways how the value is set: 1 internally - values right from database 2 from an adaptor 3 from alias 4 from your code (normal usage)

What first 3 (and also 4th) have in common? - They all use similar code like $model->fields[$column]->value

Possible solutions

Create another set-type: 1 "internal_value" that should be changed in many files 2 "user_value" that should be changed in "field.php" and "model.php" files Currently there are "value" and "modified" types. Look here ( https://github.com/spadefoot/kohana-orm-leap/blob/3.2/master/classes/base/db/orm/field.php#L111 ) to understand what I'm talking about.

So, the first 3 ways would either set value like $model->fields[$column]->internal_value, or when the values would be set from/to Model, it will set value like $this->fields[$column]->user_value.

Any comments about this approach? And which variant to make - 1st or 2nd? And what would be the best name for that set type? And if you agree that this is ok, then I could write a code that's doing these modification callbacks.