Open Krozark opened 9 years ago
At this time, the solution 1 appear to be the best one to deal with down casting. But I've no idea how to implement it right now.
Maybe using something like:
class A : public SqlObject<A>
{
// ... code
}
class B : public SqlExtendObject<A>
{
// other code
}
done since d8ec30619c94eba2e6064e1467d9477f0744b8f9 .
class A : public SqlObject<A>
{
orm::intergerField a;
MAKE_STATIC_COLUMN(i)
}
//Usual MACROS
class B : public SqlExtendObject<B,A>
{
orm::intergerField b;
MAKE_STATIC_COLUMN(b);
}
//Usual MACROS
internaly B contain a FK that point to its A base.
A | B |
---|---|
int pk | int pk |
int a | int b |
int _base_obj_ptr (reference A pk) |
All query are supported, BUT you HAVE TO use the FK to make query on the base object of B (A).
auto list = B::all(); //no changes
auto list = B::query().filter(
orm::Q<B>(18,orm::op::gt,B::$b)
); //query on B object
auto list = B::query().filter(
orm::Q<B>(12,orm::op::gt,B::$base_obj_ptr,A::$a)
); //query on A base using the FK
B::type_ptr b = B::create();
/// ...
b->save(); //will save all the bases of B (B and A)
A::type_ptr a = b; //this will works. point to the same object
/// ...
a->save(); // will save all the object (B and A bases)
There is 3 possibilities with 2 classes
1
Each class as it's own table. B refer to a A table line through the a_ptr FK.
2
in this case, A is an abstract class that is not stored in the database. All the attributes of A are forwards to its children.
3
in this case, B is not store in database. In fact, B use the A table as it's own. The idea is to add into the A table all the attributes of B. Problem : what we do if there is a A and B instance referring to the same object in the database?