gpt4 book ai didi

php - Doctrine : OneToOne entity relationship doesn't work

转载 作者:可可西里 更新时间:2023-10-31 22:46:47 25 4
gpt4 key购买 nike

我正在使用 php,我有两个实体 Message 和 Post。帖子是消息实体中的一个属性,它应该是一对一的单向关系。但是当我在我的 Controller 中调用 message->getPost()->getText() 时,我收到了这个错误消息:

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

消息实体:

<?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;
}
}

?>

Post 实体

<?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;
}

?>

我的数据库连接类:

<?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;
}

}

我的主 Controller :

<?php

class mainController
{

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

return context::SUCCESS;
}
}

最后我的项目架构是这样的:

enter image description here

最佳答案

努力改变

private $post;

public $post;

(你应该使用 protected 变量)

你还有另外两个问题:

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

应该是:

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

因为字符串的长度限制为 255 个字符。

你的属性是“texte”所以你应该有

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

代替

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

关于php - Doctrine : OneToOne entity relationship doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49946967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com