Skip to content

Commit 1d3336b

Browse files
authored
Merge pull request #9087 from kenjis/docs-upgrade-model
docs: improve Upgrade Models sample code
2 parents f9d77a2 + c53bde5 commit 1d3336b

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

user_guide_src/source/installation/upgrade_models.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Upgrade Guide
2424
2. Add this line just after the opening php tag: ``namespace App\Models;``.
2525
3. Below the ``namespace App\Models;`` line add this line: ``use CodeIgniter\Model;``.
2626
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');``.
2830

2931
If you use sub-directories in your model structure you have to change the namespace according to that.
3032
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**:
5153

5254
.. literalinclude:: upgrade_models/001.php
5355

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:
61+
62+
.. literalinclude:: upgrade_models/002.php

user_guide_src/source/installation/upgrade_models/001.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44

55
use CodeIgniter\Model;
66

7-
class UserContact extends Model
7+
class NewsModel extends Model
88
{
9-
// insert() method already implemented in parent
9+
// Sets the table name.
10+
protected $table = 'news';
11+
12+
public function setNews($title, $slug, $text)
13+
{
14+
$data = [
15+
'title' => $title,
16+
'slug' => $slug,
17+
'text' => $text,
18+
];
19+
20+
// Gets the Query Builder for the table, and calls `insert()`.
21+
return $this->builder()->insert($data);
22+
}
1023
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use CodeIgniter\Model;
6+
7+
class NewsModel extends Model
8+
{
9+
// Sets the table name.
10+
protected $table = 'news';
11+
12+
// Sets the field names to allow to insert/update.
13+
protected $allowedFields = ['title', 'slug', 'text'];
14+
15+
public function setNews($title, $slug, $text)
16+
{
17+
$data = [
18+
'title' => $title,
19+
'slug' => $slug,
20+
'text' => $text,
21+
];
22+
23+
// Uses Model's`insert()` method.
24+
return $this->insert($data);
25+
}
26+
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
class User_contact extends CI_Model
3+
class News_model extends CI_Model
44
{
5-
public function insert($name, $address, $email)
5+
public function set_news($title, $slug, $text)
66
{
7-
$this->db->insert('user_contacts', array(
8-
'name' => $name,
9-
'address' => $address,
10-
'email' => $email,
11-
));
7+
$data = array(
8+
'title' => $title,
9+
'slug' => $slug,
10+
'text' => $text,
11+
);
12+
13+
return $this->db->insert('news', $data);
1214
}
1315
}

0 commit comments

Comments
 (0)