现在的位置: 首页 > 算法 > 正文

Fleaphp常见函数功能与用法

2020年02月18日 算法 ⁄ 共 4397字 ⁄ 字号 评论关闭

  本文实例讲述了Fleaphp常见函数功能与用法。分享给大家供大家参考,具体如下。

  Fleaphp常见函数功能与用法

  1. FLEA_Db_TableDataGateway::update()用法:

  例如:修改一条 uid=22 的记录, name字段改成"11", pass字段改成"22",就要这样写:

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

  $table->update($data);

  2. FLEA_Db_TableDataGateway::updateByConditions()用法:

  例如:更新所有 level_ix = 3 的记录,并且把这些记录的特定字段(由 $row 确定)都更新为指定的值。

  $row = array(

  字段 => 字段值

  字段 => 字段值

  字段 => 字段值

  字段 => 字段值

  );

  $conditions = array('level_ix' => 3);

  $table->updateByConditions($conditions, $row);

  3. FLEA_Db_TableDataGateway::updateField()用途:更新记录的指定字段,返回更新的记录总数

  例如:以查找满足$conditions的条件,修改其中字段为class_id的值为$targetId。

  $sourceId = $_POST['source'];

  $targetId = $_POST['target'];

  $conditions = array('class_id' => $sourceId);

  $table->updateField($conditions,'class_id',$targetId);

  4. FLEA_Db_TableDataGateway::updateRowset ()用途:更新记录集(多行记录)

  用法和update ()类似,只不过是修改多条记录:

  例如:

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

  $arr=$this->_test->updateRowset($data);

  想必大家能看明白吧...呵呵。。。

  一定注意:$data 一定要是二维的,即使用updateRowset ()修改一条记录也要这样写:

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

  说得清不清楚啊?呵呵

  5. FLEA_Db_TableDataGateway::create ()用途:插入一条新记录,返回新记录的主键值

  例如:

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

  $table->create($data);

  6. FLEA_Db_TableDataGateway::createRowset()插入多行记录,返回包含所有新记录主键值的数组

  例如:

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

  $table->createRowset($data);

  7. FLEA_Db_TableDataGateway::remove () 删除一条记录,条件必须为主键

  例如:

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

  8. FLEA_Db_TableDataGateway::removeByConditions ()看名知义,当然是删除符合条件的记录喽

  正常情况下和remove()的条件可以通和,如果对有多个主键的表进行删除操作:

  conditions = array(

  '主键1' => xxx,

  '主键2' => yyy,

  '主键3' => zzz,

  )

  $table->removeByConditions($conditions);

  另注意一点:如果某个表有多个主键的话,那么它所对应的Model中的 $primaryKey 只能设置为一个最常用的主键,不能设置为一个数组

  9. & FLEA_Db_TableDataGateway::findBySql ()用途:直接使用 sql 语句获取记录

  例如:

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

  10. FLEA_Db_TableDataGateway::decrField ()用途:减小符合条件的记录的指定字段的值,返回更新的记录总数 (该操作不会引发任何事件,也不会处理关联数据)。

  例如:

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

  注意:$decr默认值为1,数字2是本人自己改的,当然你也可以改为34568了,改几就减几,明白了吧。。。

  11. FLEA_Rbac_UsersManager::updatePasswordById ()用途:直接更新密码

  例如:把ID为1的密码设为00000

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

  注意:前提是数据库中一定要有叫做Password的字段;修改后的密码是加密的。

  12. FLEA_Rbac_UsersManager::checkPassword ()用途:检查密码的明文和密文是否符合

  例如:

  $user = $usersManager->findByUsername('andy');

  $usersManager->checkPassword('000000', $user[$usersManager->passwordField]))

  13. FLEA_Rbac_UsersManager::encodePassword ()用途:将密码明文转换为密文

  例如:

  $user = $this->_student->findByUsername('andy');

  $arr=$this->_student->encodePassword($user[$this->_student->passwordField]);

  $this->_student->updatePassword($user[username],$arr);

  注意:前提是数据库中一定要有叫做Password的字段;

  14. FLEA_Rbac_UsersManager::updatePasswordById ()用途:直接更新密码

  这个我不说了啊,我想聪明的你一看例11就会明白了

  15. FLEA_Db_TableDataGateway::updateByConditions ()用途:更新符合条件的记录,成功返回更新的记录总数

  例如:

  $condition=array('id'=>2);

  $row=array('name'=>'nicholas');

  $this->_test->updateByConditions($condition,$row);

  16. FLEA_Db_TableDataGateway::updateField () 用途:更新记录的指定字段,返回更新的记录总数 该操作不会引发任何事件,也不会处理关联数据。

  例如:修改id为2的记录,把字段为name的值修改为vin就要这么写:

  $condition=array('id'=>2);

  $this->_test->updateField($condition,'name','vin');

  17. FLEA_Db_TableDataGateway::incrField () 用途:增加符合条件的记录的指定字段的值,返回更新的记录总数

  例如:这个也不说,去看例10吧,但要注意,例10是减,这个是加,嘿嘿。。。

  18. FLEA_Db_TableDataGateway::replaceRowset () 用途:替换记录集(多行数据),返回记录集的主键字段值,失败返回 false

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

  $this->_test->replaceRowset($condition);

  注意:

  ① 假设表中有id,name,job,prize等,如果在$condition中没写prize字段,会就默认插入空,原有的数据会被清除,如不注意,也许会丢失数据

  ② $condition一定是二维的

  19. FLEA_Db_TableDataGateway::removeAll ()用途:删除所有记录,用时要谨慎

  例如:

  $this->_test->removeAll ();

  20. FLEA_Db_TableDataGateway::removeAllWithLinks ()用途:删除所有记录及关联的数据

  注意:这个更加要慎用,所有与这个表有关联的表数据都将被删除,何谓有关联,也就是说,此表中的某个字段可能是另外一个表中的外键,此谓之有关联。

  例如:

  在MODEL中一个叫做com的表关联了一个叫做student的表,com表中的uid是student表中的外键,那么我们就说这两个表关联起来了,是用下面的方法关联起来的

  class Model_com extends FLEA_Db_TableDataGateway

  {

  var $tableName = 'newtable';

  var $primaryKey = 'uid';

  var $hasOne=array('tableClass' => 'Model_student',

  'foreignKey' => 'uid',

  'mappingName'=>'jobs');

  }

  这时,我们执行下面的语句:

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

  $this->_test->removeAllWithLinks();

  以上就是本次给大家分享的关于sql的全部知识点内容总结,大家还可以在下方相关文章里找到相关文章进一步学习,感谢大家的阅读和支持。

抱歉!评论已关闭.