bonjour à tous,
Je viens vers vous car j’ai un ptit hic au niveau de mon code.
J’ai creer un objet article avec ses attributs et method en appliquant l’hydration.
Après ça , j’ai créer un ItemManager afin de faire l’insertion en BDD.
Seulement fonctionne bien mais l’insertion ne se fait? Est ce normal? je vous laisse mon code afin de me donner quelques pistes.
Merci d’avance
Item class
<?php
namespace app;
use \PDO;
class Item extends \app\BaseElement {
private $_id;
private $_user_id;
private $_category;
private $_image;
private $_price;
private $_amount;
private $_date_add;
public function __construct($data){
$this->hydrate($data);
}
public function id(){return $this->_id;}
public function user_id(){return $this->_user_id;}
public function category(){return $this->_category;}
public function image(){return $this->_image;}
public function price(){return $this->_price;}
public function amount(){return $this->_amount;}
public function date_add(){return $this->_date_add;}
public function setId($id){
$id = (int) $id;
if(is_int($id)){
$this->_id = $id;
}
}
public function setDate_add($date_add){
$this->_date_add = $date_add;
}
public function setUser_id($user_id){
if(is_int($user_id)){
$this->_user_id = $user_id;
}
}
public function setCategory($category){
if(is_string($category)){
$this->_category = $category;
}
}
public function setImage($image){
if(is_int($image)){
$this->_image = $image;
}
}
public function setPrice($price){
if(is_float($price)){
$this->_price = $price;
}
}
public function setAmount($amount){
if(is_int($amount)){
$this->_amount = $amount;
}
}
}
itemManager class
<?php
namespace app;
use \PDO;
class ItemManager extends BaseManager {
public function add(Item $item){
/*$req = $this->_db->prepare(
"INSERT INTO panier_item SET user_id = ?, category = ?, image = ? ,price = ?, amount = ?, date_add = ?",
[$item->user_id(), $item->category(),$item->image(), $item->price(), $item->amount(), $item->date_add()],false,false);
*/
$req = $this->_db->insert("INSERT INTO panier_item SET user_id = :user_id, category = :category, image = :image,price = :price, amount = :amount, date_add = :date_add");
$req->bindValue(':user_id', $item->user_id(), PDO::PARAM_INT);
$req->bindValue(':category', $item->category(), PDO::PARAM_STR);
$req->bindValue(':image', $item->image(), PDO::PARAM_INT);
$req->bindValue(':price', $item->price(), PDO::PARAM_INT);
$req->bindValue(':amount', $item->amount(), PDO::PARAM_INT);
$req->bindValue(':date_add', $item->date_add(),PDO::PARAM_INT);
$req->execute();
}
}
bookcover.php
<?php
$datetime = date("d-m-Y H:i");
$db = new \app\Database($dbname);
$tab2 = array(
'user_id' => $_SESSION['auth']->id,
'category' => 'bookcover',
'image' => 2,
'price' => 17.5,
'amount' => 3,
'date_add' => $datetime
);
$bookcover = new Bookcover($tab,$tab2);
$bookcoverManager = new \app\BookcoverManager( $db);
$listBookCovers = $bookcoverManager->list("SELECT * FROM bookcover");
if(isset($_POST['button'])){
$item = new \app\Item($tab2);
if($item){
$itemManager = new \app\ItemManager($db);
$itemManager->add($item);
}
}
require "views/bookcover.view.php";
+0
-0