gpt4 book ai didi

php - 坚持冲刷后 Doctrine 关系消失

转载 作者:行者123 更新时间:2023-11-30 23:33:53 25 4
gpt4 key购买 nike

我在记录笔记的实体和让用户发表评论的实体之间建立了多对一关系。即一个用户可以发表多条评论。

第一次查看页面时评论显示正常,$note->employee->user根据与notes表的多对一关系从employee表中获取用户名。通过发布表单添加新注释时:

$activity = new \S2\Entity\Crmnotes;
$activity->solicitorid = $solicitorid;
$activity->note = $activityDescription;
$activity->employeeid = 2;
$activity->type = $activityType;
$this->_em->persist($activity);
$this->_em->flush();

然后该页面将显示来自 notes 实体的数据,但不显示来自它的任何关系的数据,即 $notes->note,$notes->type display but $notes->employee->user causes a Trying to get property非对象错误。

下次访问该页面时,所有数据将照常显示。

编辑

另外一点,日期时间对象 $note->lastmodified 在插入记录后也无法显示。当我尝试格式化日期时,我收到 Call to a member function format() on a non-object 的错误,例如$note->lastmodified->format('d/m/Y H:i');

编辑是的,这是 Doctrine 2。根据要求:

namespace S2\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
* Crmnotes
*
* @Table(name="crmnotes")
* @Entity
*/
class Crmnotes extends \S2_Doctrine_Entity
{
/**
*
* @ORM\ManyToOne(targetEntity="S2\Entity\Tblsolicitor", inversedBy="crmnotes")
* @JoinColumn(name="SolicitorID", referencedColumnName="SolicitorID")
*
*/
private $solicitor;

/**
*
* @ORM\ManyToOne(cascade={"persist"}, targetEntity="S2\Entity\Tblemployees", inversedBy="crmnotes")
* @JoinColumn(name="EmployeeID", referencedColumnName="EmployeeID")
*
*/
private $employee;

/**
* @var integer $notesid
*
* @Column(name="notesid", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $notesid;

/**
* @var integer $type
*
* @Column(name="type", type="integer", nullable=false)
*/
private $type;

/**
* @var integer $solicitorid
*
* @Column(name="SolicitorID", type="integer", nullable=false)
*/
private $solicitorid;

/**
* @var text $note
*
* @Column(name="Note", type="text", nullable=false)
*/
private $note;

/**
* @var integer $employeeid
*
* @Column(name="EmployeeID", type="integer", nullable=false)
*/
private $employeeid;

/**
* @var datetime $lastmodified
*
* @Column(name="LastModified", type="datetime", nullable=false)
*/
private $lastmodified;

public function __get($name)
{
if (isset($this->$name)){
return $this->$name;
} else {
return '';
}
}

public function __set($name, $value)
{
$this->$name = $value;
}


}

编辑

使用对象而不是尝试手动插入外键时仍然没有乐趣:

$activity = new \S2\Entity\Crmnotes;
$activity->note = $activityDescription;
$activity->type = $activityType;
$solicitor = $this->_em->getRepository('\S2\Entity\Tblsolicitor')->find($solicitorid);
$solicitor->crmnotes->add($activity);
$activity->solicitor = $solicitor;
$employee = $this->_em->getRepository('\S2\Entity\Tblemployees')->find(2);
$this->view->debug = $solicitor->crmnotes[0]->note;
$employee->crmnotes->add($activity);
$activity->employee = $employee;
$this->_em->persist($activity);
$this->_em->flush();

最佳答案

尝试删除

/**
* @var integer $employeeid
*
* @Column(name="EmployeeID", type="integer", nullable=false)
*/
private $employeeid;

当也存在使用同一列的关系时,较新的 Doctrine 会提示它。

* @JoinColumn(name="EmployeeID", referencedColumnName="EmployeeID")

也许引用 id 而不是 EmployeeID ?

  if (isset($this->$name)){
return $this->$name;
} else {
return '';
}

不需要检查,Doctrine如果为空则返回null

我没看到你在哪里设置了 LastModified 吗?

$entity->lastmodified = new \DateTime();

关于php - 坚持冲刷后 Doctrine 关系消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9153790/

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