tklustig
(Kipp Thomas)
September 11, 2017, 11:15pm
1
Hi guys,
I created blameable values im model with following options:
Blameable Behaviors
Created By:angelegt_von
Updated By:aktualisiert_von
Value:\Yii::$app->user->id
This works fine by now, but I need value like this
\Yii::$app->person->id.
Instead of automatically fill the specified attributes with the current user ID,it should be filled with person ID
person is another class(model) in my application,created by gii.
However,following code will throw out error:Getting unknown property: yii\web\Application::person
public function behaviors() {
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'angelegt_am',
'updatedAtAttribute' => 'aktualisiert_am',
'value' => new \yii\db\Expression('NOW()'),
],
'blameable' => [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'angelegt_von',
'updatedByAttribute' => 'aktualisiert_von',
'value' => \Yii::$app->person->id,
],
'uuid' => [
'class' => UUIDBehavior::className(),
'column' => 'id',
],
];
}
Any suggestions how to achieve my wish to?
jacmoe
(Jacob Moena)
September 11, 2017, 11:30pm
2
If you want the ‘person.id’, then use this:
Yii::$app->user->identity->id
jacmoe
(Jacob Moena)
September 11, 2017, 11:32pm
3
You are probably confused because ‘user’ is not referring to the User Model, but the WebUser component.
The ‘identity’ property of ‘user’ is referring to your user model
Yes, it is confusing.
tklustig
(Kipp Thomas)
September 12, 2017, 8:16am
4
Sorry, but this is nonsense.
Following output will be given using this
echo "output:".Yii::$app->user->identity->username;
output:admin
This is not want I want,'cause it refers to User, not to person.person should be something like “Schwarz,Beate”
Yii::$app->person->identity->id
Will throw out error as uppon:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: yii\web\Application::person
Further ideas how to fix this?
jacmoe
(Jacob Moena)
September 12, 2017, 10:12am
5
Read the goddamn guide instead of accusing me of nonsense!
[i]
Yii::$app->user->identity [/i]refers to the user model for the currently logged in web user.
Use that!
And do try and read the Yii2 source code - it definitely will make you more confident
softark
(Softark)
September 12, 2017, 11:33am
6
Please read the following section of the guide:
Authentication (http://www.yiiframework.com/doc-2.0/guide-security-authentication.html )
What is your identity class? Is it app\models\Person ? Probably no, I guess. Is it app\models\User ?
You’ve created Person class using Gii, that’s OK. But it doesn’t mean you can access it as an application component like the following:
app\models\Person $person = Yii::$app->person; // error!!
What you can do is accessing the identity class object:
app\models\User $user = Yii::$app->user->identity;
The only way you can connect Person to the currently logged-in user is making it the identity class (or the relation of it) in the authentication framework of Yii.
app\models\Person $person = Yii::$app->user->identity;
// Or if Person is a relation of User, then
// app\models\User $user = Yii::$app->user->identity;
// app\models\Person $person = $user->person;
echo $person->name;
tklustig
(Kipp Thomas)
September 12, 2017, 11:46am
7
jacmoe:
Read the goddamn guide instead of accusing me of nonsense!
[i]
Yii::$app->user->identity [/i]refers to the user model for the currently logged in web user.
Use that!
And do try and read the Yii2 source code - it definitely will make you more confident
All I have to do is to program another own BehaviourClass, for instance like this:
<?php
namespace common\wsl_components;
use yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
class UppercaseBehavior extends Behavior {
public function events() {
return [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
];
}
public function beforeValidate($event) {
//diese beiden Abfragen sind äquivalent
$model_0 = \frontend\models\Person::find()->where(['bew_id' => Yii::$app->user->identity->id])->one();
$model_1 = \frontend\models\Person::findOne(['bew_id' => Yii::$app->user->identity->id]);
var_dump($model_0);
var_dump($model_1);
$user = (new \yii\db\Query())
->select(['id_person_mitarbeiter'])
->from('mitarbeiter')
->where(['id_benutzer' => Yii::$app->user->identity->id])
->one();
var_dump($user);
}
}