简体   繁体   English

学说:OneToOne实体关系不起作用

[英]Doctrine : OneToOne entity relationship doesn't work

I'm working with php, and I have two entities Message and Post. 我正在使用php,我有两个实体Message和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: 但是当我在我的控制器中调用message-> getPost() - > getText()时,我收到以下错误消息:

Trying to get property of non-object in C:\\wamp64\\www\\Test\\monApplication\\controller\\mainController.php 试图在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: 我的dbconnection类:

<?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: 最后我的项目架构是这样的:

在此输入图像描述

Try to change 试着改变

private $post;

to

public $post;

(and you should use protected vars) (你应该使用受保护的变量)

You have 2 others problems : 您还有其他2个问题:

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

Should be : 应该 :

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

Because string is limited to 255 caracters length. 因为字符串限制为255个caracters长度。

And your property is "texte" so you should have 你的财产是“texte”所以你应该有

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

instead of 代替

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

first thing i would suggest to debug as you didn't mention it (no reference to messageTable) 我建议调试的第一件事就是你没有提到它(没有引用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. 使用var_dump()或dump()查看类型,并检查它是否返回数据,然后决定如何访问第一条消息。

secondly, notice there is no $message variable, there is $messages (the s in the end). 其次,注意没有$ message变量,有$ message(最后的s)。

also, there is not ->text, there is ->texte 还有,没有 - >文字,有 - > texte

then, fix the context::SUCCESS to $context::SUCCESS 然后,将context :: SUCCESS修复为$ context :: SUCCESS

and lastly, there would be more errors luckily, but hey, var_dump() would be your friend, so make use of it ! 最后,幸运的是会有更多错误,但是嘿,var_dump()会成为你的朋友,所以要好好利用它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM