M
M
Mikha Pankratov2015-03-09 18:57:04
MySQL
Mikha Pankratov, 2015-03-09 18:57:04

Get data from yii2 database (activerecord)?

Hello, created a model...using a generator.
Unable to get data.

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%users}}".
 *
 * @property integer $id
 * @property string $login
 * @property string $password
 * @property string $last_login
 * @property string $name
 * @property string $lastname
 * @property integer $vk_id
 * @property string $datereg
 * @property string $sex
 * @property string $status
 * @property string $birthday
 * @property string $crop
 * @property integer $isactive
 * @property string $activation_code
 */
class Users extends \yii\db\ActiveRecord
{
    
    private $user;
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Comments the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%users}}';
    }
    
    /**
     * @return array primary key of the table
     **/     
    public static function primaryKey()
    {
        return array('id');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['login', 'password', 'name', 'lastname', 'status', 'birthday', 'crop', 'isactive', 'activation_code'], 'required'],
            [['last_login', 'datereg', 'birthday'], 'safe'],
            [['vk_id', 'isactive'], 'integer'],
            [['sex', 'status', 'crop', 'activation_code'], 'string'],
            [['login', 'password'], 'string', 'max' => 40],
            [['name', 'lastname'], 'string', 'max' => 15]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'login' => 'Login',
            'password' => 'Password',
            'last_login' => 'Last Login',
            'name' => 'Name',
            'lastname' => 'Lastname',
            'vk_id' => 'Vk ID',
            'datereg' => 'Datereg',
            'sex' => 'Sex',
            'status' => 'Status',
            'birthday' => 'Birthday',
            'crop' => 'Crop',
            'isactive' => 'Isactive',
            'activation_code' => 'Activation Code',
        ];
    }
    
    public static function findByUsername($username)
    {
        $this->user = Users::find()->where(['name' => $username])->one();
            var_dump($this->user);
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
Sergey, 2015-03-09
@butteff

class Users extends

This means that you are working with the users table.
Where is the controller code?
In it you need to connect:
Work with the database through objects, for example like this:
$db = new users();
$res = $db->find()->All();
var_dump($res);

Here is a link in Russian about working with databases in yii2

S
suppot4545, 2016-10-27
@suppot4545

Call Model Functions in a Controller Definitive
Guide

M
Mikha Pankratov, 2015-03-09
@frmax

I did it and figured out what the problem is.
It turns out the link does not work for me.
Everything became clear in the database, but a new problem was born! :)
I send data from the form - ajax

singup = (function () { 
var name = $("#name").val();
var lastName = $("#last-name").val();
var psw = $("#password").val();
var email = $("#email").val();

    $.ajax({
        type: 'POST',
        url: "site/singup",
        data: {"name": name, "lastname": lastName, "psw":psw},
        error: function(e) {
          console.log(e);
        }
    });
});

I get into the controller in the function >> singup
Here I get the data
public function actionSingup() {
        if (Yii::$app->request->isAjax) {
            $name = Yii::$app->request->post('name');
            $lastname = Yii::$app->request->post('lastName');
            $psw = Yii::$app->request->post('psw');
            $email = Yii::$app->request->post('email');
            
            $countries = Users::find()->where(array('email'=>$email))->orderBy('name')->all();
        var_dump($countries);
        }
    }

And I get an answer
I look at it and cry...
Why did I figure out such an answer, I forgot to do json_encode()...
But I still output an empty array and there is data in the database... that's the question why is there an empty string in the output ???

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question