Php example of using reflection to insert objects _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php example
Php uses reflection to insert an object example for sharing. The copy code is as follows: *** insert insertModel (). reflection is used to reduce the efficiency. * @ paramclass $ model object * @ parambool $ is_returnLastInsertId: whether to return adding ID * @ return The code is as follows:


/**
* Insert insertModel (), which is less efficient by reflection.
* @ Param class $ model object
* @ Param bool $ is_returnLastInsertId whether to return the Add ID
* @ Return int: whether the return value is successful by default. if $ is_returnLastInsertId is true, the return value is "Add ID ".
*/
Public function insertModel ($ model, $ is_returnLastInsertId = FALSE ){
Try {
Require_once dirname (_ FILE _). '\ Models \ BaseModel. php ';
If (! Is_subclass_of ($ model, "BaseModel ")){
Exit ($ this-> getError (_ FUNCTION __, _ LINE __));
}
$ ClassName = get_class ($ model );
$ TName = $ this-> formatTabName ($ className );
$ ReflectionClass = new ReflectionClass ($ className );
$ Properties = $ reflectionClass-> getProperties ();
Unset ($ properties [0]);
$ Fields = "";
$ Vals = "";
Foreach ($ properties as $ property ){
$ PName = $ property-> getName ();
$ Fields. = $ pName .",";
$ Vals. = '\ ''. $ model-> $ pName .'\''.',';
}
$ Fields = rtrim ($ fields ,',');
$ Vals = rtrim ($ vals ,',');
$ This-> SQL = "insert into {$ tName} ({$ fields}) values ({$ vals })";
If ($ is_returnLastInsertId ){
$ This-> conn-> exec ($ this-> SQL );
$ LastId = (int) $ this-> conn-> lastInsertId ();

Return $ lastId;
} Else {
$ Row = $ this-> conn-> exec ($ this-> SQL );

Return $ row;
}
} Catch (Exception $ exc ){
Echo $ exc-> getMessage ();
}
}

The http://www.bkjia.com/PHPjc/740206.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/740206.htmlTechArticle code is as follows:/*** insert insertModel (), using reflection, slightly less efficient * @ param class $ model object * @ param bool $ is_returnLastInsertId whether to return adding ID * @ return...

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.