title: How to find count in many to many relationship in MySql, Cakephp 3.6
body: <p>I need to calculate the <strong>count(vendors.id)</strong> for each <strong>category.</strong>
  ie: how many vendors are there for each <strong>category</strong>(<strong>not sub-category</strong>)</p>
  
  <p>I am using cakephp 3.6 framework and MySQL as database</p>
  
  <p>I tried with all possible way that i knew but not found any solution. Can anybody help me, is very important for my project</p>
  
  <p>[UPDATE]
  sql query i have used:</p>
  
  <pre><code>SELECT cat.id,cat.name ,COUNT(`vendor_id`) AS vendor_count FROM `vendor_services` JOIN `categories` ON(`vendor_services`.`category_id` = `categories`.`id`) JOIN `categories` AS cat ON(categories.category_id = cat.id) WHERE 1 GROUP BY cat.id
  </code></pre>
  
  <p>[UPDATE]Bellow is the sql to create corresponding tables</p>
  
  <pre><code>-- phpMyAdmin SQL Dump
  -- version 4.7.0
  -- https://www.phpmyadmin.net/
  --
  -- Host: 127.0.0.1
  -- Generation Time: Dec 04, 2018 at 10:50 AM
  -- Server version: 10.1.24-MariaDB
  -- PHP Version: 7.1.6
  
  SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  SET AUTOCOMMIT = 0;
  START TRANSACTION;
  SET time_zone = "+00:00";
  
  --
  -- Database: `demo123`
  --
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `categories`
  --
  
  CREATE TABLE `categories` (
    `id` int(11) NOT NULL,
    `category_id` tinyint(4) NOT NULL,
    `name` varchar(60) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  --
  -- Dumping data for table `categories`
  --
  
  INSERT INTO `categories` (`id`, `category_id`, `name`) VALUES
  (1, 0, 'Books'),
  (2, 0, 'Electronics'),
  (3, 0, 'Garden'),
  (4, 1, 'Novel'),
  (5, 1, 'Science'),
  (6, 1, 'Story'),
  (7, 2, 'Mobile'),
  (8, 2, 'Tablet'),
  (9, 2, 'Headphone'),
  (10, 3, 'Pumps'),
  (11, 3, 'Pipes');
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `vendors`
  --
  
  CREATE TABLE `vendors` (
    `id` int(11) NOT NULL,
    `name` varchar(60) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  --
  -- Dumping data for table `vendors`
  --
  
  INSERT INTO `vendors` (`id`, `name`) VALUES
  (1, 'VR Enterprizes'),
  (2, 'RR Vendors'),
  (3, 'JK Suppliers');
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `vendor_services`
  --
  
  CREATE TABLE `vendor_services` (
    `id` int(11) NOT NULL,
    `vendor_id` int(11) NOT NULL,
    `category_id` int(11) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  --
  -- Dumping data for table `vendor_services`
  --
  
  INSERT INTO `vendor_services` (`id`, `vendor_id`, `category_id`) VALUES
  (1, 1, 4),
  (2, 1, 5),
  (3, 1, 6),
  (4, 1, 11),
  (5, 2, 7),
  (6, 2, 8),
  (7, 2, 9),
  (8, 3, 10),
  (9, 3, 11);
  
  --
  -- Indexes for dumped tables
  --
  
  --
  -- Indexes for table `categories`
  --
  ALTER TABLE `categories`
    ADD PRIMARY KEY (`id`),
    ADD KEY `category_id` (`category_id`);
  
  --
  -- Indexes for table `vendors`
  --
  ALTER TABLE `vendors`
    ADD PRIMARY KEY (`id`);
  
  --
  -- Indexes for table `vendor_services`
  --
  ALTER TABLE `vendor_services`
    ADD PRIMARY KEY (`id`),
    ADD KEY `vendor_id` (`vendor_id`),
    ADD KEY `category_id` (`category_id`);
  
  --
  -- AUTO_INCREMENT for dumped tables
  --
  
  --
  -- AUTO_INCREMENT for table `categories`
  --
  ALTER TABLE `categories`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
  --
  -- AUTO_INCREMENT for table `vendors`
  --
  ALTER TABLE `vendors`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
  --
  -- AUTO_INCREMENT for table `vendor_services`
  --
  ALTER TABLE `vendor_services`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;COMMIT;
  </code></pre>
  
  <p>CategoriesTable</p>
  
  <pre><code>$this-&gt;hasMany(‘Subcategories’, [ 'className'=&gt; ‘Categories’,
              'foreignKey' =&gt; 'category_id',
              'conditions' =&gt; ['category_id!= 0']
          ]);
  
  $this-&gt;belongsTo('MainCategories', [
                  'className'=&gt; ‘Categories’,
                      'foreignKey' =&gt; 'category_id',
          ]);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/hFlFO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hFlFO.png" alt="enter image description here"></a></p>