Помогите с запросом в Zend

Sulik

Новичок
Переношу сайт на zf
остановился на вложенных комментариях.

Значит функция должна возвращать массив данных такого содержания:
PHP:
stdClass Object
(
    [id] => 82
    [user] => test
    [comment] => Комментарий
    [type] => www_news
    [date] => 2011-08-17 02:12:51
    [tid] => 100
    [pid] => 82
    [in] => 1
    [comments] =>
        [0] => stdClass Object
        (
          [id] => 83
          [user] => test
          [comment] => Вложенный 1
          [type] => www_news
          [date] => 2011-08-17 02:12:51
          [tid] => 100
          [pid] => 82
          [in] => 0
          [comments] => 
       )
        [0] => stdClass Object
        (
          [id] => 87
          [user] => test
          [comment] => Вложенный 2
          [type] => www_news
          [date] => 2011-08-17 02:12:51
          [tid] => 100
          [pid] => 82
          [in] => 0
          [comments] => 
       )
)
т.е. комментарии с pid 0 в них вложенные комментарии с его id в pid
считаем есть ли комментарии перед запуском цикла - in

c таким запросом все работало
PHP:
SELECT `site_comments`.*, COUNT(DISTINCT `in`.`id`) AS `in`
		FROM `site_comments`
		LEFT JOIN `site_comments` AS `in` ON `in`.`pid` = `site_comments`.`id` AND `in`.`tid` = `site_comments`.`tid` AND `in`.`type` = `site_comments`.`type`
		WHERE `site_comments`.`pid` = '$pid' AND `site_comments`.`type` = '$type' AND `site_comments`.`tid` = '$tid'
		GROUP BY `site_comments`.`id`
		ORDER BY `site_comments`.`date`
получаю записи в массиве только с pid = 0
причем zf выдает такой же запрос:
PHP:
SELECT `site_comments`.*, DATE_FORMAT(site_comments.date, ) AS `date`, COUNT(DISTINCT in.id) AS `in`, `in`.* FROM `site_comments`
 LEFT JOIN `site_comments` AS `in` ON in.pid = site_comments.id AND in.tid = site_comments.tid AND in.type = site_comments.type WHERE (site_comments.pid = 83) AND (site_comments.tid = 100) AND (site_comments.type = ) GROUP BY `site_comments`.`id` ORDER BY `site_comments`.`date` ASC

помогите разобраться вот функция
PHP:
    public function comments($type, $id)
    {
        $comments = $this->getComments($id, $type);
        return $comments;
    }
    
    public function getComments($tid, $type, $pid = 0)
    {
       $select = $this->getAdapter()->select()
            ->from($this->_name, array(
                         $this->_name.'.*',
                         'DATE_FORMAT('.$this->_name.'.date, \'%H:%i %d:%m:%Y\') as date',
                         'COUNT(DISTINCT in.id) AS in'))
            ->joinLeft(array(
                             'in' => $this->_name),
                             'in.pid = '.$this->_name.'.id AND in.tid = '.$this->_name.'.tid AND in.type = '.$this->_name.'.type')
            ->where($this->_name.'.pid = ?', $pid)
            ->where($this->_name.'.tid = ?', $tid)
            ->where($this->_name.'.type = ?', $type)
            ->group($this->_name.'.id')
            ->order($this->_name.'.date');
        
        $row = $this->getAdapter()->query($select);
        while ($comments = $row->fetch(Zend_Db::FETCH_OBJ))
	{
		if($comments->in > 0)
		{
			$comments->comments = $this->getComments($tid, $type, $comments->id);
		}
		$c[] = $comments;
	}
        return $c;
    }
 
Сверху