0

この問題は、過去数時間私を夢中にさせてきました。属性posts_published_dateユーザーエンティティに追加しました (特定のユーザーが最後に投稿した日付を追跡します)。関連する行を次に示します。

エンティティの属性を初期化しています。

/**
 * @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;
    }
}

2 つのデバッグは、次の出力で実行されます。

Debug 1: != 220136
Debug 2: 220136 == 220136

posts_published_dateはデータベースに保存されませんが、posts_published_today取得され、正常に保存されます。

4

1 に答える 1

0

直接アクセスするのではなく、そのクラス変数のセッター関数を使用しないのはなぜですか? 試す:

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 列などを変更した場合に、コードが含まれなくなり、デバッグが困難になります。上記を試してみるとどうなりますか? あなたはすべてを固執していますか?

于 2013-02-16T21:42:24.927 に答える