CakePHP 3 - finder无法加载正确的数据

时间:2017-02-20 06:09:30

标签: mysql cakephp cakephp-3.0 cakephp-3.x

我有一个名为Bookings的表,其中包含以下属性:

  • id
  • artist_id - 外键
  • status
  • amount
  • created
  • modified

它与表格ArtistsPaymentsSessions相关联。

在View中,我使用了jQuery插件DataTables来显示满足以下条件的Bookings

  • 他们的status等于'已确认'
  • 关联表Session's属性date_end必须大于当前日期。

但是,第二个条件似乎不起作用,当满足该条件的数据条目存在时,它不返回任何内容。

在我的控制器中,该特定表的相应查找:

$bookingsConfirmed = $this->Bookings->find('all',[
            'contain' => ['Sessions', 'Sessions.Studios', 'Sessions.Engineers', 'Artists'],
            'conditions'=>['status' => 'confirmed', 'Sessions.date_end >=' => DATE(Time::now())],
            'order'=>['Bookings.created'=>'ASC']
        ]);

我尝试对病情进行以下改变,所有这些改变都没有显着差异:

  • date_end >=' => DATE(Time::now())
  • 'Sessions.date_end >=' => Time::now()
  • 'date(Sessions.date_end) >=' => DATE(Time::now())
  • 'date(Sessions.date_end) >=' => date(DATE(Time::now()))

如果我将> =符号切换为<,我的所有预订数据都会显示,尽管并非所有预订数据都符合此条件。

在SQL日志中,这是此特定表的输出:

SELECT 
  Bookings.id AS `Bookings__id`, 
  Bookings.artist_id AS `Bookings__artist_id`, 
  Bookings.status AS `Bookings__status`, 
  Bookings.amount AS `Bookings__amount`, 
  Bookings.created AS `Bookings__created`, 
  Bookings.modified AS `Bookings__modified`, 
  Sessions.id AS `Sessions__id`, 
  Sessions.booking_id AS `Sessions__booking_id`, 
  Sessions.studio_id AS `Sessions__studio_id`, 
  Sessions.engineer_id AS `Sessions__engineer_id`, 
  Sessions.guestengineer_id AS `Sessions__guestengineer_id`, 
  Sessions.date_start AS `Sessions__date_start`, 
  Sessions.date_end AS `Sessions__date_end`, 
  Sessions.starttime AS `Sessions__starttime`, 
  Sessions.hours AS `Sessions__hours`, 
  Sessions.session_genre AS `Sessions__session_genre`, 
  Sessions.no_people AS `Sessions__no_people`, 
  Sessions.studio_usage AS `Sessions__studio_usage`, 
  Sessions.otherpeople_req AS `Sessions__otherpeople_req`, 
  Sessions.special_req AS `Sessions__special_req`, 
  Sessions.created AS `Sessions__created`, 
  Sessions.modified AS `Sessions__modified`, 
  Studios.id AS `Studios__id`, 
  Studios.name AS `Studios__name`, 
  Studios.description AS `Studios__description`, 
  Studios.created AS `Studios__created`, 
  Studios.modified AS `Studios__modified`, 
  Engineers.id AS `Engineers__id`, 
  Engineers.user_id AS `Engineers__user_id`, 
  Engineers.eng_firstname AS `Engineers__eng_firstname`, 
  Engineers.eng_lastname AS `Engineers__eng_lastname`, 
  Engineers.eng_email AS `Engineers__eng_email`, 
  Engineers.eng_phoneno AS `Engineers__eng_phoneno`, 
  Engineers.eng_status AS `Engineers__eng_status`, 
  Engineers.rate AS `Engineers__rate`, 
  Engineers.created AS `Engineers__created`, 
  Engineers.modified AS `Engineers__modified`, 
  Artists.id AS `Artists__id`, 
  Artists.name AS `Artists__name`, 
  Artists.cp_id AS `Artists__cp_id`, 
  Artists.user_id AS `Artists__user_id`, 
  Artists.genre AS `Artists__genre`, 
  Artists.created AS `Artists__created`, 
  Artists.modified AS `Artists__modified` 
FROM 
  bookings Bookings 
  LEFT JOIN sessions Sessions ON Bookings.id = (Sessions.booking_id) 
  INNER JOIN studios Studios ON Studios.id = (Sessions.studio_id) 
  LEFT JOIN engineers Engineers ON Engineers.id = (Sessions.engineer_id) 
  INNER JOIN artists Artists ON Artists.id = (Bookings.artist_id) 
WHERE 
  (
    status = 'confirmed' 
    AND Sessions.date_end >= '20/2/17, 4:47 p02'
  ) 
ORDER BY 
  Bookings.created ASC

截至今天的日期,应该有3个条目显示。我做了Time::now的打印并得到了日期:

Cake\I18n\Time Object
(
    [time] => 2017-02-20T16:47:11+11:00
    [timezone] => 
    [fixedNowTime] => 
)

奇怪的是,上周工作正常,提交表格仍然有效。以MySQL为例,我今天输入的最新条目显示Sessions.date_end以YYYY-MM-DD格式填写,就像所有条目一样。

1 个答案:

答案 0 :(得分:0)

请尝试使用此行

`date_end >=' => date('Y-m-d H:i:s', Time::now()->getTimestamp())`

注意:对函数使用确切的单词大小写,date而不是DATE