A
A
Alexander Builov2015-07-19 00:37:52
Yii
Alexander Builov, 2015-07-19 00:37:52

Yii2 and DAO. Am I going right?

Hello. Being in the process of studying the framework, I decided to change the registration from ActiveRecord to Database Access Objects. In principle, I did it and everything works, but the question is, am I doing it right at all ?? Since there are not so many similar examples on the network, it’s clear that no one would even think of doing registration / authorization on the DAO, but still ...
In the controller, I describe the standard method:

public function actionRegister() {
    $model = new RegForm();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        if ($user = $model->reg()) {
            echo '<pre>'; print_r($user); echo '</pre>';
        } else {
            echo 'Register Error';
        }
    }
    return $this->render('reg', ['model' => $model]);
}

Which conducts validation, etc., in RegForm, it confuses me that it is impossible to check email for uniqueness except for:
public function rules() {
    return [
        // ...
        ['email', 'checkEmail'],
        // ...
    ];
}
public function checkEmail($attribute) {
    if (User::checkEmail($this->email))
        $this->addError($attribute, 'Эта почта уже занята!!!');
}

In the User extends Model
public function checkEmail($email) {
    $db = Yii::$app->db;
    $check = $db->createCommand('SELECT COUNT(*) FROM {{%user}} WHERE email=:email')
        ->bindValue(':email', $email)
        ->queryOne();
    return $check['COUNT(*)'];
}

Is it possible to shorten it somehow?
Next comes saving:
public function reg() {
    return User::add($this->email, $this->password, $this->action);
}

And in User:
public function add($email, $password, $action = 'Y') {
    $db = Yii::$app->db;
    $db->createCommand()->insert('{{%user}}', [
        'action'        => $action,
        'email'         => $email,
        'password_hash' => Yii::$app->security->generatePasswordHash($password),
        'auth_key'      => Yii::$app->security->generateRandomString(),
        'create_at'     => date('Y-m-d H:i:s')
    ])->execute();
    $ress = $db->createCommand('SELECT * FROM {{%user}} WHERE email=:email')
        ->bindValue(':email', $email)
        ->queryOne();
    return $ress;
}

And that's all... Everything works great, but the fact that when all this is done, our whole 5 queries are executed:
SELECT COUNT(*) FROM `bav_user` WHERE email='[email protected]'
SHOW FULL COLUMNS FROM `bav_user`
SHOW CREATE TABLE `bav_user`
INSERT INTO `bav_user` (`action`, `email`, `password_hash`, `auth_key`, `create_at`) VALUES ('Y', '[email protected]', '$2y$13$qZC4jOM0gLG7vgvWU8t0keENXlnYFzoetrsFzQpkavwktootfD.Rq', 'RuHh8UQK5V16FsEKSxZPLvSLQNhw-xCU', '2015-07-19 00:10:08')
SELECT * FROM `bav_user` WHERE email='[email protected]'

What is the second most difficult.
Am I on the right track or am I missing something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-07-19
Protko @Fesor

on the DAO, no one would even think of it,

Well, you've come. DAO is normal if you have it all closed in some repository. Or CQRS indulge. In general, it is up to you to decide then how to implement the persistence layer
That's right, a very subjective thing. Basically, this is correct that does not prevent further expansion of the system. In particular, this correspondence persistence ignorance is correct for me. Of the php ORMs, this business allows only the doctrine to be implemented (it seems like the song will soon be on the list), so DAO is better than active records.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question