/**
  * 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 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
  */
 public function relatedCount($bean, $type, $sql = NULL, $bindings = array())
 {
     if (!$bean instanceof OODBBean) {
         throw new RedException('Expected array or 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 (SQLException $exception) {
         $this->handleException($exception);
         return 0;
     }
 }