本文整理汇总了PHP中app\models\Post::setAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP Post::setAttributes方法的具体用法?PHP Post::setAttributes怎么用?PHP Post::setAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Post
的用法示例。
在下文中一共展示了Post::setAttributes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionIndex
public function actionIndex()
{
if (!$this->confirm('Remove all data and insert demo data?')) {
return false;
}
Category::deleteAll();
$categories = [['id' => 1, 'alias' => 'general', 'title' => 'General'], ['id' => 2, 'alias' => 'general-about', 'title' => 'About this script', 'parentId' => 1], ['id' => 3, 'alias' => 'general-faq', 'title' => 'F.A.Q.', 'parentId' => 1], ['id' => 4, 'alias' => 'cat13', 'title' => 'Category 1.3', 'parentId' => 1], ['id' => 5, 'alias' => 'cat14', 'title' => 'Category 1.4', 'parentId' => 1], ['id' => 6, 'alias' => 'cat15', 'title' => 'Category 1.5', 'parentId' => 1], ['id' => 7, 'alias' => 'cat2', 'title' => 'Category 2'], ['id' => 8, 'alias' => 'cat21', 'title' => 'Category 2.1', 'parentId' => 7], ['id' => 9, 'alias' => 'cat22', 'title' => 'Category 2.2', 'parentId' => 7], ['id' => 10, 'alias' => 'cat23', 'title' => 'Category 2.3', 'parentId' => 7], ['id' => 11, 'alias' => 'cat3', 'title' => 'Category 3'], ['id' => 12, 'alias' => 'cat31', 'title' => 'Category 3.1', 'parentId' => 11], ['id' => 13, 'alias' => 'cat32', 'title' => 'Category 3.2', 'parentId' => 11], ['id' => 14, 'alias' => 'cat33', 'title' => 'Category 3.3', 'parentId' => 11]];
foreach ($categories as $item) {
$model = new Category();
$model->setAttributes($item, false);
$model->save();
}
Post::deleteAll();
$posts = [['title' => 'Post 1 - 1', 'alias' => 'post11', 'categoryId' => 1], ['title' => 'Post 1 - 2', 'alias' => 'post12', 'categoryId' => 1], ['title' => 'Post 1 - 3', 'alias' => 'post13', 'categoryId' => 1], ['title' => 'Post 1 - 4', 'alias' => 'post14', 'categoryId' => 1], ['title' => 'Post 1.1 - 1', 'alias' => 'post111', 'categoryId' => 2], ['title' => 'Post 1.1 - 2', 'alias' => 'post112', 'categoryId' => 2], ['title' => 'Post 1.1 - 3', 'alias' => 'post113', 'categoryId' => 2], ['title' => 'Post 1.1 - 4', 'alias' => 'post113', 'categoryId' => 2], ['title' => 'Post 1.2 - 1', 'alias' => 'post121', 'categoryId' => 3], ['title' => 'Post 1.2 - 2', 'alias' => 'post122', 'categoryId' => 3], ['title' => 'Post 1.2 - 3', 'alias' => 'post123', 'categoryId' => 3], ['title' => 'Post 1.3 - 1', 'alias' => 'post131', 'categoryId' => 4], ['title' => 'Post 1.3 - 2', 'alias' => 'post132', 'categoryId' => 4], ['title' => 'Post 1.3 - 3', 'alias' => 'post133', 'categoryId' => 4]];
foreach ($posts as $item) {
$model = new Post();
$model->setAttributes($item, false);
$model->save(false);
}
$this->printTree(Category::find()->all());
}
示例2: actionCreate
public function actionCreate()
{
$this->view->titlePage = "Редактирование поста";
/**
* @var Post $post
*/
$post = new Post();
if (isset($_POST['Post'])) {
$post->setAttributes($_POST['Post']);
$post->active = isset($_POST['Post']['active']) ? 1 : 0;
$post->body = isset($_POST['Post']['body']) ? trim($_POST['Post']['body']) : $post->body;
$result = $post->save();
if ($result) {
$this->redirect($this->createUrl('/blog/show', ['id' => $result]));
exit;
}
}
if (Request::isAjax()) {
echo $this->view->render('create', ['post' => $post], false, false);
exit;
}
$this->view->render('create', ['post' => $post]);
}