You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/installation/upgrade_models.rst
+10-2Lines changed: 10 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,9 @@ Upgrade Guide
24
24
2. Add this line just after the opening php tag: ``namespace App\Models;``.
25
25
3. Below the ``namespace App\Models;`` line add this line: ``use CodeIgniter\Model;``.
26
26
4. Replace ``extends CI_Model`` with ``extends Model``.
27
-
5. Instead of CI3's ``$this->load->model('x');``, you would now use ``$this->x = new X();``, following namespaced conventions for your component. Alternatively, you can use the :php:func:`model()` function: ``$this->x = model('X');``.
27
+
5. Add the ``protected $table`` property and set the table name.
28
+
6. Add the ``protected $allowedFields`` property and set the array of field names to allow to insert/update.
29
+
7. Instead of CI3's ``$this->load->model('x');``, you would now use ``$this->x = new X();``, following namespaced conventions for your component. Alternatively, you can use the :php:func:`model()` function: ``$this->x = model('X');``.
28
30
29
31
If you use sub-directories in your model structure you have to change the namespace according to that.
30
32
Example: You have a version 3 model located in **application/models/users/user_contact.php** the namespace has to be ``namespace App\Models\Users;`` and the model path in the version 4 should look like this: **app/Models/Users/UserContact.php**
@@ -51,4 +53,10 @@ Path: **app/Models**:
51
53
52
54
.. literalinclude:: upgrade_models/001.php
53
55
54
-
To insert data you can just directly call the ``$model->insert()`` method because this method is built-in since CI4.
56
+
The above code is direct translation from CI3 to CI4. It uses Query Builder
57
+
directly in the model. Note that when you use Query Builder directly, you cannot
58
+
use features in CodeIgniter's Model.
59
+
60
+
If you want to use CodeIgniter's Model features, the code will be:
0 commit comments