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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)