sohoa / framework

Framework Sohoa
4 stars 4 forks source link

New feature for model #80

Closed thehawk970 closed 8 months ago

thehawk970 commented 10 years ago

I think its could be cool for sohoa :)

This mix for ActiveRecord Pattern and classic mapping model

You can use both $model['hello'] = 'world' and $model->insert('foo', 'bar');

Mapping layer

You use 4 methods map() , unmap($key, $value) , _update(Array $array) and mode()

map()

This function must return an array which use in the model

public function map() {
return ['foo' => 'bar'];
}

echo $model['foo']; // bar
Pro-tip

The parameter pass on the construct will be transmit to map function $model = new \Application\Model\Foobar($hello, $bar);

The function map will be execute with $this->map($hello, $bar);

unmap($key, $value)

This function will call on each value change if you change :

$model['foo'] = 'bar';
$model['bar'] = 'qux';

Will call 2 times the function unmap('foo' , 'bar') and unmap('bar', 'qux') Be careful for spamming !

_update

This function will call manually when you call $model->update() function with an only one argument which contains the changed value of your model

static mode ($save, $update)

This method configue the class $save has two values SAVE_IMMEDIATE and SAVE_MANU $update has too two values UPDATE_ALL_VALUE and UPDATE_CHANGE_VALUE

SAVE_IMMEDIATE : we call automatically the method unmap on update value SAVE_MANU : do nothing on update value, and need to call manually the ->update() method

UPDATE_ALL_VALUE : we send all value for _update, not operative in immediate option ! UPDATE_CHANGE_VALUE: send only the changed value of your model

ActiveRecord Pattern

Nothing on the data manipulation only some feature for send data to database, like ->sql($requete) which accept and \Hoa\Model\Query object or an string, an return the instance you can combine with all(), first(), last(), count() and item($id) i think this is logic :p

I had a little system for logging what happens and the number of result, you can get the result with ::getSqlLog() i think its usefull when you have tons of requetes :D

Have fun ps : I do some test :'D, and i code that for mojo but i think its cool for sohoa ps2 : sorry for my english and good for translate :'D

thehawk970 commented 10 years ago

Take a look to http://pastebin.com/PwqBYUaK i think its usefull to understand what i wrote :D

guiled commented 10 years ago

Désolé de venir aussi tardivement sur le sujet, mais je pense qu'il faut éviter le plus possible le fetchAll() systématique. Le chargement des données dans un tableau dès le lancement de la requête n'est pas une bonne idée, et on devrait plutôt avoir une activerecord sur un statement... @metalaka avait fait quelque chose d'intéressant à ce sujet