Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine : OneToOne entity relationship doesn't work

I'm working with php, and I have two entities Message and Post. The post is an attribute in the message entity and it's supposed to be a one to one unidirectional relation. But when I call message->getPost()->getText() in my controller I get this error message:

Trying to get property of non-object in C:\wamp64\www\Test\monApplication\controller\mainController.php

The Message entity:

<?php

/**
 * @Entity
 * @Table(name="message")
 */
class message{

    /** @Id @Column(type="integer")
     *  @GeneratedValue
     */
    public $id;

    /**
     * @ORM\OneToOne(targetEntity="post", cascade={"persist"})
     * @JoinColumn(name="post", referencedColumnName ="id")
     */
    private $post;

    /** @Column(type="integer") */
    public $likes;

    public function getPost(){
        return $this->post;
    }
    public function getLikes(){
         return $this->likes;
    }
}

?>

The Post entity

<?php

/**
 * @Entity
 * @Table(name="post")
 */
class post{

    /** @Id @Column(type="integer")
     *  @GeneratedValue
     */
    public $id;

    /** @Column(type="string", length=2000) */
    public $texte;

    /** @Column(type="string", length=200) */
    public $image;

    /** @Column(type="TIMESTAMP", length=4000) */
    public $date;
}

?>

My dbconnection class:

<?php

define ('HOST', 'localhost') ;
define ('USER', 'root'  ) ;
define ('PASS', '' ) ;
define ('DB', 'tp' ) ;

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

class dbconnection{

private static $instance=null, $entityManager;
private $error=null ;

private function __construct(){
    $config = Setup::createAnnotationMetadataConfiguration(array("../../monApplication/model/"), true);

    $param = array(
    'dbname' => DB,
    'user'  => USER,
    'password' => PASS,
    'host'  => HOST,
    'driver' => 'pdo_mysql');

    try{
        self::$entityManager = EntityManager::create($param, $config);
    }
    catch(Exception $e) {
        echo "Probleme connexion base de données:".$e->getMessage();
        $this->error = $e->getMessage();
    }

}

public static function getInstance(){
    if(self::$instance == null){
        self::$instance = new dbconnection();
    }
    return self::$instance;
}

public function closeConnection(){
    self::$instance=null;
}

public function getEntityManager(){
    if(!empty(self::$entityManager))
        return self::$entityManager;
    else
        return NULL;
}


public function __clone(){

}

public function getError(){
    return $this->error;
}

}

My mainController:

<?php

class mainController
{

    public static function showMessage($request,$context){
        $messages = messageTable::getAllMessages();
        echo $message[0]->getPost()->text;   

        return context::SUCCESS;
    }
}

And finally my project architecture is like this:

enter image description here

like image 330
Yassine Younes Avatar asked Apr 20 '18 17:04

Yassine Younes


2 Answers

Try to change

private $post;

to

public $post;

(and you should use protected vars)

You have 2 others problems :

/** @Column(type="string", length=2000) */
public $texte;

Should be :

/** @Column(type="text") */
public $texte;

Because string is limited to 255 caracters length.

And your property is "texte" so you should have

echo $message[0]->getPost()->texte; 

instead of

echo $message[0]->getPost()->text;
like image 116
ArGh Avatar answered Oct 30 '22 09:10

ArGh


first thing i would suggest to debug as you didn't mention it (no reference to messageTable)

messageTable::getAllMessages();

using var_dump() or dump() to see the type, and check if it returns data, then decide how you will access to the first message.

secondly, notice there is no $message variable, there is $messages (the s in the end).

also, there is not ->text, there is ->texte

then, fix the context::SUCCESS to $context::SUCCESS

and lastly, there would be more errors luckily, but hey, var_dump() would be your friend, so make use of it !

like image 35
volkovmqx Avatar answered Oct 30 '22 11:10

volkovmqx