How to call a method from model in YII 2 framework? - php

I am newbie in YII framework. I have installed correctly and created a Test Controller & Test Model using GII extension of YII. I have created a method in Model and want to access in Controller but unable to access.
Test controller
<?php
namespace app\controllers\api;
use Yii;
use app\models\api\Test;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class TestController extends \yii\web\Controller
{
public $modelClass = 'app\models\api\Test';
private $model;
public function filters(){
return array(
'accessControl', // perform access control for CRUD operations
array(
'RestfullYii.filters.ERestFilter +
REST.GET, REST.PUT, REST.POST, REST.DELETE, REST.OPTIONS'
),
);
}
public function actions(){
return array(
'REST.'=>'RestfullYii.actions.ERestActionProvider',
);
}
public function accessRules(){
return array(
array('allow', 'actions'=>array('REST.GET', 'REST.PUT', 'REST.POST', 'REST.DELETE', 'REST.OPTIONS'),
'users'=>array('*'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
protected function loadModel( $id = null )
{
if($this->model===null)
{
if($id!==null)
$this->model=TestModel::model()->findByPk($id);
}
return $this->model;
}
public function actionIndex()
{
//return $this->render('index');
//$array = $modelClass::model()->listUserData();
//$array = Yii::app()->model()->listUserData();
//$array = $modelClass->listUserData();
// echo TestModel()->listUserData();
print "<pre>";print_r($this->model->listUserData());
exit;
}
}
Test Model
<?php
namespace app\models\api;
use Yii;
/**
* This is the model class for table "users".
*
* #property integer $id
* #property string $username
* #property string $password
* #property string $email
* #property string $activkey
* #property integer $createtime
* #property integer $lastvisit
* #property integer $superuser
* #property integer $status
*/
class Test extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'users';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['username', 'password', 'email'], 'required'],
[['createtime', 'lastvisit', 'superuser', 'status'], 'integer'],
[['username'], 'string', 'max' => 50],
[['password', 'email', 'activkey'], 'string', 'max' => 128],
[['username'], 'unique'],
[['email'], 'unique'],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'email' => 'Email',
'activkey' => 'Activkey',
'createtime' => 'Createtime',
'lastvisit' => 'Lastvisit',
'superuser' => 'Superuser',
'status' => 'Status',
];
}
public static function listUserData(){
$UserData = Test::model()->findAll('status = "0"');
return $UserData;
}
}
i tried to search on forum but not able to resolve, please can you help me to resolve ?
Thanks in advance.

In the controller.
use pathtotestmodel/Test
public function actionIndex()
{
$model = new Test();
$userdata = $model->listUserData();
}
OR
public function actionIndex()
{
$userdata = Test::listUserData();
}

Its simple create a instance of Model and then call the required function
like
public function actionIndex()
{
$model = new Test();
print "<pre>";print_r($model->listUserData());
exit;
}
Try this
public static function listUserData(){
$UserData = Test::findAll(['status' =>0]);//in Yii2
//$UserData = Test::model()->findByAttributes(array( 'status' => 0 )); in yii 1.1
return $UserData;
}

Related

Can't set property of object if this property is array

