0

我想根据不同的时间计算租约

    $start_date = $end_date = $end_date1 = new \DateTime('first day of last month');

    $start_date = $start_date->format('Y-m-01 00:00:00');
    $end_date = $end_date->format('Y-m-t 23:59:59');
    $end_date1 = $end_date1->format('Y-m-'. date('d') .' 23:59:59');

    $start_date2 = $end_date2 = new \DateTime('now');
    $start_date2 = $start_date2->format('Y-m-01 00:00:00');
    $end_date2 = $end_date2->format('Y-m-d 23:59:59');

   $tenancyDetails = $this
        ->find()
        ->select([ 
            'total_last_month' => $this->find()->func()->count('Tenancy.id'),
            'count_last_month' => $this->find()->func()->count('Tenancy1.id'),
            'count_curr_month' => $this->find()->func()->count('Tenancy2.id'),
            'Tenancy.created', 'Tenancy1.created', 'Tenancy2.created',
        ])            
        ->leftJoin(['Tenancy1' => 'tenancy'],[
            'Tenancy1.active' => 1,
            'Tenancy1.stage !=' => 1,
            'Tenancy1.company_id' => $id,
        ])
        ->leftJoin(['Tenancy2' => 'tenancy'],[
            'Tenancy2.active' => 1,
            'Tenancy2.stage !=' => 1,
            'Tenancy2.company_id' => $id,
        ])
        ->where([
            'Tenancy.active' => 1,
            'Tenancy.stage !=' => 1,
            'Tenancy.company_id' => $id,
        ])
       ->where(function ($exp, $q) use ($start_date, $end_date) {
            return $exp->between('Tenancy.created', $start_date, $end_date);
        })
       ->where(function ($exp, $q) use ($start_date, $end_date1) {
            return $exp->between('Tenancy1.created', $start_date, $end_date1);
        })
       ->where(function ($exp, $q) use ($start_date2, $end_date2) {
            return $exp->between('Tenancy2.created', $start_date2, $end_date2);
        })->toArray();

这是我得到的。计数的数量是相同的。虽然日期不同

[
(int) 0 => object(App\Model\Entity\Tenancy) {

    'total_last_month' => (int) 4590,
    'count_last_month' => (int) 4590,
    'count_curr_month' => (int) 4590,
    'created' => object(Cake\I18n\FrozenTime) {

        'time' => '2016-03-01T10:57:21+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'Tenancy1' => [
        'created' => '2016-03-01 10:57:21'
    ],
    'Tenancy2' => [
        'created' => '2016-04-04 10:59:42'
    ],
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Tenancy'

}
]

即使我以这种方式编写了查询,但仍然是同样的问题。

    $query = $tenancies->find();
    $query->select([ 
        'total_last_month' => $query->func()->count('Tenancy.id'),
    ])
    ->where([
        'Tenancy.active' => 1,
        'Tenancy.stage !=' => 1,
        'Tenancy.company_id' => $id,
    ]) 
    ->where(function ($exp) use ($start_date, $end_date) {
        return $exp->between('Tenancy.created', $start_date, $end_date);
    });

    $query->select([ 
        'count_last_month' => $query->func()->count('Tenancy1.id'),
    ])
    ->leftJoin(['Tenancy1' => 'tenancy'],[
        'Tenancy1.company_id' => $id,
    ])
    ->where([
        'Tenancy1.active' => 1,
        'Tenancy1.stage !=' => 1,
    ]) 
    ->where(function ($exp) use ($start_date, $end_date1) {
        return $exp->between('Tenancy1.created', $start_date, $end_date1);
    });

我可以看到打印的查询是正确的。

4

1 回答 1

0

查看文档中的所有示例后,您似乎需要在使用 func() 帮助程序之前实例化 ORM 查询构建器实例。

http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions

$this->func()如果返回只是一个字符串,理论上应该是一样的。但可能会$this->func()导致您尝试构建的查询实例与未绑定的新实例之间断开连接?

尝试:

$tenancyDetails = $this>find();
$tenancyDetails->select([ 
            'total_last_month' => $tenancyDetails->find()->func()->count('Tenancy.id'),
            'count_last_month' => $tenancyDetails->find()->func()->count('Tenancy1.id'),
            'count_curr_month' => $tenancyDetails->find()->func()->count('Tenancy2.id'),
            'Tenancy.created',
            'Tenancy1.created',
            'Tenancy2.created',
        ])......        

编辑

我检查了文件中计数函数的代码/ORM/query.php。如果没有设置值 cake 将在 call 上执行 count() 查询$this->func->count()。PHP 编译器必须在构建查询之前读取并执行计数查询,这意味着在设置条件之前。这就是为什么您需要先实例化该类以停止计数查询过早执行的原因。

于 2016-04-17T04:41:58.953 回答