/** * Seeds the table. * * @return void */ public function run() { $author = new Author(); $author->first_name = 'Dan'; $author->last_name = 'Gebhardt'; $author->twitter = 'dgeb'; $author->save(); }
public function actionIndex() { $searchModel = new SearchBook(); $author = new Author(); $author->surname = ''; $authors_data = $author->find()->select(['id', "name"])->all(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'author' => $author, 'authors_data' => $authors_data]); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $result = Validator::make(Input::all(), ['title' => 'required', 'author' => 'required', 'publisher' => 'required', 'isbn' => "required|max:13|min:13|regex:/^[1-9][0-9]{12}\$/"], ['title.required' => 'Book title is required', 'author.required' => 'Book author is required', 'isbn.required' => 'ISBN is required', 'isbn.max' => 'ISBN must be exactly 13 characters', 'isbn.min' => 'ISBN must be exactly 13 characters', 'isbn.regex' => 'ISBN must contain numbers only.', 'publisher.required' => 'Publisher is required']); if ($result->fails()) { $result->errors()->add('submitted', 1); $result->errors()->add('desc', Input::get('description') !== "" ? TRUE : FALSE); return redirect()->route('books.new')->withErrors($result->errors())->withInput(); } $author_id = $request->get('author_id'); if (intval($author_id) == -1) { $new_author = new Author(); $new_author->author_name = $request->get('author'); $new_author->save(); $author_id = $new_author->id; // Create copy of newly created author $new_author = new Author(); $new_author->author_name = $request->get('author'); $new_author->record_id = $author_id; $new_author->save(); } $publisher_id = $request->get('publisher_id'); if (intval($publisher_id) == -1) { $new_publisher = new Publisher(); $new_publisher->name = $request->get('publisher'); $new_publisher->save(); $publisher_id = $new_publisher->id; // Create copy of newly created publisher $new_publisher = new Publisher(); $new_publisher->name = $request->get('publisher'); $new_publisher->record_id = $publisher_id; $new_publisher->save(); } $book = new Books(); $book->title = $request->get("title"); $book->author_id = $author_id; $book->publisher_id = $publisher_id; $book->isbn = $request->get("isbn"); $book->description = $request->get('description'); $book->date_published = $request->get('date_published'); $book->save(); $book_id = $book->id; // Create copy of newly created book $book = new Books(); $book->title = $request->get("title"); $book->author_id = $author_id; $book->publisher_id = $publisher_id; $book->isbn = $request->get("isbn"); $book->description = $request->get('description'); $book->date_published = $request->get('date_published'); $book->record_id = $book_id; $book->save(); return redirect()->route('books.home')->with('status', "New book created"); }
public function actionLogin() { $user_name = \Yii::$app->request->post()['user_name']; $password = \Yii::$app->request->post()['pwd']; $author_info = new Author(); $author_info->name = $user_name; $author_info->pwd = $password; if ($author_info->save()) { $author_id = Author::findBySql("select id from author ORDER BY id DESC LIMIT 0,1")->asArray()->all(); $array = ['error_no' => 0, 'error_msg' => '', 'data' => $author_id[0]]; echo json_encode($array); } }
/** * @inheritdoc */ public function getRelationships($post, array $includeRelationships = []) { /** @var Post $post */ // that's an example how $includeRelationships could be used for reducing requests to database if (isset($includeRelationships['author']) === true) { // as author will be included as full resource we have to give full resource $author = $post->author; } else { // as author will be included as just id and type so it's not necessary to load it from database $author = new Author(); $author->setAttribute($author->getKeyName(), $post->author_id); } return ['author' => [self::DATA => $author], 'comments' => [self::DATA => $post->comments->all()]]; }
/** * Показывает контент * @return string * @TODO Нужно предусмотреть возможность вывода разного контента, привязаного к одной категории * */ public function actionShow() { $this->cat_id = Yii::$app->getRequest()->getQueryParam('id') ? Yii::$app->getRequest()->getQueryParam('id') : null; $cat_obg = Categories::find()->where('id = ' . $this->cat_id)->one(); $allContent = Articles::find()->where('cat_id = ' . $this->cat_id)->all(); $allArticlesForPager = Articles::find()->where(['cat_id' => $this->cat_id]); $countQueryCont = clone $allArticlesForPager; $pagesGlobal = new Pagination(['totalCount' => $countQueryCont->count(), 'pageSize' => 1, 'forcePageParam' => false, 'pageSizeParam' => false]); $artPages = $allArticlesForPager->offset($pagesGlobal->offset)->limit($pagesGlobal->limit)->all(); foreach ($allContent as $article) { //var_dump($this->cat_id); $this->article_id = $article->id; $article = Articles::findOne($this->article_id); $allArticles = ArticlesContent::find()->where(['articles_id' => $this->article_id])->orderBy(['id' => SORT_DESC]); //var_dump($allArticles); exit; $countQuery = clone $allArticles; $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => isset($article->onepages) ? $article->onepages : 0, 'forcePageParam' => false, 'pageSizeParam' => false]); $models = $allArticles->offset($pages->offset)->limit($pages->limit)->all(); $this->source = Source::find()->where(['id' => $models[0]->source_id])->one()->title; $this->author = Author::find()->where(['id' => Source::find()->where(['id' => $models[0]->source_id])->one()->author_id])->one()->name; $this->articles[] = ['article' => $article, 'contents' => $models, 'pages' => $pages, 'source' => $this->source, 'author' => $this->author]; } //var_dump($this->articles); exit; return $this->renderPartial($cat_obg->action, ['articles' => $this->articles, 'cat' => $this->cat_id, 'pagesGlobal' => $pagesGlobal, 'artPages' => $artPages, 'cat_obg' => $cat_obg]); }
/** * Создание автораа * @return string */ public function actionCreate() { $model = new Author(); if ($model->load(Yii::$app->request->post())) { $model->name = Yii::$app->request->post('Author')['name']; $model->status = Yii::$app->request->post('Author')['status']; $model->description = Yii::$app->request->post('Author')['description']; $model->country_id = Yii::$app->request->post('Author')['country_id']; $model->save(false); $authors = Author::find(); $dataProvider = new ActiveDataProvider(['query' => $authors]); return $this->redirect(Url::toRoute('author/index')); } else { return $this->render('_form', ['model' => $model]); } }
/** * @param $name * @return Author */ public function __get($name) { if ('author' === $name && null !== $this->author_id) { return Author::findByID($this->author_id); } return false; }
/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker\Factory::create(); foreach (range(1, 100) as $index) { DB::table('books')->insert(['title' => $faker->word(), 'preview' => $faker->sentence(), 'author_id' => \App\Models\Author::orderByRaw("RAND()")->first()->id, 'created_at' => Carbon\Carbon::now(), 'updated_at' => Carbon\Carbon::now()]); } }
public function search($params) { $query = Author::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
public function getAuthor() { if (!empty($this->data['author_id'])) { return Author::findOneById($this->data['author_id']); } else { return false; } }
/** * Test samples for all models have been seeded. */ public function testModelSeed() { $message = 'Haven\'t you forgotten to run artisan migrate and db::seed?'; $this->assertNotEmpty(Author::all(), $message); $this->assertNotEmpty(Comment::all(), $message); $this->assertNotEmpty(Post::all(), $message); $this->assertNotEmpty(Site::all(), $message); }
/** * @param string $q * @return array */ public function actionIndex($q = null) { Yii::$app->getResponse()->format = Response::FORMAT_JSON; $model = Author::find()->limit(10); if (!is_null($q)) { $model->where(['or', ['like', 'firstname', $q], ['like', 'lastname', $q]]); } return ['results' => $model->all()]; }
/** * Удаление автора. * * @param int $id * @return \yii\web\Response */ public function actionDelete($id) { /** @var Author $modelAuthor */ $modelAuthor = Author::findOne($id); if ($modelAuthor->delete()) { return $this->redirect(['back-author/index']); } return $this->refresh(); }
/** * Updates an existing Book model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $authors = \app\models\Author::find()->all(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('update', ['model' => $model, 'authors' => $authors]); } }
public function actionIndex() { $this->view->news = Article::findAllWithGenerator(); $this->view->table = (new AdminDataTable(Author::findAll(), [function ($model) { return $model->getName(); }, function ($model) { return $model->getEmail(); }]))->render(); $this->view->display(__DIR__ . '/../../templates/admin.php'); }
/** * Test samples for all models have been seeded. */ public function testModelSeed() { $message = 'Haven\'t you forgotten to run artisan migrate and db::seed?'; $this->assertNotEmpty(Author::all(), $message); $this->assertNotEmpty(Comment::all(), $message); $this->assertNotEmpty(Post::all(), $message); $this->assertNotEmpty(Site::all(), $message); $isAuthenticated = Auth::attempt(['email' => UsersTableSeeder::SAMPLE_LOGIN, 'password' => UsersTableSeeder::SAMPLE_PASSWORD]); $this->assertTrue($isAuthenticated); }
/** * Это магический метод __get, доработанный под данную модель. При запросе * свойства author проверяет, не пустое ли у объекта свойство $author_id, и * если не пустое, то возвращает объект класса Author * * @param $k string Имя свойства * @return null Если имя свойства не равно 'author' * @return object Authors, если имя свойства равно author */ public function __get($k) { switch ($k) { case 'author': return Author::findById($this->author_id); break; default: return null; } }
/** * Updates an existing Book model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { $file = UploadedFile::getInstance($model, 'preview'); if ($model->upload($file) && $model->save()) { return $this->redirect(Url::previous()); } } return $this->render('update', ['model' => $model, 'authors' => Author::find()->all()]); }
public function actionUpdate($id) { $book = Book::findOne(['id' => $id]); $filtredModel = Yii::$app->request->get('filtredModel'); if (Yii::$app->request->isPost) { $book->load(Yii::$app->request->post()); $book->save(); $this->redirect(Url::to(array_merge(['books/index'], $filtredModel))); } return $this->render('update', ['book' => $book, 'authors' => Author::find()->all(), 'Book' => $filtredModel]); }
/** * Геттер * Обработка обращения к author * @return object| FALSE | NULL Возвращает объект Author, если есть author_id * FALSE Если author_id не установлен * NULL Если запрошено свойство которого, нет */ public function __get($k) { if ($k == 'author') { if (isset($this->author_id)) { return \App\Models\Author::findById($this->author_id); } else { return false; } } return NULL; }
public function editPreparation($product) { $item = Item::find($product->organizations->first()->pivot->id); $itemtags = $item->tags->lists(['id'])->toArray(); $book = $product->is; $bookauthors = $book->authors->lists(['id'])->toArray(); $categories = $this->getCategoriesByRecursion($item->cat_id); $authors = Author::get()->lists('full_name', 'id'); $editors = ['' => ''] + Editor::lists('e_name', 'id')->all(); $tags = Tag::lists('tag_name', 'id'); return compact('product', 'itemtags', 'authors', 'editors', 'categories', 'tags', 'book', 'item', 'bookauthors', 'categories'); }
/** * Возвращает массив авторов вида: [id=>{firsname lastname},...] * * @return array */ public static function getListAuthor() { $authors = Author::find()->select(['id', 'firstname', 'lastname'])->all(); $result = []; /** * @var Author $author */ foreach ($authors as $author) { $result[$author->id] = $author->getFullName(); } return $result; }
/** * Run the database seeds. * * @return void */ public function run() { $tolkien = Author::create(['name' => 'J.R.R. Tolkien', 'date_of_birth' => '1892-01-03']); Book::create(['title' => 'The Hobbit', 'publication_date' => '1937-09-21', 'isbn' => '9780582186552', 'author_id' => $tolkien->id]); Book::create(['title' => 'The Silmarillion', 'publication_date' => '1977-09-15', 'isbn' => '9780345325815', 'author_id' => $tolkien->id]); $karpyshyn = Author::create(['name' => 'Drew Karpyshyn', 'date_of_birth' => '1971-07-28']); Book::create(['title' => 'The Old Republic: Revan', 'publication_date' => '2011-11-15', 'isbn' => '9780857689009', 'author_id' => $karpyshyn->id]); Book::create(['title' => 'The Old Republic: Annihilation', 'publication_date' => '2012-11-13', 'isbn' => '9780345529411', 'author_id' => $karpyshyn->id]); $salvatore = Author::create(['name' => 'R.A. Salvatore', 'date_of_birth' => '1959-01-20']); Book::create(['title' => 'Homeland', 'publication_date' => '2004-03-01', 'isbn' => '9780786931231', 'author_id' => $salvatore->id]); Book::create(['title' => 'Attack of the Clones', 'publication_date' => '2003-04-01', 'isbn' => '9780345428820', 'author_id' => $salvatore->id]); }
/** * Updates an existing Book model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $authors = Author::find()->asArray()->all(); for ($i = 1; $i <= Author::find()->count(); $i++) { $authors_array[$i] = $authors[$i - 1]['firstname'] . ' ' . $authors[$i - 1]['lastname']; } if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'authors_array' => $authors_array]); } }
public function run() { $rawData = array_map('str_getcsv', file(__DIR__ . '/../data/authors.csv')); $headers = array_shift($rawData); $items = array_map(function ($row) use($headers) { return array_combine($headers, $row); }, $rawData); if (!array_walk($items, function ($item) { Author::updateOrCreate(['id' => $item['id']], $item); })) { throw new Exception('There was an issue importing authors.'); } }
public function run() { if (class_exists('Faker\\Factory')) { $faker = Faker\Factory::create(); $specimens = Specimen::all()->all(); $museums = Museum::all()->all(); $authors = Author::all()->all(); $animalGroups = AnimalGroup::all()->all(); for ($i = 1; $i <= 500; $i++) { Scan::create(['scanId' => $faker->randomNumber(5), 'scanQuality' => $faker->randomElement(['low', 'medium', 'high']), 'fileDirectory' => '//scan' . $faker->randomNumber(2), 'location' => $faker->country, 'scanTime' => $faker->dateTime, 'voltage' => $faker->randomFloat(2, 1, 20), 'voxelSize' => $faker->randomFloat(1, 0, 1) . 'mm', 'imageCount' => $faker->randomNumber(4), 'current' => '120v', 'sequence' => $faker->randomNumber(4), 'specimenId' => $faker->randomElement($specimens)->id, 'museumId' => $faker->randomElement($museums)->id, 'authorId' => $faker->randomElement($authors)->id, 'animalGroupId' => $faker->randomElement($animalGroups)->id]); } } }
/** * Seeds the table. * * @return void */ public function run() { $post = new Post(); $post->title = 'JSON API paints my bikeshed!'; $post->body = 'If you\'ve ever argued with your team about the way your JSON responses should be ' . 'formatted, JSON API is your anti-bikeshedding weapon.'; /** @var Site $site */ $site = Site::firstOrFail(); /** @var Author $author */ $author = Author::firstOrFail(); $post->site_id = $site->id; $post->author_id = $author->id; $post->save(); }
/** * Updates an existing Book model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if (Yii::$app->request->isPost) { $model->load(Yii::$app->request->post()); $model->imageFile = UploadedFile::getInstance($model, 'preview'); $model->uploadFile(); $model->save(false); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'authors' => (new AuthorCollection())->createEntity(Author::find()->all(), true)]); } }
/** * Показываем контент * @param $id * @return string */ public function actionShow($id) { $article = Articles::findOne($id); $comment = new Comments(); $this->title = $article->title; $allArticles = ArticlesContent::find()->where(['articles_id' => $id]); //var_dump($allArticles); exit; $countQuery = clone $allArticles; $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => isset($article->onepages) ? $article->onepages : 0, 'forcePageParam' => false, 'pageSizeParam' => false]); $models = $allArticles->offset($pages->offset)->limit($pages->limit)->all(); $this->source = Source::find()->where(['id' => $models[0]->source_id])->one()->title; $this->author = Author::find()->where(['id' => Source::find()->where(['id' => $models[0]->source_id])->one()->author_id])->one()->name; return $this->render('view', ['articles' => $allArticles, 'contents' => $models, 'pages' => $pages, 'title' => $this->title, 'source' => $this->source, 'author' => $this->author, 'comment' => $comment]); }