查询yii2框架中的冒号是什么意思? [英] What does it mean the colon in queries yii2 framework?

查看:179
本文介绍了查询yii2框架中的冒号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是yii2的新手,我想知道查询中的冒号是什么意思?

I'm totally new in yii2 and I want to know what does it mean the colon in the query?

我已经研究了绑定参数,但是在yii2文档中说:

I have made a research about binding parameters, but in the yii2 documentation says:

// returns all inactive customers
$sql = 'SELECT * FROM customer WHERE status=:status';

哪一方来自数据库?左侧还是右侧?

which side is from the data base? the left side or the right side?

这是一个简单的文本,哪个是数据库中的一列?我很困惑.

which is a simple text and which one is a column from the DB? Im so confused.

在没有冒号的情况下进行查询的另一种方式是什么?有效吗?

what would be another way to make the query without the colon? is it valid?

在下一个示例中为什么有'anyo = **:**valor'?还有一些没有?

why it has 'anyo = **:**valor' in the next example? and some others dont?

$dbLibro = Libro::find()->where('tipo = "Nacimiento"')->andWhere('cerrado = 0')->andWhere('anyo = :valor',[':valor'=>date("Y")])->one();

我希望它的清楚原因是文档对我来说有点混乱.

I hope its clear cause the documentation is a bit confusing for me.

推荐答案

冒号与Yii2没有直接关系,而是与

The colons are not directly related with Yii2, it's related with PHP PDO extension that used by Yii2.

每个冒号都是占位符,以后用于绑定值.检查例如此问题.

Each colon is placeholder used later for binding value. Check for example this question.

如果我们在ActiveQuery中编写此查询:

If we write this query in ActiveQuery:

SELECT * FROM customer WHERE status = :status

我们可以得到这样的东西:

we can get something like this:

$query = Customer::find()->where('status = :status', [':status' => Customer::STATUS_ACTIVE]);

假设STATUS_ACTIVE常量等于1,执行后将转换为:

Assuming STATUS_ACTIVE constant equals to 1, after execution it transforms to this:

SELECT * FROM "customer" WHERE status = 1

因此,左侧(等于之前)代表列名,右侧代表-值,之后将被安全地绑定.

So the left side (before equals) represents column name, right part - value which will be safely binded after.

但是您不必自己编写参数,Yii2 QueryBuilder会自动为您生成参数.

But you don't have to write params by yourself, Yii2 QueryBuilder generates it automatically for you.

还有其他不用冒号编写查询的方法,它们的使用频率更高.该查询可以这样写:

There are other ways to write query without colons and they are used more often. This query can be written like this:

$query = Customer::find(['status' => Customer::STATUS_ACTIVE]);
$models = $query->all();

或者使用快捷方式这样:

Or like this using shortcut:

$models = Customer::findAll(['status' => Customer::STATUS_ACTIVE]);

或者甚至可以将其放在

Or it can be even put inside of a scope:

$models = Customer::find()->active();

在这种情况下,Yii会自动生成参数,这将等效于此:

In this case Yii generates parameters automatically and it will be equivalent to this:

SELECT * FROM "customer" WHERE "status"=:qp1

1将绑定到:qp1参数,请注意,在这种情况下,列名也用双引号引起来.

Value 1 will be binded to :qp1 parameter, note that in this case column names are also double quoted.

如果尝试使用更多条件,则参数将为:qp2:qp3等(默认PARAM_PREFIX:qp).

If you try to use more conditions, params will be :qp2, :qp3 and so on (default PARAM_PREFIX is :qp).

对于您的第二个查询,可以这样重写:

As for your second query, it can be rewritten like this:

$model = Libro::find()
    ->where([
        'tipo' => 'Nacimiento',
        'cerrado' => 0,
        'anyo' => date('Y'),
    ])->one();

在这种状态下,这种查询看起来更好并且可读性更好.

Such queries look way better and readable in this state.

Yii2允许在查询中生成甚至更复杂的条件,请检查本部分以获取更多详细信息.

Yii2 allows generate even more complex conditions in queries, check this section of the docs for more details.

PS .最好在代码中使用英文命名.考虑其他支持您的代码的国际开发人员.可以使用数据库功能根据使用的RDBMS计算date('Y').

P.S. It's better to use english for naming in your code. Think about other international developers supporting your code. date('Y') can be calculated using database functions depending on used RDBMS.

这篇关于查询yii2框架中的冒号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