From 9015a34946dfb13e8c626125cb6fcf77e328651c Mon Sep 17 00:00:00 2001 From: SASSU Thomas <thomas.sassu5@etu.univ-lorraine.fr> Date: Tue, 17 Mar 2020 13:56:26 +0100 Subject: [PATCH] Partie 1 models test --- .../src/controleurs/ControleurScript.php | 6 +++-- GamePedia/src/modeles/Comment.php | 24 ++++++++++++++++++ GamePedia/src/modeles/Game.php | 3 +++ GamePedia/src/modeles/User.php | 25 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 GamePedia/src/modeles/Comment.php create mode 100644 GamePedia/src/modeles/User.php diff --git a/GamePedia/src/controleurs/ControleurScript.php b/GamePedia/src/controleurs/ControleurScript.php index 71e6d15..748e662 100644 --- a/GamePedia/src/controleurs/ControleurScript.php +++ b/GamePedia/src/controleurs/ControleurScript.php @@ -2,6 +2,8 @@ namespace gp\controleurs; +use gp\modeles\Comment; +use gp\modeles\User; use Illuminate\Database\Capsule\Manager as DB; use gp\modeles\Game_rating; use gp\modeles\Genre; @@ -376,11 +378,11 @@ class ControleurScript //random game $game = Game::all()->random(1)->first(); //fake data - $title = $user->email; + $title = $faker->text(50); $content = $faker->text(200); $date = $faker->date('Y-m-d', '2015-02-01'); //insert comment - Comment::create($title, $content, $date); + Comment::create($game->id, $user->email, $title, $content, $date); } } } diff --git a/GamePedia/src/modeles/Comment.php b/GamePedia/src/modeles/Comment.php new file mode 100644 index 0000000..8e50f92 --- /dev/null +++ b/GamePedia/src/modeles/Comment.php @@ -0,0 +1,24 @@ +<?php +namespace gp\modeles; +use Illuminate\Database\Eloquent\Builder; +class Comment extends \Illuminate\Database\Eloquent\Model +{ + protected $table = 'comment'; + protected $primaryKey = 'id'; + public $timestamps = false ; + + public static function create($id_game, $email, $title, $content, $createdAt){ + $obj = new Comment(); + $obj->email = $email; + $obj->title = $title; + $obj->content = $content; + $obj->createdAt = $createdAt; + $obj->save(); + Game::where('id', '=', $id_game)->first()->comments()->attach([$id_game]); + return $obj; + } + + function games(){ + return $this->BelongsToMany('gp\modeles\Game', 'game2comment','comment_id'); + } +} \ No newline at end of file diff --git a/GamePedia/src/modeles/Game.php b/GamePedia/src/modeles/Game.php index 11f8781..ddc463d 100644 --- a/GamePedia/src/modeles/Game.php +++ b/GamePedia/src/modeles/Game.php @@ -31,4 +31,7 @@ class Game extends \Illuminate\Database\Eloquent\Model public function similarGames(){ return $this->belongsToMany('gp\modeles\Games', 'similar_games','game1'); } + public function comments(){ + return $this->belongsToMany('gp\modeles\comment', 'game2comment','game_id'); + } } \ No newline at end of file diff --git a/GamePedia/src/modeles/User.php b/GamePedia/src/modeles/User.php new file mode 100644 index 0000000..7410dee --- /dev/null +++ b/GamePedia/src/modeles/User.php @@ -0,0 +1,25 @@ +<?php +namespace gp\modeles; +use Illuminate\Database\Eloquent\Builder; +class User extends \Illuminate\Database\Eloquent\Model +{ + protected $table = 'user'; + protected $primaryKey = 'id'; + public $timestamps = false ; + + public static function create($fname, $name, $email, $adress, $numtel, $date){ + $obj = new User(); + $obj->name = $name; + $obj->fname = $fname; + $obj->email = $email; + $obj->adress = $adress; + $obj->numtel = $numtel; + $obj->date = $date; + $obj->save(); + return $obj; + } + + function games(){ + return $this->BelongsToMany('gp\modeles\Game', 'game2genre','genre_id'); + } +} \ No newline at end of file -- GitLab