/** * @param Binder $binder * @return boolean */ public static function bindCategories($binder) { $postId = $binder->binderId; $categories = $binder->bindedData; // Clear all existing mappings ModelCategory::deleteByParentIdType($postId, CmsGlobal::TYPE_POST); if (isset($categories) && count($categories) > 0) { foreach ($categories as $key => $value) { if (isset($value) && $value > 0) { $toSave = new ModelCategory(); $toSave->parentId = $postId; $toSave->parentType = CmsGlobal::TYPE_POST; $toSave->categoryId = $value; $toSave->save(); } } } return true; }
/** * @return array - ModelCategory associated with parent */ public function getModelCategories() { $parentType = $this->categoryType; return $this->hasMany(ModelCategory::className(), ['parentId' => 'id'])->where("parentType='{$parentType}'"); }