Search code examples
yiicgridview

YII "Property x is not defined" ... except it is


Solution:

There was a Samples.php file in the Document Root of the application. How this actually functioned in the normal Yii execution chain of the app is beyond me. But it did, and I deleted it and now stuff works like normal. Thanks!


Update: This is almost certainly an app-level caching issue, except all caching (that I know of) is disabled. I can go into the produciton Models folder and delete various models, and immediatley Yii throws the expected error that these models don't exist. Not so with the Samples model. It stays around.


I'm having a very strange problem with Yii Search.

What I'm doing is searching on a related model just like I've done 100 times before. This works fine on my development host but as soon as I publish it to the production host, it fails with: Property "Samples.cancer_search" is not defined.

I've double and triple checked that the files are actually being published. The property is listed in the "safe on search" list within rules in the Samples model. The property is declared as a public var in the model. Works perfectly in dev, but when I publish these files (and even the whole app) to production, it still gives property is not defined.

Even stranger than this, I went into the prod server and copied the bad model, controller, and view to use a new name (uses the same db table) and it works fine. All I did was change the name of the model, controller and View from Samples to SamplesTest/SamplesTestController, and it works!

I've disabled all caching (was using memcache) and restarted the server. Still gives the same error.

The model (works on dev, not on prod. shortened for clarity)

 class Samples extends CActiveRecord
 {

// .... // 

// for search/sort on related data
public $cancer_search;
public $cancer_search_test;

// .... // 

public function rules()
{
    return array(
        array('id, patient_id, cancer, cancer_id, cancer_search, cancer_search_test, requested_date, ...', 'safe', 'on'=>'search'),
    );
}

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'cancer' => array(self::BELONGS_TO, 'Cancers', 'cancer_id'),
        'cancerTest' => array(self::BELONGS_TO, 'Cancers', 'cancer_id'),
        )
}

public function attributeLabels()
{
    return array(
        'cancer_id' => 'Cancer Classification',
        'cancer' => 'Cancer Classification',
        'cancer_search' => 'Cancer Classification',
        'cancer_search_test' => 'Cancer Classification',
    );
}

public function search()
{  
    $criteria=new CDbCriteria;        
    $criteria->group = 't.id';  
    $criteria->together = true;

    $criteria->with[] = 'cancer';
    if($this->cancer_search) {
      $criteria->addSearchCondition("cancer.name",$this->cancer_search);
    }

    $criteria->compare('id',$this->id,true);
    $criteria->compare('patient_id',$this->patient_id,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'sort'=>array(
            'attributes'=>array(                   
                 'cancer_search'=>array(
                    'asc'=>'cancer.name',
                    'desc'=>'cancer.name DESC',
                ),
                '*',
            ),
        ),

    ));

}

and the search view

 // on production, this doesn't throw an error, but the search doesn't work
 $columns[]= array(
        'name'  => 'cancer',
        'value'=>'$data->cancer->name', 
        'type'  => 'raw',
        'htmlOptions'=>array('style'=>'width:100px;'),
    );

 // gives property not defined error on prod, even though it is defined, and works fine on dev
 $columns[]= array(
        'name'  => 'cancer_search',
        'value'=>'$data->cancer->name', 
        'type'  => 'raw',
        'htmlOptions'=>array('style'=>'width:100px;'),
    );

  // both of these work fine on dev.

I've cleared the Yii assets folder to no avail.

Am I going insane or is their possibly some caching I'm not aware of?

Thanks.

--

One additional clue- I noticed in the search grid that on the prod server it's not using the correct attribute label in the column header either. Not sure why cause it's the same on dev and prod.


edit 2 I tried simply declaring a public var in the model and tracing it to the log in the controller search method, and same deal. Var exists on dev, but not on prod.

edit 3 Now I know something is being cached. I just deleted the Samples model and it continues to use the old cached version that doesn't find my new properties. Memcached is disabled, and I reset the mysql query cache. Still using this phantom model.


Solution

  • It turned out that there was an old Samples.php model file in the Document Root of the application. I must have accidentally uploaded it there.

    Surprisingly, rather than causing the Yii app to fail, this actually worked to override the Samples.php model that was (or wasn't) in Models/

    I expected that the app would only load its components under Protected but apparently not.

    Thanks.