Model Design Question - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Model Design Question (/showthread.php?tid=75033) |
Model Design Question - IVIike - 12-11-2019 Hello guys, I have a question regarding the design of the application and/or database tables. First, I have an (kind of) "abstract" class called AbstractClass where I define some variables and also a corresponding database table (having columns for the variables). Here comes the problem: If I then extend a SubClass and add some "other child" variables how can I load the child class from the database having also the variables of the parent class? As far as I tried it is not possible to load datas from a database VIEW or am I wrong? My current approach is to connect to the database table of the child and "inner join" the parent table. But only the child variables are loaded into the child class object where as the "var_dump" of the database request gives me all columns of the JOIN. Is there a mistake in my thought regarding the design or is this kind of situation not yet implemented? An similiar example is to have a variable as a custom class which needs to be loaded seperately as CI does not recognize it... I can provide example code if the question is unclear or needs clarification. RE: Model Design Question - vincent78 - 12-11-2019 Hello IVlike, This is not an easy task to manage Abstract/Concrete class/table. I've created an ORM that manages it: https://github.com/vmoulin78/concorde Here is an example: https://github.com/vmoulin78/demo-concorde-postgresql As you can see on the CDM: https://github.com/vmoulin78/demo-concorde-postgresql/blob/master/CDM.png there is an abstract table Person and two concrete tables Author and Commentator and the code: https://github.com/vmoulin78/demo-concorde-postgresql/blob/master/application/controllers/Blog_controller.php Author::find(1); (or Person::find(1); it's the same) You get an object Author with the Author properties and the Person properties If you have a question about creating the CDM, the SQL file for the database or about initializing the project, don't hesitate to post it here Vincent RE: Model Design Question - IVIike - 12-13-2019 Ok, but so to say there is no way to achieve this within CI.. @Vincent: Does you framework also work with CI4 as the documentation states CI3... RE: Model Design Question - MGatner - 12-13-2019 Code examples would help, as I don’t fully understand what your trying to do. RE: Model Design Question - vincent78 - 12-13-2019 The Concorde Project is based on CodeIgniter 3, so it is stable and ready for production use. The ORM is in the folder ./system/artefact The framework has been improved:
RE: Model Design Question - MGatner - 12-14-2019 @vincent78 this is the CodeIgniter 4 forum. RE: Model Design Question - IVIike - 12-16-2019 @MGatner: Here is my abstract class: PHP Code: <?php PHP Code: <?php If I then load the child from the database only the child variables are set: PHP Code: $db = \Config\Database::connect(); RE: Model Design Question - MGatner - 12-20-2019 Mike I think I get what you are trying to do, and I'm also not sure why it isn't working. First off though I would recommend making these classes in the Entities folder for framework consistency. You can do whatever you want of course, but in Model-View-Controller the model handles loading data from the database (you're using the Builder class instead, which is fine) and then the returned results are Entities (which can be arrays, generic objects, or a custom object class like yours). Just a suggestion. CustomResultObject should be setting a value for each field returned in the result set: PHP Code: foreach ($this->{$_data}[$i] as $key => $value) So first thing I'd check is that your result actually has the data. Something like: PHP Code: var_dump($db1->get()->getResultArray()); |