Yii Framework Forum: Onchange Field - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Onchange Field How can i track changes of filed? Rate Topic: -----

#1 User is offline   UASergey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-June 13

Posted 17 September 2013 - 04:58 AM

Hello!

I have a field user_name. If i am changing the field then i have to change field "user_log" and append some text.

user_log will be look like:

Quote

user have changed name: newname;
user have changed name: newname1;
user have changed name: newnewname;


How can i track changes of user_name?
0

#2 User is offline   NEW_NAME 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 17-September 13

Posted 17 September 2013 - 05:08 AM

use beforeSave() method in your model
0

#3 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,317
  • Joined: 21-February 11
  • Location:Greece

Posted 17 September 2013 - 07:23 AM

Hi

ovverides beforeSave or afterSave method
to do that check this
http://www.yiiframew.../en/post.create
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#4 User is offline   UASergey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-June 13

Posted 17 September 2013 - 12:37 PM

View PostKonApaz, on 17 September 2013 - 07:23 AM, said:

Hi

ovverides beforeSave or afterSave method
to do that check this
http://www.yiiframew.../en/post.create



How will i know about changed field?
0

#5 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,317
  • Joined: 21-February 11
  • Location:Greece

Posted 17 September 2013 - 06:38 PM

View PostUASergey, on 17 September 2013 - 12:37 PM, said:

How will i know about changed field?


You could save the attribute to another variable before changed

private $old_attr1;
public function afterFind()
{
    $this->old_attr1 = $this->attr1;
}

protected function afterSave()
{
    parent::afterSave();
    if ($this->old_attr1 !== $this->attr1) { //what you want to do }
}

Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
1

#6 User is offline   UASergey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-June 13

Posted 19 September 2013 - 05:03 PM

View PostKonApaz, on 17 September 2013 - 06:38 PM, said:

You could save the attribute to another variable before changed

private $old_attr1;
public function afterFind()
{
    $this->old_attr1 = $this->attr1;
}

protected function afterSave()
{
    parent::afterSave();
    if ($this->old_attr1 !== $this->attr1) { //what you want to do }
}



Can I change $this->attr1?


I failed to do this:
protected function afterSave()
{
    parent::afterSave();
    if ($this->old_attr1 !== $this->attr1) { $this->attr1=$this->attr1+' text'; }
}

0

#7 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,317
  • Joined: 21-February 11
  • Location:Greece

Posted 20 September 2013 - 12:58 AM

View PostUASergey, on 19 September 2013 - 05:03 PM, said:

Can I change $this->attr1?


I failed to do this:
protected function afterSave()
{
    parent::afterSave();
    if ($this->old_attr1 !== $this->attr1) { $this->attr1=$this->attr1+' text'; }
}



Hi

If you want just log the changes then

protected function afterSave()
{
    parent::afterSave();
    if ($this->old_attr1 !== $this->attr1) { $this->attr1 = $this->attr1 . ' text'; //or $this->attr1 = $this->old_attr1 . ' text';  //depending what you want to do... }
//some code to log
}



but if you want to do something to save in your database you should use beforeSave
protected function beforeSave()
{
    if(parent::beforeSave())
    {
        $this->attr1 = $this->attr1 . ' text'; //or $this->attr1 = $this->old_attr1 . ' text';  //depending what you want to do 
//some code to log...
        return true;
    }
    else
        return false;
}


Thanks for voting :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#8 User is offline   UASergey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-June 13

Posted 20 September 2013 - 04:24 AM

View PostKonApaz, on 20 September 2013 - 12:58 AM, said:

but if you want to do something to save in your database you should use beforeSave
protected function beforeSave()
{
    if(parent::beforeSave())
    {
        $this->attr1 = $this->attr1 . ' text'; //or $this->attr1 = $this->old_attr1 . ' text';  //depending what you want to do 
//some code to log...
        return true;
    }
    else
        return false;
}




But it does not work. Example:

Model:
class Product extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return 'product';
	}


    public function beforeSave()
    {

        if(parent::beforeSave())
        {
            $this->p_name = 'another value';
            return true;
        }
        else
            return false;

    }
}


here is code which call the model
Product::model()->updateByPk(1,array('p_name'=>'This Value'));


But database saving value "This Value". Why if I tried to change it to "another value"?
What do I wrong?
0

#9 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 881
  • Joined: 19-February 13
  • Location:India

Posted 20 September 2013 - 04:33 AM

View PostUASergey, on 20 September 2013 - 04:24 AM, said:

But it does not work. Example:

Model:
class Product extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return 'product';
	}


    public function beforeSave()
    {

        if(parent::beforeSave())
        {
            $this->p_name = 'another value';
            return true;
        }
        else
            return false;

    }
}


here is code which call the model
Product::model()->updateByPk(1,array('p_name'=>'This Value'));


But database saving value "This Value". Why if I try to change it to "another value"?
What do I wrong?



update query
Thanks,
Ankit Modi
Skype : amodi06
0

#10 User is offline   UASergey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-June 13

Posted 20 September 2013 - 04:49 AM

View PostMaggie Q(Ankit Modi), on 20 September 2013 - 04:33 AM, said:



I do just the same
0

#11 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,317
  • Joined: 21-February 11
  • Location:Greece

Posted 20 September 2013 - 08:42 AM

View PostUASergey, on 20 September 2013 - 04:24 AM, said:

But it does not work. Example:

Model:
class Product extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return 'product';
	}


    public function beforeSave()
    {

        if(parent::beforeSave())
        {
            $this->p_name = 'another value';
            return true;
        }
        else
            return false;

    }
}


here is code which call the model
Product::model()->updateByPk(1,array('p_name'=>'This Value'));


But database saving value "This Value". Why if I tried to change it to "another value"?
What do I wrong?


updateByPk is just a helper method and does not triggered beforeSave and afterSave methods like update method does, so use update method
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users