Answer the question
In order to leave comments, you need to log in
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
class Users extends
$db = new users();
$res = $db->find()->All();
var_dump($res);
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);
}
});
});
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);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question