yii advanced 2.0.13
#property $content keeps array (before saving in DB it will be encoded in json in database.)
#property string $name
So in ActiveForm I have inputs with names like content[image] or content[anykey], also I have not-array properties like name; I want to put new values after Post, well:
echo $model->name; // output: Test
$model->name = 'any new name';
echo $model->name; // any new name
It works, but
print_r($model->content);
/* output
Array
(
[title] => bla
[anykey] =>
[seo-title] => bla-bla
)*/
$model->content['anykey'] = 'bla-bla-bla';
echo $model->content['anykey']; // null output:
It doesn't. We can not set new value in array property, so I tried next tip:
$content = $model->content;
$content['anykey'] = 'bla-bla-bla';
$model->content = $content;
echo $model->content['anykey']; // bla-bla-bla
It works
Can somebody explain, why is this happening?
Controller
<?php
namespace backend\controllers;
use Yii;
use common\models\Categories;
use backend\models\CategoriesCRUD;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
* CategoriesController implements the CRUD actions for Categories model.
*/
class CategoriesController extends Controller
{
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
...
/**
* Updates an existing Categories model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
// here I put code above
$model->content['anykey'] = 'bla-bla-bla';
echo $model->content['anykey']; // old value
// die();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
}
Model
<?php
namespace common\models;
use Yii;
use yii\web\UploadedFile;
/**
* This is the model class for table "{{%categories}}".
*
* #property integer $id
* #property string $name
* #property integer $status
* #property integer $sort_order
* #property integer $parent_id
* #property string $content
*
* #property Categories $parent
* #property Categories[] $categories
* #property CategoriesRelationships[] $categoriesRelationships
* #property CategoriesRelationships[] $categoriesRelationships0
* #property ProductsToCategories[] $productsToCategories
*/
class Categories extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return '{{%categories}}';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['status', 'sort_order'], 'integer'],
['content', 'each', 'rule' => ['trim']],
[['name'], 'string', 'max' => 255],
[['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => Categories::className(), 'targetAttribute' => ['parent_id' => 'id']],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Заголовок',
'status' => 'Показывать',
'sort_order' => 'Порядок сортировки',
'parent_id' => 'Родительская категория',
'content' => 'Содержание',
];
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
/* Обработка данных перед занесением в базу
в content содержится любая контентная информация
*/
// #TODO добавить собственную валидацию изображений и свои методы для их обработки (Для категорий, аватарок и прч)
// here I put solution, but before I found it I've been trying in Controller
$content = (array) $this->content;
$content['image'] = $this->uploadImage($this, $content, 'image', 'content[image]');
$this->content = $content;
$this->content = json_encode($this->content, JSON_UNESCAPED_UNICODE);
return true;
}
return false;
}
}
Although it is not directly an answer to your question:
I'd suggest to use different attributes for the serialized value, say serializedContent. This way - content is always an array, and serializedContent is always a json string.
Then create a setter and a getter in your model:
public function setContent($value) {
$this->serializedContent = json_encode($value);
}
public function getContent() {
return json_decode($this->serializedContent,true);
}

issue with Yii 2 framework

I was trying to make a blog with YII2, my framework is confusing to call data from database.
For example when I call "username" from "user" table,
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'user.fullname', --->> Yii2 is thinking that this is a category and not a user table
'title',
'description',
'content:html',
'count_view',
'status',
'created_at',
],
]) ?>
I am getting this error: -->> unknown property: app\models\Category::fullname
please could you help me to solve this issue, where I did make a mistake?
and here is my post model contains:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "post".
*
* #property integer $id
* #property integer $user_id
* #property string $title
* #property string $description
* #property string $content
* #property integer $count_view
* #property string $status
* #property string $created_at
*
* #property User $user
* #property TagAssign[] $tagAssigns
*/
class Post extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'post';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_id', 'count_view','category_id'], 'integer'],
[['content', 'status'], 'string'],
[['created_at'], 'safe'],
[['count_view'], 'default','value'=>0],
[['user_id'], 'default','value'=>Yii::$app->user->id],
[['title', 'description'], 'string', 'max' => 255],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'title' => 'Title',
'description' => 'Description',
'content' => 'Content',
'category' => 'Category',
'count_view' => 'Count View',
'status' => 'Status',
'created_at' => 'Created At',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(Category::className(), ['id' => 'user_id']);
}
public function getCategory()
{
return $this->hasOne(User::className(), ['id' => 'category_id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getTagAssigns()
{
return $this->hasMany(TagAssign::className(), ['post_id' => 'id']);
}
}
and here user Model:
<?php
namespace app\models;
use Yii;
use yii\web\IdentityInterface;
/**
* This is the model class for table "user".
*
* #property integer $id
* #property string $username
* #property string $password
* #property string $fullname
* #property string $status
* #property string $role
* #property string $created_At
*
* #property Post[] $posts
*/
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'user';
}
public $current_password;
public $new_password;
public $confirm_password;
public $authKey;
/**
* #inheritdoc
*/
public function rules()
{
return [
[['status', 'role'], 'string'],
[['created_At'], 'safe'],
[['username', 'password', 'fullname'], 'string', 'max' => 45],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'username',
'password' => 'password',
'fullname' => 'fullname',
'status' => 'Status',
'role' => 'Role',
'created_At' => 'Created At',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getPosts()
{
return $this->hasMany(Post::className(), ['user_id' => 'id']);
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token,$type=null)
{
return static::findOne(['access_token'=>$token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey == $authKey;
}
public static function findByUsername($username)
{
return static::findOne(['username'=>$username]);
}
public function validatePassword($password)
{
if(Yii::$app->security->validatePassword($password,$this->password))
{
return true;
} else {
return false;
}
}
}
Change to this in Post model.
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'category_id']);
}
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'user_id']);
}

