Yii Framework Forum: [Solved] $Record->_Attributes Is Null Occasionally (With Remedy) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Solved] $Record->_Attributes Is Null Occasionally (With Remedy) the attributes array of an ActiveRecord class apears to be Null

#1 User is offline   Kiriakos Kappa Krastillis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 06-June 11
  • Location:Greece

Posted 09 July 2013 - 06:05 PM

SOLUTION: Do not delete your foreign keys

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)
	{
		if($attributes!==false)
		{
			$record=$this->instantiate($attributes);
			$record->setScenario('update');
			$record->init();
			$md=$record->getMetaData();
			foreach($attributes as $name=>$value)
			{
				if(property_exists($record,$name))
					$record->$name=$value;
				elseif(isset($md->columns[$name]))
					$record->_attributes[$name]=$value;
			}
			$record->_pk=$record->getPrimaryKey();
			$record->attachBehaviors($record->behaviors());
			if($callAfterFind)
				$record->afterFind();
			return $record;
		}
		else
			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.\
cheers
I like software development.
KINDstudios.gr
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 538
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 09 July 2013 - 06:16 PM

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.
0

#3 User is offline   Kiriakos Kappa Krastillis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 06-June 11
  • Location:Greece

Posted 10 July 2013 - 09:23 AM

View PostCeBe, on 09 July 2013 - 06:16 PM, said:

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.

Attached File(s)


I like software development.
KINDstudios.gr
0

#4 User is offline   Kiriakos Kappa Krastillis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 06-June 11
  • Location:Greece

Posted 11 July 2013 - 05:54 AM

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....
I like software development.
KINDstudios.gr
0

#5 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 538
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 11 July 2013 - 06:11 AM

What is SelfAwareActiveRecord ? Can you post that code too?
0

#6 User is offline   Kiriakos Kappa Krastillis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 06-June 11
  • Location:Greece

Posted 11 July 2013 - 07:53 AM

Hi CeBe,

Just in time :-) 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
I like software development.
KINDstudios.gr
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users