filthy
(Stephane Belanger174)
1
DB tables
person (id, name, user_id) <-- fk to user table.
user (id, name)
comment (id, comment, project_id, person_id) <–fk to person table.
on login, id (from user) sets Yii::app()->user->id
User can comment on a project.
What i want to do, in pseudo code:
On save comment,
get person.id where person.user_id = Yii::app()user->id
set comment.person_id = person.id
Want to do this in the model and call when needed.
dniznick
(Dniznick)
2
Don’t overthink it!
... //controller code dealing with $_POST stuff
if($model->validate()) //comment model presumably
$user=User::model()->findByPk(Yii::app()->user->id);
$model->person_id=$user->person->id //assuming 'person' relations and HAS_ONE relationship. Better try/catch exceptions here though.
... //do other stuff
filthy
(Stephane Belanger174)
3
Ended up creating this since i could not make the solution above work
public function beforeSave()
{
if($this->isNewRecord)
{
$this->user_created=Yii::app()->user->name;
$this->tenant_id = Yii::app()->user->tenantId;
$personId = Yii::app()->db->createCommand()
->select('id')
->from('person')
->where('user_id =' .Yii::App()->user->id)
->queryScalar();
$this->person_id = $personId;
}
else