簡體   English   中英

Symfony-實體-一個具有兩個setter / getter的字段?

[英]Symfony - entity - one field with two setter / getter?

我想要具有兩個或多個getter / setter,f的字符串類型列(用於位操作)。 例如:

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

形式:

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

當我選中復選框時,它僅“輸入”最后一個設置器,而不是全部:

對不起我的英語不好

好的,我找到了一個解決方案-錯誤在於我的“ get函數(它沒有返回正確的值,因此他認為該字段未更改)”。

檢查位是否正確的正確方法:return($ this-> status&$ this :: STATUS_NEW?true:false);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM