Fleaphp common functions and usage examples, fleaphp function examples

Source: Internet
Author: User
Tags function examples

Fleaphp common functions and usage examples, fleaphp function examples

This article describes the functions and usage of Fleaphp common functions. We will share this with you for your reference. The details are as follows:

1. FLEA_Db_TableDataGateway: update () usage:

For example, to modify a record with uid = 22, change the name field to "11", and change the pass field to "22", write as follows:

$data = array('uid'=>22,'name'=>11,'pass'=>22);$table->update($data);

2. FLEA_Db_TableDataGateway: updateByConditions () usage:

For example, update all records with level_ix = 3 and update the specific fields of these records (determined by $ row) to the specified values.

$ Row = array (field => Field Value Field => field value ); $ conditions = array ('level _ ix '=> 3); $ table-> updateByConditions ($ conditions, $ row );

3. FLEA_Db_TableDataGateway: updateField () Purpose: update the specified field of the record and return the total number of updated records

For example, to search for conditions that meet $ conditions, modify the value of the field class_id to $ targetId.

$sourceId = $_POST['source']; $targetId = $_POST['target']; $conditions = array('class_id' => $sourceId);$table->updateField($conditions,'class_id',$targetId);

4. FLEA_Db_TableDataGateway: updateRowset () Purpose: Update record set (multi-row record)

The usage is similar to update (), except that multiple records are modified:

For example:

$data =array(array('id'=>'2','name'=>'111','job'=>'111'),array('id'=>'3','name'=>'222','job'=>'222'));$arr=$this->_test->updateRowset($data);

I think you can understand it...

Note: $ data must be two-dimensional, that is, you must use updateRowset () to modify a record as follows:

$data=array(array('id'=>'2','name'=>'111','job'=>'111'));

Is it clear? Haha

5. FLEA_Db_TableDataGateway: create () Purpose: Insert a new record and return the primary key value of the new record.

For example:

$data = array(array('uid'=>22,'name'=>11,'pass'=>22),array('uid'=>23,'name'=>12,'pass'=>23));$table->create($data);

6. FLEA_Db_TableDataGateway: createRowset () inserts multiple rows of records and returns an array containing the primary key values of all new records.

For example:

$data = array(array('uid'=>22,'name'=>11,'pass'=>22),array('uid'=>23,'name'=>12,'pass'=>23));$table->createRowset($data);

7. FLEA_Db_TableDataGateway: remove () to delete a record. The condition must be a primary key.

For example:

remove(array("id"=>"2"));

8. FLEA_Db_TableDataGateway: removeByConditions (). Check the name and meaning. Of course, delete the qualified records.

Under normal circumstances, the conditions for removing () can be met. If you delete a table with multiple primary keys:

Conditions = array ('Primary key 1' => xxx, 'Primary key 2' => yyy, 'Primary key 3' => zzz,) $ table-> removeByConditions ($ conditions );

Note: If a table has multiple primary keys, the $ primaryKey in the corresponding Model can only be set to one of the most common primary keys, but not an array.

9. & FLEA_Db_TableDataGateway: findBySql () Purpose: Obtain records directly using SQL statements

For example:

$arr=$this->_test->findBySql('SELECT * FROM newtable');

10. FLEA_Db_TableDataGateway: decrField () Purpose: reduce the value of a specified field in a qualified record and return the total number of updated records (this operation will not trigger any event or process associated data ).

For example:

$arr=$this->_test->decrField(array('id'=>'3'),'prize',$decr = 2);

Note: The default value of $ decr is 1, and the number 2 is changed by yourself. Of course, you can change it to 34568. If you change it to a few, you can reduce it by a few...

11. FLEA_Rbac_UsersManager: updatePasswordById () Purpose: directly update the password

For example, set the password with ID 1 to 00000.

$arr=$this->_student->updatePasswordById ('1','000000');

Note: The database must contain a field called Password. The modified Password is encrypted.

12. FLEA_Rbac_UsersManager: checkPassword () Purpose: Check whether the plaintext and ciphertext of the Password Match

For example:

$user = $usersManager->findByUsername('andy');$usersManager->checkPassword('000000', $user[$usersManager->passwordField]))

13. FLEA_Rbac_UsersManager: encodePassword () Purpose: Convert plaintext to ciphertext

For example:

$user = $this->_student->findByUsername('andy');$arr=$this->_student->encodePassword($user[$this->_student->passwordField]);$this->_student->updatePassword($user[username],$arr);

Note: The premise is that the database must have a field called Password;

14. FLEA_Rbac_UsersManager: updatePasswordById () Purpose: directly update the password

I don't want to talk about it anymore. I think you will understand it when you look at it for example 11.

15. FLEA_Db_TableDataGateway: updateByConditions () Purpose: update the qualified records. The total number of records that are successfully updated is returned.

For example:

$condition=array('id'=>2);$row=array('name'=>'nicholas');$this->_test->updateByConditions($condition,$row);

16. FLEA_Db_TableDataGateway: updateField () Purpose: update the specified field of the record and return the total number of updated records. This operation will not trigger any events or process associated data.

For example, if you change the record id to 2 and change the value of field name to vin, you need to write it like this:

$condition=array('id'=>2);$this->_test->updateField($condition,'name','vin');

17. FLEA_Db_TableDataGateway: incrField () Purpose: add the value of the specified field of the qualified record, and return the total number of updated records

For example, let's not talk about this. Let's take a look at Example 10. But note that example 10 is a subtraction, and this is a addition...

18. FLEA_Db_TableDataGateway: replaceRowset () Purpose: Replace the record set (multi-row data), return the primary key field value of the record set, and return false if the record set fails.

$condition=array(array('id'=>2,'name'=>nicholas,'job'=>good));$this->_test->replaceRowset($condition);

Note:

① Assume that the table contains id, name, job, prize, etc. If the prize field is not written in $ condition, null is inserted by default, and the original data is cleared. If you do not pay attention to it, data may be lost.
② $ Condition must be two-dimensional

19. FLEA_Db_TableDataGateway: removeAll () Purpose: delete all records with caution.

For example:

$this->_test->removeAll ();

20. FLEA_Db_TableDataGateway: removeAllWithLinks () Purpose: delete all records and associated data

Note: this should be used with greater caution. All table data associated with this table will be deleted. What is association? That is to say, A field in this table may be a foreign key in another table, which is associated.

For example:

In the MODEL, a table named com is associated with a table named student. uid in the com table is the foreign key in the student table, so we can say that these two tables are associated, is associated with the following method:

class Model_com extends FLEA_Db_TableDataGateway{var $tableName = 'newtable';var $primaryKey = 'uid';var $hasOne=array('tableClass' => 'Model_student','foreignKey' => 'uid','mappingName'=>'jobs');}

Then, we execute the following statement:

$this->_test =& FLEA::getSingleton('Model_com'); $this->_test->removeAllWithLinks();

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.