tommysonsylverstone / Projet5OCR_TB

0 stars 0 forks source link

Insert a post using both Post.php and PostManager.php file #27

Closed tommysonsylverstone closed 5 years ago

tommysonsylverstone commented 5 years ago

I've spent two days and I still can't get why I can't insert a post. I've tried many things, but I still get the same errors.

Undefined variable: data in C:\wamp64\www\PROJET_5\test\addPostTest\models\Post.php on line 13

and

Fatal error: Uncaught TypeError: Argument 1 passed to Post::hydrate() must be of the type array, null given, called in C:\wamp64\www\PROJET_5\test\addPostTest\models\Post.php on line 13 and defined in C:\wamp64\www\PROJET_5\test\addPostTest\models\Post.php on line 16

It seems like the hydrate function I did is at fault, but I did it as I was taught.

addPostView.php `<?php

function chargeClass($class) { require '../models/'.$class.'.php'; }

spl_autoload_register('chargeClass');

session_start(); $title = 'Ajout de billet'; $pManager = new PostManager(); if (isset($_POST['confirm-button'])) {
$post = new Post(); $titleP = $post->getTitleP($_POST['titleP']); $chapo = $post->getChapo($_POST['chapo']); $content = $post->getContent($_POST['content']); $authorName = $post->getAuthorName($_POST['authorName']); $pManager->addPost($post);
}

ob_start();

?>





<?php $content = ob_get_clean(); ?>

<?php require('includes/template.php'); ?> `

Post.php

`<?php

class Post { private $id; private $titleP; private $chapo; private $content; private $postDate; private $authorName; private $lastUpdated;

public function __construct() {
        $this->hydrate($data);
    }

    public function hydrate(array $data) {
        foreach ($data as $key => $value) {
            $method = 'set'.ucfirst($key);

            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }
    }

public function getId():int {
    return $this->id;
}

public function setId(int $id):void {
    $this->id = $id;
}

public function getTitleP():string {
    return $this->titleP;
}

public function setTitleP(string $titleP) {
    $this->titleP = $titleP;
}

public function getChapo():string {
    return $this->chapo;
}

public function setChapo(string $chapo) {
    $this->chapo = $chapo;
}

public function getContent():string {
    return $this->content;
}

public function setContent(string $content) {
    $this->content = $content;
}

public function getPostDate() {
    return $this->postDate;
}

public function setPostDate($postDate) {
    $this->postDate = $postDate;
}

public function getAuthorName():string {
    return $this->authorName;
}

public function setAuthorName(string $authorName) {
    $this->author = $authorName;
}

public function getLastUpdated() {
    return $this->lastUpdated;
}

public function setLastUpdated($lastUpdated) {
    $this->lastUpdated = $lastUpdated;
}

} ` PostManager.php

`<?php

require_once('BaseManager.php');

class PostManager extends BaseManager { public function __construct() { $this->db = $this->dbConnect(); }

// some functions

public function addPost(Post $post) {
    if (!empty($titleP) && !empty($chapo) && !empty($content) && !empty($authorName)) {
    $q = $this->db->prepare('INSERT INTO posts(titleP, chapo, content, postDate, authorName) VALUES(:titleP, :chapo, :content, NOW(), :authorName');

    $q->bindValue(':titleP', $post->getTitleP());
    $q->bindValue(':chapo', $post->getChapo());
    $q->bindValue(':content', $post->getContent());
    $q->bindValue(':authorName', $post->getAuthorName());

    $q->execute();

    $post->hydrate(['id' => $this->db->lastInsertId()]);
    }
}

// some other functions

} `

Any idea @abdelba ?

tommysonsylverstone commented 5 years ago

Text formating is weird... even tho I did this for every code pages