使用 Zend_Db 将 php 布尔值插入 mysql 位列
我正在使用 Zend Framework 1.11.4 和 Zend_Db。问题是,我有一个性别列,其值为 0 或 1(BIT(1)),当我设置为 false 时,插入没问题,但是当我设置为 true 时,会出现以下错误:“数据对于列来说太长” sex' at row 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;
}
}
感谢您的帮助!
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 '
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 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;
}
}
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
位数据类型的 MySQL 实现不一定是单个位,而是在创建表时可以在 1 到 64 位之间变化。大多数数据库连接器在数据类型转换方面都存在困难,因为 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.