/** * 新增或编辑后执行 * @param string $context * @param array $row * @param mixed $params * @return void */ public function onAfterSave($context, array &$row, $params = null) { $isCreate = $context === 'posts\\services\\Posts::create' ? true : false; $isModify = $context === 'posts\\services\\Posts::modifyByPk' ? true : false; if (!$isCreate && !$isModify) { return; } if (($postId = (int) $params) <= 0) { return; } $service = Service::getInstance('Posts', 'posts'); $fields = $service->getModuleFieldsByPostId($postId); if ($fields === array()) { return; } $attributes = array(); foreach ($fields as $name => $field) { if (isset($row[$name])) { $attributes[$name] = $row[$name]; } else { if ($isCreate) { $attributes[$name] = ''; } } } if ($attributes === array()) { return; } $db = $service->getDb(); $dbProxy = $db->getDbProxy(); $tableName = $db->getTblprefix() . TableNames::getPostProfile(); $profile = Profile::getInstance($tableName, $postId, $dbProxy); $profile->save($attributes); }
/** * 新增或编辑后执行 * @param string $context * @param array $row * @param mixed $params * @return void */ public function onAfterSave($context, array &$row, $params = null) { $isCreate = $context === 'users\\services\\Users::create' ? true : false; $isModify = $context === 'users\\services\\Users::modifyByPk' ? true : false; if (!$isCreate && !$isModify) { return; } if (($userId = (int) $params) <= 0) { return; } $sexEnum = DataUsers::getSexEnum(); $sex = isset($row['sex']) ? trim($row['sex']) : ''; $birthday = isset($row['birthday']) ? trim($row['birthday']) : ''; $address = isset($row['address']) ? trim($row['address']) : ''; $qq = isset($row['qq']) ? (int) $row['qq'] : 0; $headPortrait = isset($row['head_portrait']) ? trim($row['head_portrait']) : ''; $remarks = isset($row['remarks']) ? trim($row['remarks']) : ''; $sex = isset($sexEnum[$sex]) ? $sex : DataUsers::SEX_UNKNOW; $birthday = date('Y-m-d', strtotime($birthday)) === $birthday ? $birthday : ''; $qq = $qq > 0 ? $qq : ''; $attributes = array(); if ($isCreate) { $attributes = array('sex' => $sex, 'birthday' => $birthday, 'address' => $address, 'qq' => $qq, 'head_portrait' => $headPortrait, 'remarks' => $remarks); } else { if (isset($row['sex'])) { $attributes['sex'] = $sex; } if (isset($row['birthday'])) { $attributes['birthday'] = $birthday; } if (isset($row['address'])) { $attributes['address'] = $address; } if (isset($row['qq'])) { $attributes['qq'] = $qq; } if (isset($row['head_portrait'])) { $attributes['head_portrait'] = $headPortrait; } if (isset($row['remarks'])) { $attributes['remarks'] = $remarks; } if ($attributes === array()) { return; } } $db = Service::getInstance('Users', 'users')->getDb(); $dbProxy = $db->getDbProxy(); $tableName = $db->getTblprefix() . TableNames::getUserProfile(); $profile = Profile::getInstance($tableName, $userId, $dbProxy); $profile->save($attributes); }