[Solved] $Record->_Attributes Is Null Occasionally (With Remedy)

[size="4"]SOLUTION: Do not delete your foreign keys[/size]

Hey there.

I am getting trouble with the initialization of an ActiveRecord class. Today I started getting errors of the form:

Cannot use a scalar value as an array (/path/to/framework/CActiveRecord.php:1804)

which turns out to be the attribute assignment directly after the elseif


	 * Creates an active record with the given attributes.

	 * This method is internally used by the find methods.

	 * @param array $attributes attribute values (column name=>column value)

	 * @param boolean $callAfterFind whether to call {@link afterFind} after the record is populated.

	 * @return CActiveRecord the newly created active record. The class of the object is the same as the model class.

	 * Null is returned if the input data is false.


	public function populateRecord($attributes,$callAfterFind=true)








			foreach($attributes as $name=>$value)











			return $record;



			return null;


Xdebug reveals that on that occasion the _attributes property is NULL.

Does anybody know what could be causing this? I’ve tested it on 1.1.12 and 1.1.13 and it is consistent.

This behavior has surfaced on two distinct models. The only thing they have in common is an interface and a similar relation (they are related to the same model through a many many relation).

I’d love to hear if anybody had something similar happen to them.\


Can you post your model code?

Did you override instantiate() method or constructor of AR?

private $_attributes=array();

by default it is not null, must be something in your code that changes the value.

Also verify you have not made any changes to original framework code.

Hi there,

The framework is a pull of the 1.1.12 tag from yii’s official git repo on github. Unmodified.

I attached the two files that behave as said. Sadly they don’t seem to be modifying init(), __construct() or any other init critical method.

Their common ancestor is an automagic class that inherits from CActiverecord. What that does is mostly provide formating and autocompletion. nothing before the file is completely initialized.

I have to add that after checking Yesterday that behavior happens either to the one class or the other. Not both at the same time. When it happens it doesn’t matter whether the model is created via a relation access or a find method.

This succeeds:

$model = CacheContentTypesTerritory::model()->findByPk($id);

$category = $model->contentType;

This fails:

$model = CacheContentTypesTerritory::model()->findByPk($id);

$category = CacheContentType::model()->findByPk($model->content_type_id);

This successds again:

$model = CacheContentTypesTerritory::model()->findByPk($id);

$category = (clone) CacheContentType::model()->findByPk($model->content_type_id);

CacheContentTypesTerritory is a class having a relation to CacheContentType


No idea why Yii fails to initialize the model on the 2nd variant but it does…

What is SelfAwareActiveRecord ? Can you post that code too?

Hi CeBe,

Just in time :slight_smile: apparently I had deleted the foreign keys in my DB for that relation. For some reason that made the model’s init go haywire. Maybe because the name of the relation exists two times? Anyway.

Reinstating the foreign keys did the job