YII implementation model Add a default value method (2 methods) _php instance

Source: Internet
Author: User

The example of this article describes the method of adding the default value of YII implementation model. Share to everyone for your reference, specific as follows:

Yii model inherits from Cactiverecord

Some fields may not appear in the form, but need to be added to the program . such as order number, time stamp, Operation user_id and so on.

Here are two ways to:

1, in the Rules () method set:

Public function rules ()
{
  //note:you should only define the rules for those attributes that
  //would receive user Inputs.
  Return Array (
    ' Start, end ', ' required '),
    array (' user_id ', ' numerical ', ' integeronly ' =>true),
    Array (' timestamp ', ' Default ', ' Value ' =>date (' y-m-d h:i:s ')),
    //The following rule are used by search ().
    Please remove the those attributes that should is searched.
    Array (' ID, start, end, user_id, timestamp ', ' safe ', ' on ' => ' search ')
  ;
}

2, in the BeforeSave () method set:

function BeforeSave ()
{
  $this->user_id = Yii::app ()->user->id;
  return true;
}

It should be noted that theBeforeSave () method requires return true, otherwise it will not be saved .

I hope this article will help you with the PHP program design based on the YII framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.