CodeIgniter 的 get() 活动记录方法的用途是什么?

发布于 2024-11-28 15:50:01 字数 485 浏览 3 评论 0原文

我在我的应用程序中使用 CodeIgniter,但我有点困惑。我写了一些这样的查询:

public function checkemail($email) {
    $this->db->select('email')->from('user')->where('email', $email);
}

但是在CodeIgniter活动记录手册中,他们谈论$this->db->get()

我应该在 $this->db->select() 查询之后添加它吗?

我的函数不会产生任何错误。

我什么时候应该使用get()

I am using CodeIgniter for my application and I am a bit confused. I wrote some queries like this:

public function checkemail($email) {
    $this->db->select('email')->from('user')->where('email', $email);
}

But in the CodeIgniter active record manual, they talk about $this->db->get().

Should I add it after the $this->db->select() query?

My function doesn't produce any errors.

When should I use get()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦浮鱼 2024-12-05 15:50:02

是的,您需要在其他方法之后运行 get()select()from()where() 将各自的语句添加到查询中,而 get() 实际运行查询并返回结果作为对象。

在这种情况下,您可以将其添加到链的末尾。

public function checkemail($email) {
    $this->db
        ->select('email')
        ->from('user')
        ->where('email', $email)
        ->get();
}

如果您想稍后使用结果,请确保将其分配给变量。

$user = $this->db
    ->select('email')
    ->from('user')
    ->where('email', $email)
    ->get();

Yes, you'll need to run get() after the other methods. select(), from() and where() add their respective statements to the query, and get() actually runs the query and returns the result as an object.

In this case, you could just add it on to the end of the chain.

public function checkemail($email) {
    $this->db
        ->select('email')
        ->from('user')
        ->where('email', $email)
        ->get();
}

If you want to work with the result afterwards, make sure that you are assigning it to a variable.

$user = $this->db
    ->select('email')
    ->from('user')
    ->where('email', $email)
    ->get();
如若梦似彩虹 2024-12-05 15:50:02

如果您使用get("table_name"),则不需要使用from("table_name")。这似乎只是一种替代语法。

用户指南中,一直在底部显示:如图所示之前,查询的 FROM 部分可以在 $this->db->get() 函数中指定,因此请使用您喜欢的任何方法。

If you use get("table_name") then you don't need to use from("table_name"). It's just an alternative syntax it seems.

From the user guide, all the way at the bottom it says: As shown earlier, the FROM portion of your query can be specified in the $this->db->get() function, so use whichever method you prefer.

じее 2024-12-05 15:50:02

当前的脚本是无用的,因为即使您使用 get() 执行构建的查询,您的方法也不会将找到的 email 值返回给调用脚本。

在我看来,您的脚本意图很可能是检查用户表中是否存在传入的电子邮件字符串。如果这是真的,那么我建议返回一个布尔类型值。

public function checkemail(string $email): bool
{
    return (bool) $this->db
        ->where('email', $email)
        ->count_all_results('user');  
}

如果由于某种奇怪的原因,您实际上希望返回传入的字符串,那么您需要返回结果行对象的 email 属性。如果没有名为 email 的行或属性,则返回 null

在下面的脚本中,组合了 from() 方法、where() 方法和(查询执行)get() 方法如get_where()row() 用于指示结果数据应该是 1 一维对象或 null

public function checkemail(string $email): ?string
{
    return $this->db
        ->get_where('user', ['email' => $email])
        ->row()
        ->email ?? null;
}

The current scripting is useless because even if you execute the built query with get(), your method isn't returning the found email value to the calling script.

It seems most likely to me that your scripting intention is to check if the passed-in email string exists in the user table. If this is true, then I recommend returning a boolean type value.

public function checkemail(string $email): bool
{
    return (bool) $this->db
        ->where('email', $email)
        ->count_all_results('user');  
}

If, for some odd reason, you actually wish to return the passed-in string, then you'll need to return the email property of the result row object. If there is no row or no property named email, return null.

In the below script, the from() method, the where() method, and the (query executing) get() method are combined as get_where(). row() is used to dictate that the result data should be either a 1 one dimensional object or null.

public function checkemail(string $email): ?string
{
    return $this->db
        ->get_where('user', ['email' => $email])
        ->row()
        ->email ?? null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文