From a938ad3dcb21e1f1325b5b89645126a950d225c4 Mon Sep 17 00:00:00 2001 From: SASSU Thomas <thomas.sassu5@etu.univ-lorraine.fr> Date: Wed, 4 Mar 2020 11:54:46 +0100 Subject: [PATCH] bug association not fixed --- GamePedia/php/script_TD1.php | 3 +- .../src/controleurs/ControleurScript.php | 35 ++++++++++++++- GamePedia/src/modeles/Company.php | 2 +- GamePedia/src/modeles/Game.php | 10 ++--- GamePedia/src/modeles/Game_rating.php | 45 +++++++++++++++++-- 5 files changed, 82 insertions(+), 13 deletions(-) diff --git a/GamePedia/php/script_TD1.php b/GamePedia/php/script_TD1.php index ce837f8..1615d71 100644 --- a/GamePedia/php/script_TD1.php +++ b/GamePedia/php/script_TD1.php @@ -1,7 +1,6 @@ <?php -require_once __DIR__ . '../vendor/autoload.php'; -session_start(); +require_once dirname (__DIR__) . '../vendor/autoload.php'; use \Slim\Slim as Slim; use Illuminate\Database\Capsule\Manager as DB; diff --git a/GamePedia/src/controleurs/ControleurScript.php b/GamePedia/src/controleurs/ControleurScript.php index 2a10849..b156863 100644 --- a/GamePedia/src/controleurs/ControleurScript.php +++ b/GamePedia/src/controleurs/ControleurScript.php @@ -82,9 +82,40 @@ class ControleurScript echo ("<p>---------------------------------------------<br>" . "<p>(name, deck) des personnages du jeu 12342<br>" . "<p>---------------------------------------------<br>"); - foreach ($q1 as $item) { - echo "<p>nom :$q1->name , deck : $q1->deck</p>"; + echo "<p>nom :$item->name , deck : $item->deck</p>"; + } + + $q2 = Game::where('name','like',"Mario%")->get(); + echo ("<p>---------------------------------------------<br>" . + "<p>les personnages des jeux dont le nom (du jeu) débute par 'Mario<br>" . + "<p>---------------------------------------------<br>"); + foreach ($q2 as $game) { + echo "<p>Characters from :$game->name</p>"; + $list = $game->characters()->get(); + foreach ($list as $char) + echo "<p>nom : $char->name</p>"; + } + + $q3 = Company::where("name", "like", "%sony%")->get(); + echo ("<p>---------------------------------------------<br>" . + "<p>les jeux développés par une compagnie dont le nom contient 'Sony<br>" . + "<p>---------------------------------------------<br>"); + foreach ($q3 as $company) { + echo "<p>Games from :$company->name</p>"; + $list = $company->games()->get(); + foreach ($list as $game) + echo "<p>nom : $game->name</p>"; + } + + $q2 = Game::where('name','like',"%Mario%")->get(); + echo ("<p>---------------------------------------------<br>" . + "<p>le rating initial (indiquer le rating board) des jeux dont le nom contient Mario<br>" . + "<p>---------------------------------------------<br>"); + foreach ($q2 as $game) { + $list = $game->ratings()->get(); + foreach ($list as $rate) + echo "<p>id rating_board : $rate->rating_board_id, name : {$rate->rating_boards()->first()->name}</p>"; } } diff --git a/GamePedia/src/modeles/Company.php b/GamePedia/src/modeles/Company.php index b532492..c2bdaac 100644 --- a/GamePedia/src/modeles/Company.php +++ b/GamePedia/src/modeles/Company.php @@ -7,6 +7,6 @@ class Company extends \Illuminate\Database\Eloquent\Model public $timestamps = false; function games(){ - return $this->BelongsToMany('gp\modeles\Game', 'game2company','company_id'); + return $this->BelongsToMany('gp\modeles\Game', 'game_developers','comp_id'); } } diff --git a/GamePedia/src/modeles/Game.php b/GamePedia/src/modeles/Game.php index 2d8743e..1743136 100644 --- a/GamePedia/src/modeles/Game.php +++ b/GamePedia/src/modeles/Game.php @@ -10,17 +10,17 @@ class Game extends \Illuminate\Database\Eloquent\Model public function characters(){ return $this->belongsToMany('gp\modeles\Character', 'game2character','game_id'); } - public function platform(){ + public function platforms(){ return $this->belongsToMany('gp\modeles\Platform', 'game2platform','game_id'); } - public function theme(){ + public function themes(){ return $this->belongsToMany('gp\modeles\Theme', 'game2theme','game_id'); } - public function genre(){ + public function genres(){ return $this->belongsToMany('gp\modeles\Genre', 'game2genre','game_id'); } - public function rating(){ - return $this->belongsToMany('gp\modeles\Rating', 'game2rating','game_id'); + public function ratings(){ + return $this->belongsToMany('gp\modeles\Game_rating', 'game2rating','game_id'); } public function publishers(){ return $this->belongsToMany('gp\modeles\Company', 'game_publishers','game_id'); diff --git a/GamePedia/src/modeles/Game_rating.php b/GamePedia/src/modeles/Game_rating.php index d11d362..acdc7ec 100644 --- a/GamePedia/src/modeles/Game_rating.php +++ b/GamePedia/src/modeles/Game_rating.php @@ -3,14 +3,53 @@ namespace gp\modeles; class Game_rating extends \Illuminate\Database\Eloquent\Model { protected $table = 'game_rating'; - protected $primaryKey ='id'; + protected $primaryKey =['id', 'rating_id']; public $timestamps = false; - + public $incrementing = false; function games(){ return $this->BelongsToMany('gp\modeles\Game', 'game2rating','rating_id'); } function rating_board(){ - return $this->BelongsTo('gp\modeles\Rating_board', 'id'); + return $this->BelongsTo('gp\modeles\Rating_board', 'rating_board_id'); + } + + /** + * Set the keys for a save update query. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + protected function setKeysForSaveQuery(Builder $query) + { + $keys = $this->getKeyName(); + if(!is_array($keys)){ + return parent::setKeysForSaveQuery($query); + } + + foreach($keys as $keyName){ + $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName)); + } + + return $query; + } + + /** + * Get the primary key value for a save query. + * + * @param mixed $keyName + * @return mixed + */ + protected function getKeyForSaveQuery($keyName = null) + { + if(is_null($keyName)){ + $keyName = $this->getKeyName(); + } + + if (isset($this->original[$keyName])) { + return $this->original[$keyName]; + } + + return $this->getAttribute($keyName); } } -- GitLab