Issue modifying a model in a static method

Hi, I try to modify a model loaded from DB through a model class having a static method. I try to ask to DB if exists a row, in negative result it is created without any issue, but in another case it crash in first else line. Code:




public static function obtenerIdAnonimo()

	{

		$ip = ip2long(CHttpRequest::getUserHostAddress());

		$anonimo = self::model()->findAllByAttributes(array('ip' => $ip));

		

		if ($anonimo == null)

		{

			$anonimo = new UAnonimo;

			$anonimo->ip = $ip;

			$anonimo->fecha_creacion = date('Y-m-d H:i:s');

			$anonimo->fecha_visita = date('Y-m-d H:i:s');

			$anonimo->save();

		}

		else

		{

			$anonimo->fecha_visita = date('Y-m-d H:i:s');

			$anonimo->save();

		}

		

		return $anonimo->id;

	}



I’m using Yii 1.1.12 and PHP 5.3.16. I check fecha_visita property with a var_dump and it exists. I try to change “$anonimo = self::model()->findAllByAttributes(array(‘ip’ => $ip));” with"$anonimo = UAnonimo::model()->findAllByAttributes(array(‘ip’ => $ip))" without results.

¿Anyone have some idea of my issue?

Hi JaCk0,

"findAllByAttributes()" returns an array of AR objects, not an instance of AR object.

You can use "findByAttributes()" instead.