@@ -27,10 +27,10 @@ and :doc:`Seeds <../dbmgmt/seeds>` to create more useful database setups later.
2727::
2828
2929 CREATE TABLE news (
30- id int(11) NOT NULL AUTO_INCREMENT,
31- title varchar (128) NOT NULL,
32- slug varchar (128) NOT NULL,
33- body text NOT NULL,
30+ id INT UNSIGNED NOT NULL AUTO_INCREMENT,
31+ title VARCHAR (128) NOT NULL,
32+ slug VARCHAR (128) NOT NULL,
33+ body TEXT NOT NULL,
3434 PRIMARY KEY (id),
3535 KEY slug (slug)
3636 );
@@ -111,22 +111,20 @@ following code to your model.
111111 return $this->findAll();
112112 }
113113
114- return $this->asArray()
115- ->where(['slug' => $slug])
116- ->first();
114+ return return $this->where(['slug' => $slug])->first();
117115 }
118116
119117With this code, you can perform two different queries. You can get all
120- news records, or get a news item by its ` slug < # >`_ . You might have
121- noticed that the ``$slug `` variable wasn't sanitized before running the
118+ news records, or get a news item by its slug. You might have
119+ noticed that the ``$slug `` variable wasn't escaped before running the
122120query; :doc: `Query Builder <../database/query_builder >` does this for you.
123121
124122The two methods used here, ``findAll() `` and ``first() ``, are provided
125- by the Model class. They already know the table to use based on the ``$table ``
123+ by the `` CodeIgniter\ Model`` class. They already know the table to use based on the ``$table ``
126124property we set in **NewsModel ** class, earlier. They are helper methods
127125that use the Query Builder to run their commands on the current table, and
128126returning an array of results in the format of your choice. In this example,
129- ``findAll() `` returns an array of objects .
127+ ``findAll() `` returns an array of array .
130128
131129Display the news
132130-------------------------------------------------------
@@ -150,14 +148,14 @@ a new ``News`` controller is defined. Create the new controller at
150148 {
151149 public function index()
152150 {
153- $model = new NewsModel( );
151+ $model = model(NewsModel::class );
154152
155153 $data['news'] = $model->getNews();
156154 }
157155
158156 public function view($slug = null)
159157 {
160- $model = new NewsModel( );
158+ $model = model(NewsModel::class );
161159
162160 $data['news'] = $model->getNews($slug);
163161 }
@@ -170,7 +168,13 @@ access to the current ``Request`` and ``Response`` objects, as well as the
170168``Logger `` class, for saving information to disk.
171169
172170Next, there are two methods, one to view all news items, and one for a specific
173- news item. You can see that the ``$slug `` variable is passed to the model's
171+ news item.
172+
173+ Next, the ``model() `` function is used to create the **NewsModel ** instance.
174+ This is a helper function. You can read more about it :doc: `here </general/common_functions >`.
175+ You could also write ``$model = new NewsModel(); ``, if you don't use it.
176+
177+ You can see that the ``$slug `` variable is passed to the model's
174178method in the second method. The model is using this slug to identify the
175179news item to be returned.
176180
@@ -180,7 +184,7 @@ the views. Modify the ``index()`` method to look like this::
180184
181185 public function index()
182186 {
183- $model = new NewsModel( );
187+ $model = model(NewsModel::class );
184188
185189 $data = [
186190 'news' => $model->getNews(),
@@ -202,7 +206,7 @@ and add the next piece of code.
202206
203207 <h2><?= esc($title) ?></h2>
204208
205- <?php if (! empty($news) && is_array($news)) : ?>
209+ <?php if (! empty($news) && is_array($news)): ?>
206210
207211 <?php foreach ($news as $news_item): ?>
208212
@@ -215,7 +219,7 @@ and add the next piece of code.
215219
216220 <?php endforeach; ?>
217221
218- <?php else : ?>
222+ <?php else: ?>
219223
220224 <h3>No News</h3>
221225
@@ -244,13 +248,12 @@ add some code to the controller and create a new view. Go back to the
244248
245249 public function view($slug = null)
246250 {
247- $model = new NewsModel( );
251+ $model = model(NewsModel::class );
248252
249253 $data['news'] = $model->getNews($slug);
250254
251- if (empty($data['news']))
252- {
253- throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: '. $slug);
255+ if (empty($data['news'])) {
256+ throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: ' . $slug);
254257 }
255258
256259 $data['title'] = $data['news']['title'];
0 commit comments