在cakephp3中添加要查询的字段

时间:2016-10-14 09:48:51

标签: php cakephp

在cakephp3中,我似乎无法在查询结果中添加一个字段。在cakephp2中,这个想法很简单但是因为我处理的是对象,所以这是不同的。我检查过以前的SE帖子,但无法得到答案。它必须简单,但我无法找到它。  在这里,我尝试将自定义字段“距离”添加到结果集

$lessons=$this->Lessons->getLessonsDay($searchDate,$searchFirstName,$searchLastName,$searchSubject, $tutorIds);
         foreach( $lessons as $key => $item){
        //  debug($item); 
             $stlat=$item->student->address_lat;
             $stlong=$item->student->address_long;
             $tutlat=$item->tutor->address_lat;
             $tutlong=$item->tutor->address_long; 

              $dist=$this->calculate_distance( $stlat,$stlong,$tutlat,$tutlong);
              debug($dist);
             $lessons->distance= $dist; //no effect
             $lessons[$key]['distance']=$dist; //error
             $lessons[$key]['Lessons']['distance']=$dist; //Error: Cannot use object of type Cake\ORM\Query as array  

            }

How to append to a CakePHP3 ResultSet?

http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#checking-if-a-query-or-resultset-is-empty

1 个答案:

答案 0 :(得分:-1)

也许你想做

$item->distance = $dist – arilia