php 在没有where子句的情况下计算yii2中表中的所有记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37338557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:10:29  来源:igfitidea点击:

Count all record in table in yii2 without where clause

phpyiiyii2yii2-advanced-app

提问by Yasin Patel

I want to count all record from table without specify any condition :

我想在不指定任何条件的情况下计算表中的所有记录:

now, i am doing by this way

现在,我这样做

$result['cms'] = Cms::find()->where([])->count();

and it will give me result,but i don't want to use whereclause.

它会给我结果,但我不想使用where子句。

So how to count all records without where clause.

那么如何在没有where 子句的情况计算所有记录

Thank you

谢谢

回答by scaisEdge

You can see this doc http://www.yiiframework.com/doc-2.0/yii-db-activequery.html

你可以看到这个文档http://www.yiiframework.com/doc-2.0/yii-db-activequery.html

simply using

简单地使用

count(): returns the result of a COUNT query.

count():返回 COUNT 查询的结果。

Cms::find()->count();

all(): returns an array of rows with each row being an associative array of name-value pairs.

all():返回一个行数组,每一行都是一个名称-值对的关联数组。

Cms::find()->all();

see this guide for more http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html

有关更多信息,请参阅本指南 http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html