mysql bit to Active Record

Hi,

I’m having issues with an active record form that I generated in the crud generator in gii. In my form I have:


<div class="row">

		<?php echo $form->labelEx($model,'isActive'); ?>

		<?php echo $form->dropDownList($model,'isActive', array(0=>'No', 1=>'Yes')); ?>

		<?php echo $form->error($model,'isActive'); ?>

	</div>

In my view.php I have this in my CDetailView widget:


'isActive'=>array('name'=>$model->getAttributeLabel('isActive'), 'value'=>$model->isActive?'Yes':'No'),

it seems like model is not returning the right value because no matter what is in my database the view says ‘Yes’ and if I try to update a record using the form it always has ‘No’ selected in the drop down list. I’ve tried changing it in the database to look at either 1 or 0 and I get the same results. I’ve tried just echo’ing out $model->isActive and it doesn’t output anything where if you would echo true; it would output a 1.

The only thing I can say is working with the issue is that when I go to save my update it will enter the correct value into the database. It just won’t read anything correctly.

Any ideas or could someone point me in the right direction for documentation for this because I couldn’t find any?

Thanks,

-Nazum

Sorry, I forgot to mention that isActive is a “bit” datatype in mysql. I’m not sure why yii is handling it differently than all of the other data. I’ve even tried casting it into a boolean and I get incorrect results.

Thanks again,

-Nazum

Any ideas here? I really have no idea why nothing is getting returned for this value.

Thanks,

-Nazum

is the value correctly saved in the database?

var_dump the model. what does it say?

Last night when you asked me that I was on my windows desktop and I couldn’t recreate the problem so I kinda thought I was going crazy for a little bit. This morning I’m on my Mac laptop and the problem is back. I using php 5.3.2 on both. Any ideas why it would be different between the two. The values are correct in the database but it only works when I ran it on my windows machine. I’m about to try it on our Linux production server to see if the issue occurs there. Obviously if it occurs there we have a pretty big problem. Any ideas?

Thanks,

-Nazum

Yeah it reacts the same way as my Mac when served up on our linux server. That makes this a bigger problem. Its not a client issue (I didn’t really expect it to be) because I tried it in several browsers on Linux, Mac OS X, and Windows 7. var_dump($model) says that [“isActive”]=> string(1) “” which is odd that its seeing it as a string but it still gives an empty string as its value. Like I said it is saved as a bit in MySQL. Any Ideas?

Thanks,

-Nazum

Anyone have any ideas on this?

When its served on my Windows 7 machine (which is working), I have: Apache 2.2.11, PHP 5.3.0, and MySQL 5.1.36

On the Linux Server I’m running CentOS 5.5 (not working), I have: Apache 2.2.3, PHP 5.3.5, and MySQL 5.0.77

Those are the newest compatible versions I could find for CentOS.

I don’t know if that helps but this really boggles me. I suppose MySQL could return the bit differently between the two versions since my Windows machine has a newer version but it doesn’t seem like PHP would be likely since the versions are closer. I don’t even know if the versions matter in this case.

Any help is appreciated.

Thanks,

-Nazum

So I figured out what was wrong. It had to do with MySQL. Apparently in the older versions of MySQL bits are thought of differently than newer versions. I resolved the issue by changing the datatype to a tinyint and using 0 as false and anything else as true. It isn’t quite as nice since it could be more than just the two values but it does seem a bit more robust.

-Nazum

I just spent 2 hours on an issue with this. On my Dev machine I had an older version of mysql.

Data would be getting saved just fine to the BIT field, but never could i load the model correctly.

So bit data types are unstable in yii, i would say.

Quick solution, change DataType in the table.

it also could be a problem with the ternary’s first part. Instead of using


$model->isActive ? 'Yes' : 'No'

use


$model->isActive === 1 ? 'Yes' : 'No'

[quote name=‘nazum’ post=‘1’]In my view.php I have this in my CDetailView widget:


'isActive'=>array('name'=>$model->getAttributeLabel('isActive'), 'value'=>$model->isActive?'Yes':'No'),

[/quote]

Just found this old thread and have a solution for using BIT in mysql. Use the PHP function "ord"




$value = ord($record->isActive);



Google "php mysql bit" for more info.

I know this is an old topic but I also ran into the problem and would like to provide a solution for others facing the same problem:

  • create protected/components/EActiveRecord.php

  • change your models to extend to EActiveRecord instead of CActiveRecord




<?php

/**

 * Extends CActiveRecord for an appropriate behavior of bit(1) field in

 * MySQL

 */

class EActiveRecord extends CActiveRecord {

  public function beforeSave() {

    foreach ($this->tableSchema->columns as $columnName => $columnDefinition) {

      if ($columnDefinition->dbType == 'bit(1)') {

        if($this->$columnName) {

          $this->$columnName = 0x01;

        } else {

          $this->$columnName = 0x00;

        }

      }

    }

    return parent::beforeSave();

  }

  public function afterFind() {

    foreach ($this->tableSchema->columns as $columnName => $columnDefinition) {

      if ($columnDefinition->dbType == 'bit(1)') {

        $this->$columnName = ord($this->$columnName);

      }

    }

    return parent::afterFind();

  }

}


?>



I have encountered the same problem. A MySQL column defined as bit (not null default b’0’) works on my local Mac-based environment and fails in the production Linux-based environment.

A good explanation is here: http://gphemsley.wordpress.com/2010/02/08/php-mysql-and-the-bit-field-type/

My easiest solution is to change the database columns to tinyint(1) not null default 0.

Thanks everyone for the explanations!