Make join from 2 tables in Active Data Provider

I have 2 tables:User and Posts. User can have many posts,post can't have many users. How to build relations in that models and how to make Join in ActiveDataProvider I have user_id in my Posts table and want to show data in my gridview like Posts(id,title,text) and User(name) how can I do that?I need to make relations in my model and how can I use it?;
Posts model:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "posts".
*
* #property integer $id
* #property integer $user_id
* #property string $post_title
* #property string $post_text
*/
class Posts extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'posts';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_id'], 'integer'],
[['post_title'], 'string', 'max' => 50],
[['post_text'], 'string', 'max' => 255],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'post_title' => 'Post Title',
'post_text' => 'Post Text',
];
}
public function insertPost()
{
$userId = \Yii::$app->user->identity->id;
$posts = new Posts();
$posts->user_id = $userId;
$posts->post_title = $this->post_title;
$posts->post_text = $this->post_text;
return $posts->save();
}
public function getUser()
{
return $this->hasOne(User::classname(),['user_id'=>'id']);
}
}
User model:
* #property integer $id
* #property string $email
* #property string $password
* #property string $name
*/
class User extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'user';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['email'], 'string', 'max' => 100],
[['password'], 'string', 'max' => 255],
[['name'], 'string', 'max' => 25],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'email' => 'Email',
'password' => 'Password',
'name' => 'Name',
];
}
public function setPassword($password)
{
$this->password = sha1($password);
}
public function validatePassword($password)
{
return $this->password === sha1($password);
}
public static function findIdentity($id)
{
return self::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
}
public function validateAuthKey($authKey)
{
}
public function getPost()
{
return $this->hasMany(Posts::classname(),['id'=>'user_id']);
}
}
You already have a relation (your function getPost ) in User model between User and Post
you can access the the value of Post eg:
$userModel = User::find()->where([ 'id' => $id])->one();
$myUserPost = $userModel->post;
$myUserPostAttribute = $userModel->post->attribute;
for ActiveDataProvider you can use
$dataProvider = User::find()->where([ 'id' => $id]);
and eventually add getter for single attribute in User Model eg:
getMyPostAttribute1()
{
return $this->post->attribute1
}
so you can easly use this getter in a gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
......
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'myPostAttribute1',
....

Yii2 AccessControl

