/**
  * Counts the number of related beans in an N-M relation.
  * This method returns the number of beans of type $type associated
  * with reference bean(s) $bean. The query can be tuned using an
  * SQL snippet for additional filtering.
  *
  * @param RedBean_OODBBean|array $bean     a bean object or an array of beans
  * @param string                 $type     type of bean you're interested in
  * @param string                 $sql      SQL snippet (optional)
  * @param array                  $bindings bindings for your SQL string
  *
  * @return integer
  *
  * @throws RedBean_Exception_Security
  */
 public function relatedCount($bean, $type, $sql = NULL, $bindings = array())
 {
     if (!$bean instanceof RedBean_OODBBean) {
         throw new RedBean_Exception_Security('Expected array or RedBean_OODBBean but got:' . gettype($bean));
     }
     if (!$bean->id) {
         return 0;
     }
     $beanType = $bean->getMeta('type');
     try {
         return $this->writer->queryRecordCountRelated($beanType, $type, $bean->id, $sql, $bindings);
     } catch (RedBean_Exception_SQL $exception) {
         $this->handleException($exception);
         return 0;
     }
 }