Query not return any result in logic hook but query work in MySQL workbench

My code in Cases module hook:

$sql = "select c.id from cases c
        left join emails e on e.parent_id = c.id and e.parent_type = 'Cases'
        left join emails_text et on et.email_id = e.id
        where c.id = '{$focus->id}' and et.to_addrs like '%_string_%'";
$result = $focus->db->query($sql,true);
$check =  $focus->db->fetchByAssoc($result);
if(!empty($check)){
     $GLOBALS['log']->fatal("Hello World ".$check);
}

I search solution in google many people suggest to using SugarQuery but it not more complex for my query or I do wrong with my code?

Please suggest me for solution.

Sugar 7.6 Ent

Thanks,

M

Parents Reply
  • I always do queries to the database like this and they work properly:


    <?php

    global $sugar_config;

    $server = $sugar_config['dbconfig']['db_host_name'];

    $user = $sugar_config['dbconfig']['db_user_name'];

    $password = $sugar_config['dbconfig']['db_password'];

    $db = $sugar_config['dbconfig']['db_name'];

    $db_handle = mysqli_connect( $server, $user, $password, $db );

    if( !$db_handle ) {

       echo "Connection to DB failure";

       $GLOBALS['log']->fatal('Connection to DB failure');

    }

    else {

         //Here your custom code

         $sql = 'SELECT ...'; //Your SQL Query

         $res = mysqli_query( $db_handle, $sql );

         if( $res ) {

           $GLOBALS['log']->fatal('Query successful');

                

              //Loop the result

              while( $row = mysqli_fetch_array( $res ) ) {

                   //...

              }

         }

         else {

            $GLOBALS['log']->fatal('Query failed');

         }

    }

    ?>

    Hope that helps.

    David.

Children
  • Thanks,

    But I have already one I try with another method by using CURL and return JSON then I decode JSON into value.

    <?php

    include('../config.php');

    //JSON function...

    mysql_connect($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password']) or die(mysql_error());

    mysql_select_db($sugar_config['dbconfig']['db_name']) or die(mysql_error());

    mysql_query("SET NAMES UTF8");

    unset($sql);

                            $sql = "SELECT count(*) as 'cc' FROM cases c ".

                                    "LEFT JOIN emails e ON e.parent_id = c.id and e.parent_type = 'Cases' ".

                                    "LEFT JOIN emails_text et ON et.email_id = e.id ".

                                    "WHERE c.id = '{$id}' AND et.to_addrs LIKE '%_emailaddr_%' ";

            $result = mysql_query($sql);

                    while($row = mysql_fetch_array($result)){

                    $Json->add('cc',$row['cc']);

            }

    When I run It's work perfect but when I put in logic hook not work.

    copy CURL url and run return 1 but when using logic hook return 0 or It might not any data.

  • Now I know why no data return because cases still on creating and when I query in before save no data return case and email not create relation. So easier solution for me is modified core instance of Sugar, bad solution but work in InboundeEmail create case.