j4mie / paris

A lightweight Active Record implementation for PHP5, built on top of Idiorm.
http://j4mie.github.com/idiormandparis/
996 stars 131 forks source link

Feature rq: Model->setId() and isDirty([field]) #10

Closed arcijsg closed 11 years ago

arcijsg commented 13 years ago

Hi, J4mie!

After some time back to my IdiORM-izing and Paris-ing code, I found two things to be useful:

(1) setId() instance method for Model (helpful if working with legacy database, with quite randomly named primary key columns): this one cannot be implemented in clean way by extending Model class as Model::orm->_get_id_column_name() is protected, so I ended up with:

public function setId($id) {
   $idColumn = self::_get_id_column_name(get_class());
   return $this->set($idColumn, $id);
}

(2) method(-s) to check, whether given field or any fields of Model object are dirty (changed by set())

Thanks for the good job!

j4mie commented 13 years ago

Hi,

For point (1), I think your solution is probably the correct way to go. It seems to me that setting the ID of an object is actually quite a rare thing (I assume you're using some sort of UUID source to generate IDs rather than letting the database deal with it?)

Point (2) looks like a good idea, I'll add it to my todo list.

arcijsg commented 13 years ago

For (1) - reason is quite simple really - I often have to create several new model objects, linked through many-to-many table (having some additional attributes), and save all them in a single transaction - so, row IDs are generated by database for rows of data tables, but I set them manually to create link object.

j4mie commented 13 years ago

The develop branches of both Paris and Idiorm now have an is_dirty method. Would you mind giving these a try?

Cheers

arcijsg commented 13 years ago

Hi, J4mie - is_dirty() worked for me - thanks!

treffynnon commented 11 years ago

This looks like it has be resolved. With regard point one this is something that could easily be implemented on per project basis with a class abstract.