CodeIgniter:防止 row_array() 与 row() 拼写错误?
现在,在获取单行结果时,我两次错误地使用 row() 而不是 row_array() 。通常,它不会受到检查,也不会发出任何警告消息。 15-30 分钟后我终于注意到了这个问题;呵呵呵!
有谁对防止这种错误的方法有什么好的建议吗?对象格式不会引发 PHP 警告,但是,当然,当您希望从模型方法获取数组时,代码将不起作用。
我不小心使用了:
if ($query->num_rows() > 0) {
return $query->row();
}
应该是:
if ($query->num_rows() > 0) {
return $query->row_array();
}
我考虑重写 row() 数据库方法并添加 log_message() 语句,这至少会让我知道(在调试级别)我正在使用对象格式,而不是更典型的(对于我的代码)格式。
Twice now I've mistakenly used row() instead of row_array() when fetching a single row result. Usually, it goes unchecked without any warning messages. 15-30 minutes later I finally notice the issue; doh doh!
Does anyone have any good suggestions as to ways to prevent this mistake. The object format doesn't throw a PHP warning, but - of course - the code doesn't work when you expect an array from a model method.
I accidentally used:
if ($query->num_rows() > 0) {
return $query->row();
}
Which should instead be:
if ($query->num_rows() > 0) {
return $query->row_array();
}
I thought about overriding the row() database method and adding in a log_message() statement that would at least let me know (on debug level) that I'm using the object format, instead of the much more typical (for my code) format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何避免在 CodeIgniter 模型方法中出现数据类型错误?很简单,声明方法的返回数据类型。
如果您的返回数据与声明的类型(
:
之后)不匹配,那么 php 将为您生成错误。让我们借此机会消除任何混乱...
如果您调用
row()
或row_array()
并且您的 sql 逻辑提供了超过一行数据——那么您只会获得结果集的第一行。如果调用
row()
但结果集中没有行,则返回值为null
。如果调用
row_array()
但结果集中没有行,您将收到一个空数组作为返回值。因此,
row()
可能返回可迭代或不可迭代的数据类型;而row_array()
始终可以安全地直接传递到迭代器(如foreach()
或array_map()
) - 它可能不会有任何东西要迭代。How do you avoid making data-type errors in your CodeIgniter model methods? Simple, declare your methods' return data type.
If your return data does not match the declared type(s) (after
:
), then php is going to generate errors for you.Let's take this opportunity to clear up any confusion...
If you call either
row()
orrow_array()
and your sql logic affords the possibility of more than one row of data -- then you are only going to get the first row of the result set.If you call
row()
but there are no rows in the result set, you will have a return value ofnull
.If you call
row_array()
but there are no rows in the result set, you will receive an empty array as the return value.Hence,
row()
may return an iterable or non-iterable data type; whereasrow_array()
can ALWAYS be safely passed directly into an iterator (likeforeach()
orarray_map()
) -- it just might not having anything to iterate.row() 返回对象类型,row_array() 返回数组。
所以使用row_array来解析DB中行数据的数据比较好。
row() returns object type, and row_array() returns array.
So it is better in parsing the data of row data in DB when using row_array.
“预防”的唯一方法就是再次学习文档,这样您就可以保持使用哪个函数的共同习惯。
您可以自己注意,如果要返回多个记录,请使用
row_array()
,对于单个记录,请使用>row()
函数。请记住,不要快速输入代码,请在继续之前逐步校对代码。
覆盖原始函数是不好的做法,在检索单个记录时使用 row() 函数很有用,因此不要这样做。
The only way to "prevent" is pretty much learn over the documentation again so you can keep a common habit of using which function.
You can note yourself, if you are going to return multiple records, use the
row_array()
, for a singular record, use therow()
function.Remember, don't speed type your code, proofread your code step by step before continuing.
Overriding the original function is bad practice, using the
row()
function is useful when retrieving a singular record, so don't do this.