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);

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

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题