public function relations() { $rels = array('parent' => array(self::BELONGS_TO, 'Comment', 'parent_id'), 'user' => array(self::BELONGS_TO, 'CommentUser', 'user_id'), 'userLike' => array(self::HAS_ONE, self::$commentLikeType, 'comment_id', 'on' => 'userLike.user_id=' . intval(Yii::app()->user->id))); if (self::$commentableType) { // dynamic relation with commentable AR $behavior = $this->getCommentableBehavior(); $rels['items'] = array(self::MANY_MANY, self::$commentableType, sprintf("%s(%s, %s)", $behavior->mapTable, $behavior->mapCommentColumn, $behavior->mapRelatedColumn), 'together' => false); Commentable::$commentableType = self::$commentableType; $rels['commentableItems'] = array(self::MANY_MANY, 'Commentable', sprintf("%s(%s, %s)", $behavior->mapTable, $behavior->mapCommentColumn, $behavior->mapRelatedColumn), 'together' => false); } return $rels; }
public function getSubscribedUsers($commentableType, &$cs) { $items = $this->getRelatedItems($cs); $itemIds = array_keys($items); Commentable::$commentableType = $this->commentableType; $itemsSubs = Commentable::model()->with('subscriptions')->findAllByPk($itemIds, array('index' => 'id')); $subs = array_reduce($itemsSubs, function ($res, $item) { return array_merge($res, $item->subscriptions); }, array()); $uids = array_map(function ($sub) { return $sub->user_id; }, $subs); if (empty($uids)) { return array(); } $us = CommentUser::model()->findAllByPk($uids, array('with' => 'notifyUser', 'index' => 'id')); $this->log('subscribed users: ' . print_r(array_keys($us), true)); return $us; }