I am new in Yii2 and I try to make AccessControl and success
but the problem is after I success for login and redirect to other page
my Identity _attributes always are null.So if I check with Yii::$app->user->isGuest the return value is always true
this is my LoginHandler.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginHandler extends Model
{
public $user_name;
public $user_password;
public $rememberMe = true;
private $_user;
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_name', 'user_password'], 'required'],
[['user_name', 'user_password'], 'string', 'max' => 100],
['user_password','authenticate'],
];
}
public function authenticate($attribute, $params){
// return true;
}
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}
protected function getUser()
{
if ($this->_user === null) {
$this->_user = User::findByUsername($this->user_name);
}
return $this->_user;
}
}
LoginController
<?php
namespace backend\controllers;
use Yii;
use app\models\user;
use app\models\LoginHandler;
class LoginController extends \yii\web\Controller
{
public function actionIndex()
{
return $this->render('index');
}
public function actionSignin(){
$user = User::findByUsername('admin');
$model = new LoginHandler();
if(Yii::$app->request->post()){
$data = Yii::$app->request->post();
$model->attributes = $data;
if ($model->login()) {
return $this->redirect(['/user/test']);
}else{
die('test');
}
}
return $this->render('login');
}
}
My User.php as model
namespace app\models;
use Yii;
/**
* This is the model class for table "user".
*
* #property integer $user_id
* #property string $user_name
* #property string $user_password
*/
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface{
public $id;
public $authKey;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'user';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_name', 'user_password'], 'required'],
[['user_name', 'user_password'], 'string', 'max' => 100]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => 'User ID',
'user_name' => 'User Name',
'user_password' => 'User Password',
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
public static function findByUsername($username){
return static::findOne(['user_name' => $username]);
}
}
and the last is my configuration main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'user' => [
'identityClass' => 'backend\models\User',
'loginUrl' => ['login/signin'],
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
Thanks in advance.
You mentioned AccessControl in your question. In Yii2 AccessControl is the special behavior class to manage access rules inside controller:
http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html
and I don't see AccessControl in you code.
Anyway.
Most probably the problem is in your implementation of User class.
Looking at your code I can imagine that the table structure is: user_id (PK), user_name, user_password.
If so, then the method getId() returns variable
($this->id) which is never initialized. But this method is used by Yii to store current user in session. In your case it should return $this->user_id.
And if you wish to make remember me working, you should implement correctly getAuthKey and validateAuthKey too.
Here is details:
http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
If this not helps, then show your table structure and code of view which pass authentication data to LoginController
It looks you should check for
Yii::$app->user->identity

Yii2 Search model, Invalid argument supplied for foreach

I am new at Yii framework and I am facing this problem that when i am creating search model to add filters and sorting, it's not working, i already have tried this solution Yii2 ActiveDataProvider - Invalid argument supplied for foreach() and even generated the new models and controller but the result is same, it did not work.
Please have a look at the code and kindly tell me what i am doing wrong.
Controller
namespace backend\controllers;
use Yii;
use backend\models\Contacts;
use backend\models\ContactsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* ContactsController implements the CRUD actions for Contacts model.
*/
class ContactsController extends Controller
{
...
/**
* Lists all Contacts models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new ContactsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
...
}
Model
namespace backend\models;
use Yii;
use backend\models\User;
/**
* This is the model class for table "user_contacts".
*
* #property string $id
* #property string $user_id
* #property string $contact_id
* #property string $created_on
* #property string $modified_on
*
* #property User $contact
* #property User $user
*/
class Contacts extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'user_contacts';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_id', 'contact_id'], 'integer'],
[['created_on', 'modified_on'], 'safe'],
[['user_id', 'contact_id'], 'unique', 'targetAttribute' => ['user_id', 'contact_id'], 'message' => 'The combination of User ID and Contact ID has already been taken.']
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'user_id' => Yii::t('app', 'User ID'),
'contact_id' => Yii::t('app', 'Contact ID'),
'created_on' => Yii::t('app', 'Created On'),
'modified_on' => Yii::t('app', 'Modified On')
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getContact()
{
return $this->hasOne(User::className(), ['id' => 'contact_id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
/**
* #inheritdoc
* #return ContactsSearch the active query used by this AR class.
*/
public static function find()
{
return new ContactsSearch(get_called_class());
}
}
Search model
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Contacts;
/**
* ContactsSearch represents the model behind the search form about `backend\models\Contacts`.
*/
class ContactsSearch extends Contacts
{
/**
* #inheritdoc
*/
public function rules()
{
return [
[['id', 'user_id', 'contact_id'], 'integer'],
[['created_on', 'modified_on'], 'safe']
];
}
/**
* #inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* #param array $params
*
* #return ActiveDataProvider
*/
public function search($params)
{
$query = Contacts::find();
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$this->load($params);
if(!$this->validate())
{
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'contact_id' => $this->contact_id,
'created_on' => $this->created_on,
'modified_on' => $this->modified_on
]);
return $dataProvider;
}
}
View(index.php)
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'user_id',
'contact_id',
'created_on',
'modified_on',
['class' => 'yii\grid\ActionColumn']
]
]);
Error
PHP Warning – yii\base\ErrorException
Invalid argument supplied for foreach()
1. in F:\Files\Web\PHP\wamp\www\Php\sites\expenses\vendor\yiisoft\yii2\BaseYii.php at line 517
public static function configure($object, $properties)
{
foreach ($properties as $name => $value) {
$object->$name = $value;
}
return $object;
}
2. in F:\Files\Web\PHP\wamp\www\Php\sites\expenses\backend\models\Contacts.php at line 76 – yii\base\Object::__construct()
public static function find()
{
return new ContactsSearch(get_called_class());
}
3. in F:\Files\Web\PHP\wamp\www\Php\sites\expenses\backend\models\ContactsSearch.php at line 43 – backend\models\Contacts::find()
public function search($params)
{
$query = Contacts::find();
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$this->load($params);
...

Categories