php not recognizing CActiveRecord integer as integer

Hi all,

I used gii to create a model of a database table. Here is the table definition:


CREATE TABLE `omega_activemove` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `rigid` int(11) NOT NULL ,

  `x` float NULL ,

  `y` float NULL ,

  `speed` float NOT NULL DEFAULT '0',

  `resourcenodeid` int(11) DEFAULT NULL,

  `salvageid` int(11) DEFAULT NULL,

  PRIMARY KEY(`id`),

  UNIQUE KEY(`rigid`),

  KEY `activemovern_ref` (`resourcenodeid`),

  KEY `activemovesrig_ref` (`salvageid`),

  KEY `activemoverig_ref` (`rigid`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

The header of the CActiveRecord looks like this:


/**

 * This is the model class for table "{{activemove}}".

 *

 * The followings are the available columns in table '{{activemove}}':

 * @property integer $id

 * @property integer $rigid

 * @property double $x

 * @property double $y

 * @property double $speed

 * @property integer $resourcenodeid

 * @property integer $salvageid

 *

 * The followings are the available model relations:

 * @property Resourcemap $resourcenode

 * @property Rig $salvage

 * @property Rig $rig

 */



I wrote some code where it gets all the rows of this table and processes each one. At one point I call another function sending the rigid property. In the subsequent function I check to see if it is an integer by using is_int(). It returns false. When I echo the value, it is the number 1 (one).

Here is the looping function and the first part of the called function:


    public function moveLoop () {

        

        // Run through active moves

        $moves = Activemove::model()->findAll();

        

        foreach ($moves as $move) {

            // Move rig

            

            $result = [b]omegaRig::moveRigXY($move->rigid, $move->x, $move->y, $move->speed);[/b]


            // If Rig at final stop start extract/salvage if applicable

            if ($result == 'arrived') {

                if (! is_null($move->resourcenodeid)) {

                    // start an extraction

                    omegaExtract::startExtraction($move->rigid,$move->resourcenodeid);

                } elseif (! is_null($move->salvageid)) {

                    // start salvage operation

                }

                Activemove::model()->deleteByPk($move->id);

            }

            

        }

       

    }


   public function moveRigXY($rigid, $x, $y, $speed, $duration = 1) {


        // speed in km/hr, duration in ticks

        

        if (is_int($rigid)) {

            echo "ping\n";

            $rig = Rig::model()->findByPk($rigid);

        } else {

            echo "pong\n";

            $rig = $rigid;

        }

 

This always returns "pong" as it is not recognizing $rigid as a 1 (one).

Hi,

try var_dump() to see the type of your variable. I’m guessing it’s a string and that is why your test - is_int() - is not going to pass. Use is_numeric() instead.

I have tested it a bit more and have seen the following"

is_int($move->rigid) in the calling function returns true.

is_int($rigid) in the called function returns false.

Very disturbing.

When I change the call to this it works:

$result = omegaRig::moveRigXY(b[/b]$rigid1, $move->x, $move->y, $move->speed);

I do not want to have to do that everywhere. This hasn’t happened to me before with Yii and I have written a lot of similar code.

Shouldn’t Yii CActiveRecord type a database integer as an integer?

You could always use is_numeric.

is_numeric fixes the "problem". It still bothers me that is_int for an integer passed to a function is not recognized as an integer.

Based on this, it looks like the ActiveRecord is correctly setting it as int, but php’s loose typing isn’t properly keeping it when it gets passed to a function. Don’t think there is anything Yii can do to fix that tho.

I was going to suggest using function type hinting, but apparently you can’t do that with int, only Objects and array.

Rather than the type getting lost when passed to a function, I imagine an int passed to a function will always be an int at the other side, perhaps it’s related to the magic method stuff. Using a magic method in that instance results in a string. Perhaps doing:




$rigid = $move->rigid;

$result = omegaRig::moveRigXY($rigid, $move->x, $move->y, $move->speed);



might work?