S
S
Soo2018-07-10 07:14:13
Yii
Soo, 2018-07-10 07:14:13

Yii2, in Active Records how to select the desired properties?

class User extends ActiveRecord implements \yii\web\IdentityInterface
{
    public $id;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;
    public $permissions;

    private static $current_user = [
        'id' => false,
        'username' => false,
        'password' => false,
        'authKey' => false,
        'accessToken' => false,
        'permissions' => false,
    ];
//...
public function setPassword($username, $new_password)
    {
    $user = new User();
    $user = User::find(['username'=>$username])->one();
    var_dump($user);
    if($user){
      $user->password= Yii::$app->getSecurity()->generatePasswordHash($new_password);
      $user->save(); 
      return 1;
    }
        return null;
    }
}

The dump outputs the following:
object(app\models\User)#241 (15) { ["id"]=> NULL ["password"]=> NULL ["authKey"]=> NULL ["accessToken"]=> NULL ["permissions"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(1) ["username"]=> string(5) "zorro" ["password"]=> string(60) "$2y$13$ycaghC0uigV2pgWyT2lmIOHXYPVsclzVejgAGSnWESjPPYUFwRlBq" ["permissions"]=> string(2) "ad" ["description"]=> NULL } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(1) ["username"]=> string(5) "zorro" ["password"]=> string(60) "$2y$13$ycaghC0uigV2pgWyT2lmIOHXYPVsclzVejgAGSnWESjPPYUFwRlBq" ["permissions"]=> string(2) "ad" ["description"]=> NULL } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } } object(app\models\User)#262 (15) { ["id"]=> NULL ["password"]=> NULL ["authKey"]=> NULL ["accessToken"]=> NULL ["permissions"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(1) ["username"]=> string(5) "zorro" ["password"]=> string(60) "$2y$13$ycaghC0uigV2pgWyT2lmIOHXYPVsclzVejgAGSnWESjPPYUFwRlBq" ["permissions"]=> string(2) "ad" ["description"]=> NULL } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(1) ["username"]=> string(5) "zorro" ["password"]=> string(60) "$2y$13$ycaghC0uigV2pgWyT2lmIOHXYPVsclzVejgAGSnWESjPPYUFwRlBq" ["permissions"]=> string(2) "ad" ["description"]=> NULL } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }

With a simple call to $user->id, I cannot call the properties, there are null everywhere. If you set find()->isArray(), the data is displayed normally in an array. How can I make it possible to call via properties and then save via $user->save()?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2018-07-10
@Soo

And if you remove the properties that you have in the database? for ActiveRecord models, you can omit properties so that auto-substitution works, you can use php-doc

/**
 * Class User
 * @property $username string
 */
class User extends ActiveRecord implements \yii\web\IdentityInterface
{

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question