Identity map

Why doesn’t Yii have an identity map?




$test = Test::model()->findByPk(1);

$test->someAttribute = true;


$test = Test::model()->findByPk(1);

return $test2->someAttribute;



returns false.

Identity map

I think it would be great, I’ve missed it a couple of times.

Thanks.

seems like you’ve forgotten to call $test->save()

No, thats the whole idea, see this example:




$test = Test::model()->findByPk(1);

$test->someAttribute = true;

echo doSomethingCleverBeforeSave();

$test->save();


function doSomethingCleverBeforeSave() {

	$test = Test::model()->findByPk(1);

	if ($test->someAttribute === true) return true;

}



If the primary key is a user input, and the function is independent to the rest of the script, you would expect it to return true.

I think his point is that if a certain model is referenced multiple times in the same controller action, it should be singleton (i.e. a reference should be passed back by findByPk() instead of a copy with potentially stale data from the db.

I agree that would be great, but I don’t think you should hold your breath until this is implemented ;)

Exactly, i really need it and it’s implemented in most modern frameworks. It’s described as a “must have” in the book “PHP objects, design patterns and practice”.

If anyone’s still reading this, I’ve implemented an identitymap for Yii, here’s the forum post for it:http://www.yiiframework.com/forum/index.php?/topic/23802-identitymap-feature-for-active-record/