I am currently trying to manipulate a database with way too many
relations. i wont make it complex for you, so here is a very
simplified example:
my table (animals):
id, type, title
- 1, big, elephant
- 2, big, giraffe
- 3, small, cat
- 4, small, dog
what i want to do is to have two models “Big” and “Small”.
“Big” model will only take care of animals with type ‘big’ (elephant
and giraffe),
and “Small” model will take care of animals with type ‘small’ (cat and
dog).
is it possible to do?
Yes, you can create the model called BigAnimal and then define the $useTable = ‘animals’; Then define the beforeFind and the beforeSave to set the type to ‘big’ every time. Then you can do the same for the SmallAnimal model. Here is an example
While this is the way you can accomplish this, it seems it would be better to keep 1 to 1 model to table. Then you could add the necessary functions to query big and little animals all from the same model. It keeps the code a little cleaner. So something like: