doujiang1001 2018-08-31 06:36
浏览 64
已采纳

Symfony - 实体 - 一个带有两个setter / getter的字段?

I want to have string type column (for bit manipulations) with two or more getter / setter, f. ex:

const STATUS_NEW         = 0b00001000;
const STATUS_NEW2        = 0b00000010;

/**
 * @var String
 *
 * @ORM\Column(name="modelStatus", type="string", length=1, nullable=true)
 */
private $status;

public function getNew()
{
    return ($this->status and $this::NEW);
}

public function setNew(bool $set)
{
    if ($set) {
        $this->status = ($this->status | $this::STATUS_NEW);
    } else {
        $this->status = ($this->status & ~$this::STATUS_NEW);
    }
    return $this;
}

public function getNew2()
{
    return ($this->status and $this::NEW2);
}

public function setNew2(bool $set)
{
    if ($set) {
        $this->status = ($this->status | $this::NEW2);
    } else {
        $this->status = ($this->status & ~$this::NEW2);
    }
    return $this;
}

with form:

$builder->add('new', CheckboxType::class, [
    'required' => false,
->add('new2', CheckboxType::class, [
    'required' => false,
])

When I check checkboxes, it "enter" only the last setter, not in all:(

Sorry for my english

  • 写回答

1条回答 默认 最新

  • duanjue9889 2018-08-31 07:00
    关注

    Ok, I found a solution - error was in my "get function (it doesn't return correct value, so he thought that field wasn't changed).

    Correct way to check if bit is set: return ($this->status & $this::STATUS_NEW ? true : false);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改