doulongsi1831 2012-08-06 15:19
浏览 63
已采纳

SugarCRM将数据库数据保存到文件中

Is there any way I can save the data of a specific table of the sugarcrm database into a doc file ?

I have a custom module with username,some notes and date. I want to write this into the database and into a file as well.

Its not just a php file. I want to use logic hooks and write the code. I want to use the logic hooks to access database and then write the data into the file.

Thanks in advance

  • 写回答

2条回答 默认 最新

  • doumeng3345 2012-08-06 16:01
    关注

    Saving as a DOC file probably isn't the best idea, since it is primarily used for formatting information. A standard .txt file is usually what you would use for such a process.

    With that said, there isn't any methods built into sugar that will let you do this. You will need to build the capability into the module.

    What exactly are you attempting to accomplish? There is a very powerful auditing tool set, which is good for seeing revisions to a module object. If you are just wanting to monitor changes to the table, you can setup logging for that table/database inside of SQL.

    +++Ok, if you are just looking to write to a file after saves, follow the instructions at: http://cheleguanaco.blogspot.com/2009/06/simple-sugarcrm-logic-hook-example.html for a quick how-to on getting the logic hooks working. You are going to want to make a php file that simply uses the data passed to it via the bean class, and either writes to the file directly from the data within bean, or uses the bean->id parameter to do a SQL query and write to the file from that data.

    Also, is this a DOC file that is going to be immediately generated and then destroyed at the end of the transaction? Or is it more of a log file that will be persistent?

    ++++That is simple enough then Where you have the Query right now, replace it with:

    $file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
    $query = "SELECT * FROM data_base.table";
    $result = $bean->db->query($query,true);
    $dbRowData = $bean->db->mysql_fetch_assoc($result);
    $printedArray = print_r($dbRowData);     
    fwrite($file, $printedArray) or die('Could not write to file.');
    fclose($file);
    

    *A quick note, you might need to set permissions in order to be able to read/write to the file, but those are specific to the machine type, so if you encounter errors with either do a search for setting permissions for your particular server type.

    **Also, 'SELECT * FROM database.table' is going to return ALL of the rows in the entire table. This will generate a very large file, and be a performance hindrance as the table grows. You should use the bean class to update the last saved tuple:

    $file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
    $query = "SELECT * FROM data_base.table WHERE id = '".$focus->id."';";
    $result = $bean->db->query($query,true);
    $dbRowData = $bean->db->mysql_fetch_assoc($result);
    $printedArray = print_r($dbRowData);     
    fwrite($file, $printedArray) or die('Could not write to file.');
    fclose($file);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C