Builder Tables#3794
Conversation
|
I encountered this issue recently and never thought it was a bug. I was working with multiple DB tables in the same model class and thought that maybe Models were originally conceived to be one DB table per Model class. My solution for this is rather simple but meant for one-time consumption only. /**
* Always ensure the model is using the right table when
* model uses multiple tables.
*
* @param string $table
*
* @return \CodeIgniter\Database\BaseBuilder
*/
protected function ensureBuilderUsesTable(string $table)
{
$this->builder = $this->db->table($table);
return $this->builder;
}Then it is used like this: $this->ensureBuilderUsesTable('users');
$this->insert($data);
return $this->ensureBuilderUsesTable('logins')->delete('id', $id); |
|
Elegant! I don't think it solves the bug but it is a good workaround. I do think a model should be reference a single table only, and honestly the functionality of calling |
|
Removing |
|
If use of |
|
I think what I would (ideally) like to see is |
|
I suppose an alternate solution that might head more in the deprecation direction would be:
|
|
I like the implementation. It makes it work like I thought it always worked lol. Pretty sure I'd used it like that before. But the intent was always |
|
Good to know @lonnieezell! I like that this cleans things up a bit beyond the bug itself; I don't like the table tracking on |
michalsn
left a comment
There was a problem hiding this comment.
I like these changes.
Also, I'm not a fan of the idea of depreciating the builder() method or changing the visibility. This method is widely used in the Model and probably in the many users' implementations. It would be a "cosmetic" change that would make users' life harder without any (really good) reason. At least this is how I see it.
|
I am not a fan of having an extra property of BaseBuilder for the raw table name which is quite different from the processed I have an alternate idea though not yet tested. In the |
|
I like the static idea but I'm afraid it could cause too much confusion, as Builders carry "state" with them. Contrived example, but if I use |
|
@paulbalandan Ball is in your court! |
|
I guess you're right. I haven't considered that far. If that's the case then I suppose this fix is the most appropriate. |
|
Can we at least then mention in the user guide that |
Description
See #3793 for the underlying issue
This one might be controversial, but there's a bug and I have not found a simpler fix.
Model::builder($tableName)will return an existing shared instance of theBuilderregardless of$tableName's value. This is for a couple reasons:builder()is protected and the__call()method that loads it in turn does not pass on parametersbuilder()detects a existing shared instance it will return it immediately, regardless of the table nameThis PR attempts to fix the issue by:
$tableNameproperty forBuilder, assigned during initializationModel::builder()to be public (also cleans up some unnecessary__call()steps)Builder::getTable()to check the table against a requested name in the parameterChecklist: