在CakePHP中使用Count

时间:2015-05-21 08:36:06

标签: cakephp cakephp-2.5

我正在尝试使用count函数,这就是它的样子:

$sql = "SELECT COUNT(Live) as c FROM tapplicant WHERE CompletedDate >= CURDATE() ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo  $row['c'] ; 

我如何使用cakePHP方式转换它? ,我试过了:

$count = $this->Article->find('count', 
                                    array('conditions' => array('Tapplicant.Live')));

然后查看$ count的值:

<?php echo $count ?>

我试过了:

        $this->Tapplicant = array(
    'c' => 'COUNT(*)',
);
$options = array(
    'fields' => array(
        'Tapplicant.c',
    ),
);
$data = $this->find('all', $options);
   $this->set('data', $data );

基本上我只是想计算tapplicant.Live的值,其中有5条记录。

2 个答案:

答案 0 :(得分:2)

你几乎拥有它。在您的情况下,值数组应该有2个字段。第1列是列名,第2是条件的值。

考虑到您已将Tapplicant表正确绑定到文章模型:

$count = $this->Article->find('count', array('conditions' => array('Live >=' => 'CURDATE()' )));

或者就是这样,如果你想计算所有行:

$count = $this->Article->find('count');

答案 1 :(得分:1)

您需要像下面这样设置条件数组: -

TopAnimation.Completed += (s,e) =>
{
   _MyWindow.BeginAnimation(Window.TopProperty, null);
};