Activerecord - Property Of Non-Object - To Asarray() Or Not To Asarray()...

Working on what I thought would be somewhat simple login protection, preventing a User from doing to many Login attempts…




$model = self::find()

	//->from('tbl_protect')//already set in model.

	->orderBy(['create_time' => SORT_ASC])

	->select('expiration_time')

	->limit('1')

	//->asArray()

	->all();






// github - yii2/docs/guide/active-record.md

// to return customers in terms of arrays rather than `Customer` [b]objects[/b]:

$customers = Customer::find()

    ->asArray()

    ->all();



If I understand this correctly, if “asArray()” isn’t set than returned output from query would be a object? The problem I’m facing is when I attempt to gather “expiration_time” I get “Trying to get property of non-object”, so I thought to gather it from array…

With the "asArray()" output off.




Array

(

    [0] => app\models\LoginAttempt Object

        (

            [_attributes:yii\db\BaseActiveRecord:private] => Array

                (

                    [expiration_time] => 1386878094

                )


            [_oldAttributes:yii\db\BaseActiveRecord:private] => Array

                (

                    [expiration_time] => 1386878094

                )


            [_related:yii\db\BaseActiveRecord:private] => Array

                (

                )


            [_errors:yii\base\Model:private] => 

            [_validators:yii\base\Model:private] => 

            [_scenario:yii\base\Model:private] => default

            [_events:yii\base\Component:private] => 

            [_behaviors:yii\base\Component:private] => Array

                (

                )


        )


)



with "asArray()" output on.




Array

(

    [0] => Array

        (

            [expiration_time] => 1386878094

        )


)



At this point I was thinking i could…




$c = mysql_fetch_array($model);

print_r($c['expiration_time']);

var_dump($c['expiration_time']);



but i get “mysql_fetch_array() expects parameter 1 to be resource, array given”. I’m not entirely certain that my approach is the right way to handle this situation. I’m trying to collect that one bit of data so i can compare it with another piece of data and depending on it the user would be banned or not. I’ve been toying with this for an entire day, thought it wouldn’t hurt to see if anyone has suggestions.

Thanks for taking the time to read this.


->all()

is for getting multiple records. That’s why you’re getting array of something in both cases. For just a single record you can use


->one()

Samdark I could kiss you!!!




$model = self::find()

	->orderBy(['create_time' => SORT_ASC])

	->select('expiration_time')

	->limit('1')

	->one();



was the trick. :smiley:

[edited] Yii2 is so damn awesome btw, thanks for everything you have done! and the entire team.