将PHP布尔与Zend_Db的MySQL的位列 [英] Insert php boolean into mysql bit column with Zend_Db

查看:185
本文介绍了将PHP布尔与Zend_Db的MySQL的位列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Zend框架1.11.4也Zend_Db的。问题是,我有一个列色情其值为0或1(BIT(1)),当我把假的插入是好的,但是当我把真正出现以下错误:数据太长列'性'在行1

I'm using Zend Framework 1.11.4 and also Zend_Db. The problem is, I have a column for sex which has the value 0 or 1(BIT(1)), when I put false the insertion is fine, but when I put true the following error appears: 'Data too long for column 'sex' at row 1 '

我已经调试和验证这是一个布尔值!假(0)没有错误,但与真正的错误发生(类Application_Model_UserNodeMapper):

I already debugged and verified it's a boolean! With false(0) no error, but with true the error happens (Class 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;
}

}  

code为Application_model_UserNode(某些属性是葡萄牙语,我已经改变了sexo性别澄清的东西):

Code for Application_model_UserNode(Some properties are in portuguese, I have changed sexo to sex to clarify things):

<?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的执行位数据类型不一定是单个位,但在创建表的一个时间和64位之间可能会有所不同。大多数DB连接器具有与数据类型转换的困难,因为MySQL的位实际上不是一点,你会通俗考虑一下。正确的解决方法是使用一个列类型,是不是有点,但TINYINT(1),如您的评论表示。

The MySQL implementation of the bit datatype is not necessarily a single bit, but can vary between one and 64 bits at the time of the creation of the table. Most db connectors have difficulty with the datatype conversion because the MySQL bit is not actually a bit as you would colloquially think about it. The correct solution is to use a column type that is not bit, but tinyint(1), as indicated in your comment.

这篇关于将PHP布尔与Zend_Db的MySQL的位列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