count() public method

Returns the COUNT(*) for the query. If the query has not been modified, and the count has already been performed the cached value is returned
public count ( )
 /**
  * Login and submit a POST request to a $url that is expected to delete a given record,
  * and then verify its removal.
  *
  * @param int $user_id The user to login as.
  * @param String $url The url to send the request to.  Be sure to include a trailing /.
  * @param int $delete_id The id of the record to delete.
  * @param String $redirect_url The url to redirect to, after the deletion.
  * @param \Cake\ORM\Table $table The table to delete from.
  */
 protected function deletePOST($user_id, $url, $delete_id, $redirect_url, $table)
 {
     $this->fakeLogin($user_id);
     $this->post($url . $delete_id);
     $this->assertResponseSuccess();
     // 2xx, 3xx
     $this->assertRedirect($redirect_url);
     // Now verify that the record no longer exists
     $query = new Query(ConnectionManager::get('test'), $table);
     $query->find('all')->where(['id' => $delete_id]);
     $this->assertEquals(0, $query->count());
 }
 /**
  * Attaches comments to each entity on find operation.
  *
  * @param \Cake\Event\Event $event The event that was triggered
  * @param \Cake\ORM\Query $query The query object
  * @param array $options Additional options as an array
  * @param bool $primary Whether is find is a primary query or not
  * @return void
  */
 public function beforeFind(Event $event, $query, $options, $primary)
 {
     if ($this->_enabled && $query->count() > 0) {
         $pk = $this->_table->primaryKey();
         $tableAlias = Inflector::underscore($this->_table->alias());
         $query->contain(['Comments' => function ($query) {
             return $query->find('threaded')->contain(['Users'])->order($this->config('order'));
         }]);
         if ($this->config('count') || isset($options['comments_count']) && $options['comments_count'] === true) {
             $query->formatResults(function ($results) use($pk, $tableAlias) {
                 return $results->map(function ($entity) use($pk, $tableAlias) {
                     $entityId = $entity->{$pk};
                     $count = TableRegistry::get('Comment.Comments')->find()->where(['entity_id' => $entityId, 'table_alias' => $tableAlias])->count();
                     $entity->set('comments_count', $count);
                     return $entity;
                 });
             });
         }
     }
 }
Example #3
0
 /**
  * Prepare CakePHP paging
  *
  * @param Request $request DataTable request
  */
 protected function preparePaging(Request $request)
 {
     self::$countBeforePaging = $this->query->count();
     $this->query->limit($request->getLength());
     $this->query->offset($request->getStart());
 }