S
S
Stepan2015-01-26 13:39:32
MySQL
Stepan, 2015-01-26 13:39:32

How to access desired data from a table in Yii2?

Array
(
    [0] => app\models\Users Object
        (
            [id] => 
            [username] => 
            [password] => 
            [authKey] => 
            [_attributes:yii\db\BaseActiveRecord:private] => Array
                (
                    [id] => 1
                    [username] => victor
                    [password] => 12344321
                    [authKey] => 12344321
                    [accessToken] => 12344321
                )

            [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
                (
                    [id] => 1
                    [username] => victor
                    [password] => 12344321
                    [authKey] => 12344321
                    [accessToken] => 12344321
                )

            [_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] => Array
                (
                )

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

        )

    [1] => app\models\Users Object
        (
            [id] => 
            [username] => 
            [password] => 
            [authKey] => 
            [_attributes:yii\db\BaseActiveRecord:private] => Array
                (
                    [id] => 2
                    [username] => admin
                    [password] => 123443221
                    [authKey] => 12344321
                    [accessToken] => 12344321
                )

            [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
                (
                    [id] => 2
                    [username] => admin
                    [password] => 123443221
                    [authKey] => 12344321
                    [accessToken] => 12344321
                )

            [_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] => Array
                (
                )

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

        )

)

Here is what the command gives me
$user = Users::find()->all();
Then I need to go through all the fields and find the one I need
public static function findByUsername($username)
    {

        $user = Users::find()->all();

        foreach ($user as $usr) {

            print_r($usr);
            if (strcasecmp($usr['username'], $username) === 0) {

                return new static($usr);
            }
        }

        return null;
    }

Right here. But it doesn't work, how can I fix it? What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2015-01-26
@xoma2

$usr->username is needed instead of $usr['username'].
Well, or User::find()->asArray()->all(), decide for yourself.
UPD: in general, you wrote hellish trash. To find a user by field, you need to writeUser::findOne(['username'=>$username]).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question