Attachbehavior on model

How do you attach a behavior on the fly on a CActiverecord?

Atm I have


 $hotspots = new Hotspots();

        $hotspots->attachBehavior('morray','ext.morray.MorrayBehavior');

        $hotspots = $hotspots::model()->with('metadata')->findAll();


        $hotspots = CJSON::encode($hotspots->toArray());

but I get an Fatal error: Call to a member function toArray() on a non-object.

I know I can assign it through public function behaviors() in the model. But I was curious about the attachBehaviour method. Any pointers?