本文整理匯總了PHP中app\models\Post::link方法的典型用法代碼示例。如果您正苦於以下問題:PHP Post::link方法的具體用法?PHP Post::link怎麽用?PHP Post::link使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類app\models\Post
的用法示例。
在下文中一共展示了Post::link方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: actionCreate
/**
* Creates a new Post model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Post();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
/** @var Category[] $categories */
$categories = Category::find()->where(['id' => $model->categories_id])->all();
foreach ($categories as $category) {
$model->link('categories', $category);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', ['model' => $model, 'category_names' => ArrayHelper::map(Category::find()->all(), 'id', 'name')]);
}
}
示例2: actionNew
public function actionNew()
{
$postModel = new Post();
$category = new Category();
$categoryList = ArrayHelper::map($category->find()->asArray()->all(), 'id', 'title');
$postList = $postModel->find()->asArray()->all();
$message = '';
if ($postModel->load(Yii::$app->request->post()) && $postModel->save()) {
$selectedCategories = Category::find()->where(['id' => $postModel->categoryIds])->all();
foreach ($selectedCategories as $category) {
$postModel->link('categories', $category);
}
$message = 'Category added success';
}
return $this->render('new', ['model' => $postModel, 'message' => $message, 'categoryList' => $categoryList, 'postList' => $postList]);
}
示例3: actionEdit
public function actionEdit($id)
{
$model = Moderation::findOne((int) $id);
if (!$model) {
throw new Exception("Запись не найдена!");
}
if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post(), $model->formName()) && $model->validate() && !Yii::$app->request->post('nosave', false)) {
$post = new Post();
$post->attributes = $model->attributes;
$post->visible = true;
$tags = Yii::$app->request->post($model->formName(), ['tags' => []])['tags'];
if ($post->save()) {
if (!empty($tags)) {
foreach ($tags as $tag) {
$modelTag = Tag::add($tag);
if ($modelTag) {
$post->link('tags', $modelTag);
}
}
}
$model->delete();
// публикация в твиттере
ContentGenerator::Twitter($post);
return $this->redirect('moderate');
}
}
return $this->render('_edit', ['model' => $model]);
}