Skip to content

Commit 83d0274

Browse files
committed
docs: add explanation for "Working with Query Builder"
1 parent d174a1b commit 83d0274

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

user_guide_src/source/models/model.rst

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,25 +610,49 @@ This is best used during cronjobs, data exports, or other large tasks.
610610
Working with Query Builder
611611
**************************
612612

613-
You can get access to a shared instance of the Query Builder for that model's database connection any time you
614-
need it:
613+
Getting Query Builder for the Model's Table
614+
===========================================
615+
616+
CodeIgniter Model has one instance of the Query Builder for that model's database connection.
617+
You can get access to the **shared** instance of the Query Builder any time you need it:
615618

616619
.. literalinclude:: model/043.php
617620

618-
This builder is already set up with the model's ``$table``. If you need access to another table
619-
you can pass it in as a parameter, but be aware that this will not return a shared instance:
621+
This builder is already set up with the model's ``$table``.
622+
623+
.. note:: Once you get the Query Builder instance, you can call methods of the
624+
:doc:`Query Builder <../database/query_builder>`.
625+
However, since Query Builder is not a Model, you cannot call methods of the Model.
626+
627+
Getting Query Builder for Another Table
628+
=======================================
629+
630+
If you need access to another table, you can get another instance of the Query Builder.
631+
Pass the table name in as a parameter, but be aware that this will **not** return
632+
a shared instance:
620633

621634
.. literalinclude:: model/044.php
622635

636+
Mixing Methods of Query Builder and Model
637+
=========================================
638+
623639
You can also use Query Builder methods and the Model's CRUD methods in the same chained call, allowing for
624640
very elegant use:
625641

626642
.. literalinclude:: model/045.php
627643

644+
In this case, it operates on the shared instance of the Query Builder held by the model.
645+
628646
.. important:: The Model does not provide a perfect interface to the Query Builder.
629647
The Model and the Query Builder are separate classes with different purposes.
630648
They should not be expected to return the same data.
631-
For example, if you need to get the compiledInsert you should do so directly on the builder instance.
649+
650+
If the Query Builder returns a result, it is returned as is.
651+
In that case, the result may be different from the one returned by the model's method
652+
and may not be what was expected. The model's event is not be triggered.
653+
654+
To prevent unexpected behavior, do not use Query Builder methods that return results
655+
and specify the model's method at the end of the method chaining.
632656

633657
.. note:: You can also access the model's database connection seamlessly:
634658

0 commit comments

Comments
 (0)