Php 条令未加载或保存实体中的特定属性
在过去的几个小时里,这个问题一直让我发疯。我将属性posts\u published\u date添加到我的user实体中(它跟踪某个特定用户上次发表文章的日期)。以下是一些相关信息: 初始化实体中的属性Php 条令未加载或保存实体中的特定属性,php,mysql,doctrine-orm,doctrine,Php,Mysql,Doctrine Orm,Doctrine,在过去的几个小时里,这个问题一直让我发疯。我将属性posts\u published\u date添加到我的user实体中(它跟踪某个特定用户上次发表文章的日期)。以下是一些相关信息: 初始化实体中的属性 /** * @Column(type="integer", length=3, nullable=false) */ protected $posts_published_today; /** * @Column(type="string", length=7, nullable=tru
/**
* @Column(type="integer", length=3, nullable=false)
*/
protected $posts_published_today;
/**
* @Column(type="string", length=7, nullable=true)
*/
protected $posts_published_date;
/**
* @Column(type="integer", length=3, nullable=true)
*/
protected $posts_published_limit;
使用实体中的属性:
public function setPostsPublishedToday($value){
$today = date("now");
if ($today != $this->posts_published_date){
$this->posts_published_today = 1;
debug($this->posts_published_date . " != " . $today, "1");
$this->posts_published_date = $today;
debug($this->posts_published_date . " == " . $today, "2");
} else {
$this->posts_published_today = $value;
}
}
这两个调试以以下输出运行:
Debug 1: != 220136
Debug 2: 220136 == 220136
发布日期没有存储在数据库中,但是今天发布的发布可以很好地检索和存储。为什么不使用该类变量的setter函数,而不是直接访问它?尝试:
public function setPostsPublishedToday($value)
{
$today = date("now");
if ($today != $this->getPostsPublishedDate()) {
$this->posts_published_today = 1;
$this->setPostsPublishedDate($today);
} else {
$this->posts_published_today = $value;
}
}
这样,所有内容都通过基本方法进行路由,您可以保证这些方法的行为。如果您允许您的代码访问类变量,那么如果您碰巧更改了某个特定的DB列,那么它将变得不可污染,并且是调试的噩梦。当您尝试上述操作时会发生什么?你坚持一切吗?他在实体内部工作。。。我不担心在这样的环境中使用公共API,但这在OOP中总是一个大讨论:)是的,我在实体本身中。你建议的改变没有效果。结果是,今天发布的帖子成功地被更改了(我在phpMyAdmin中进行了检查),而发布的帖子日期保持不变。这说明persist和flush工作正常。您应该添加在本例中使用的代码。此外,考虑使用<代码>“DATETIME”< /COD>字段类型,用于<代码> PsSpPusiSuffDyDe< <代码>属性,然后最终分配一个<代码> DATESTIME/<代码>对象。