簡體   English   中英

使用Zend_Db將php布爾值插入mysql位列

[英]Insert php boolean into mysql bit column with Zend_Db

我正在使用Zend Framework 1.11.4和Zend_Db。 問題是,我有一個性別列,其值為0或1(BIT(1)),當我設置為false時,插入很好,但是當我設置為true時,出現以下錯誤:'數據對於列而言太長'性別”在第1行“

我已經調試並確認它是布爾值! 如果為false(0),則沒有錯誤,但為true,則發生錯誤(類Application_Model_UserNodeMapper):

public function save(Application_Model_UserNode $user){  
$sex = $user->getSex();  
if($sex == 'm'){  
    $user->setSex(false); //NO ERROR!!  
}  
else{  
        $user->setSex(true); //ERROR!!  
}
$data = $user->getProperties();   
if(null === ($id = $user->getId())) {    
    unset($data['id']);  
    $id = $this->getDbTable()->insert($data);
    return $id;             
} 
else{
   $this->getDbTable()->update($data, array('id = ?' => $id));  
   return null;
}

}  

Application_model_UserNode的代碼(某些屬性是葡萄牙語,為了清楚起見,我已將sexo更改為sex):

<?php


class Application_Model_UserNode
{
protected $id;
protected $nome_completo;
protected $nome_exibicao;   
protected $senha;
protected $status;
protected $email;
protected $sex;
protected $data_nasc;
protected $cidade_id;
protected $pais_id;


function __construct(array $options = null)
{
    if (is_array($options)) {
        $this->setOptions($options);
    }
}

public function __set($name, $value)
{
    $method = 'set' . $name;
    if (('mapper' == $name) || !method_exists($this, $method)) {
        throw new Exception('Invalid userNode property');
    }
    $this->$method($value);
}

public function __get($name)
{
    $method = 'get' . $name;
    if (('mapper' == $name) || !method_exists($this, $method)) {
        throw new Exception('Invalid guestbook property');
    }
    return $this->$method();
}

public function setOptions(array $options)
{
    $methods = get_class_methods($this);
    foreach ($options as $key => $value) {
        $method = 'set' . ucfirst($key);
        if (in_array($method, $methods)) {
            $this->$method($value);
        }
    }
    return $this;
}


public function getId() {
    return $this->id;
}

public function setId($id) {
    $this->id = $id;
}

public function getNome_completo() {
    return $this->nome_completo;
}

public function setNome_completo($nome_completo) {
    $this->nome_completo = $nome_completo;
}

public function getNome_exibicao() {
    return $this->nome_exibicao;
}

public function setNome_exibicao($nome_exibicao) {
    $this->nome_exibicao = $nome_exibicao;
}


public function getSenha() {
    return $this->senha;
}

public function setSenha($senha) {
    $this->senha = $senha;
}

public function getStatus() {
    return $this->status;
}

public function setStatus($status) {
    $this->status = $status;
}

public function getEmail() {
    return $this->email;
}

public function setEmail($email) {
    $this->email = $email;
}

public function getSex() {
    return $this->sex;
}

public function setSex($sex) {
    $this->sex = $sex;
}

public function getData_nasc() {
    return $this->data_nasc;
}

public function setData_nasc($data_nasc) {
    $this->data_nasc = $data_nasc;
}


public function getProperties(){
    $properties = get_object_vars($this);
    return $properties;
}


}

謝謝您的幫助!

位數據類型的MySQL實現不一定是單個位,但是在創建表時可以在1到64位之間變化。 大多數數據庫連接器在數據類型轉換方面都有困難,因為MySQL位實際上並不是您通常會想到的。 正確的解決方案是使用不是bit的列類型,而是使用tinyint(1),如您的注釋中所示。

暫無
暫無

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

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