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

图片转代码服务由CSDN问答提供 功能建议

我希望有两个或更多getter / setter,f的字符串类型列(用于位操作)。 例如:

  const STATUS_NEW = 0b00001000; 
 nstst STATUS_NEW2 = 0b00000010; 
 
 / ** 
 * @var String 
 * 
 * @ORM \ Column  (name =“modelStatus”,type =“string”,length = 1,nullable = true)
 * / 
private $ status; 
 
公共函数getNew()
 {
 return($ this->  status和$ this :: NEW); 
} 
 
公共函数setNew(bool $ set)
 {
 if($ set){
 $ this-> status =($ this-> status  | $ this :: STATUS_NEW); 
} else {
 $ this-> status =($ this-> status&〜$ this :: STATUS_NEW); 
} 
返回$ this; 
  } 
 
公共函数getNew2()
 {
 return($ this-> status和$ this :: NEW2); 
} 
 
公共函数setNew2(bool $ set)
 {
 if  ($ set){
 $ this-> status =($ this-> status | $ this :: NEW2); 
} else {
 $ this-> status =($ this-> status  &〜$ this :: NEW2); 
} 
以表格形式返回$ this; 
} 
   
 
 

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

当我选中复选框时,它只“输入”最后一个设置器,而不是全部:(

对不起我的英文

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    打赏 评论

相关推荐 更多相似问题