Error: Trying To Get Property Of Non-Object

I have the following code which retrieves player data for a record in the draw table. Having trouble retrieving the players name from a mysql view. I also get the same error if i get the Compteamperson table and try to get the name from the related table.




            $compres = Compresults::model()->findAll('drawid='.$id);

            

            $gbtype = '';

            foreach($compres as $row)

            {

                $pmodel = VwCompteamperson::model()->find('id='.$row->playerid);


                d2l($pmodel);

                

                print $pmodel->fullname;   <<< ERROR HERE


            }




The line "d2l($pmodel);" displays the following structure. I use this function to verify data. it seems fine




VwCompteamperson Object

(

    [_new:CActiveRecord:private] => 

    [_attributes:CActiveRecord:private] => Array

        (

            [id] => 1

            [fullname] => John Smith

            [comppersonid] => 1

            [personid] => 1

            [compteamid] => 9

        )


    [_related:CActiveRecord:private] => Array

        (

        )


    [_c:CActiveRecord:private] => 

    [_pk:CActiveRecord:private] => 

    [_alias:CActiveRecord:private] => t

    [_errors:CModel:private] => Array

        (

        )


    [_validators:CModel:private] => 

    [_scenario:CModel:private] => update

    [_e:CComponent:private] => 

    [_m:CComponent:private] => 

)



Im just not getting why $pmodel->fullname throws an error. I know i will probably kick myself as this seems pretty simple.

Any help would be appreciated.

Hi,

I think the find() is return with null value - the print_r(null) output is empty, so you can’t see

Try this:




<?php

$compres = Compresults::model()->findAll('drawid=:id', array(':id' => $id)); // bind variables to prevent the SQL injection!


$gbtype = '';

foreach ($compres as $row) {

    $pmodel = VwCompteamperson::model()->find('id=:id', array(':id' => $row->playerid));

    if($pmodel === null) {

        print '$pmodel is null - playerid: ' . $row->playerid . "\n";

    } else {

        d2l($pmodel);

        print $pmodel->fullname . "\n";

    }

}



Hi Argent,

Yes you are 100% correct! I didnt realise in the data there was an id that was 0 which returned null, of course if I had have scrolled down further past the first d2l listing I would have easily found it. so thank you for pointing out the obvious that I was having trouble seeing. Also thank you for the code example, I will use that format for future "find" functions.

Regards

Greg J