Help! Using Beans from command line script?

Trying to write a utility to associate imported contacts with imported accounts (using alternate account id from import. I'm getting 'Class 'Accounts' not found'  what do I need to add to the script to get it to work?

<?php

if(!defined('sugarEntry'))define('sugarEntry', true);

require 'data/BeanFactory.php';

require_once("modules/Accounts/Account.php");

$pdo = new PDO($dsn, $user, $pass, $opt);

$stmt = $pdo->query('SELECT bridge_account_id_c,id_c FROM accounts_cstm');

while ($account_row = $stmt->fetch())

  {

  echo $account_row['id_c']."\n";

  $stmt2 = $pdo->query("SELECT id_c FROM contacts_cstm WHERE bridge_account_id_c = '".$account_row['bridge_account_id_c']."'");

  while ($contact_row = $stmt2->fetch())

  {

  $bean = new Accounts();

  $bean->retrieve($account_row['id_c']);

  $bean->load_relationship('contacts');

  $bean->contacts->add($contact_row['id_c']);

  $bean->save();

  }

  }

?>

  • Hello,

    As the error suggests, the correct class  name is "Account", or alternatively you can use,

    BeanFactory::newBean('Accounts');

    Hope this helps.

  • That's a start. I wish it were that easy.  I think I'm in pseudo no-man's land here because I'm doing this in the context of running the PHP script from the command line.  It's just a one-time run utility so it doesn't need to actually be part of the application.  But I'm finding that the hierarchy of include files is rather complex and I've yet to determine what includes and what functions I need to call to get the script to the point it can sucessfully use the beans!  Here's an update to the script.  At this point it's failing at trying to setup the getTypeInstance function in the DBManagerFactory.

    PHP Notice:  Undefined index: log in /var/www/GEO/gamechanger/include/database/DBManagerFactory.php on line 96

    PHP Fatal error:  Call to a member function fatal() on null in /var/www/GEO/gamechanger/include/database/DBManagerFactory.php on line 96

    <?php

    if(!defined('sugarEntry'))define('sugarEntry', true);

    //require_once('../../include/entryPoint.php');

    require_once ("data/BeanFactory.php");

    require_once("modules/Accounts/Account.php");

    require_once("include/database/DBManagerFactory.php");

    require_once("include/TimeDate.php");

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";

    $opt = [

        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,

        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,

        PDO::ATTR_EMULATE_PREPARES   => false,

    ];

    $pdo = new PDO($dsn, $user, $pass, $opt);

    $stmt = $pdo->query('SELECT bridge_account_id_c,id_c FROM accounts_cstm');

    while ($account_row = $stmt->fetch())

      {

      echo $account_row['id_c']."\n";

      $stmt2 = $pdo->query("SELECT id_c FROM contacts_cstm WHERE bridge_account_id_c = '".$account_row['bridge_account_id_c']."'");

      while ($contact_row = $stmt2->fetch())

      {

      $bean = new Account();

      $bean->retrieve($account_row['id_c']);

      $bean->load_relationship('contacts');

      $bean->contacts->add($contact_row['id_c']);

      $bean->save();

      }

      }

    ?>

  • Hello Jeff,

    <?php

    define('sugarEntry', true); // I did not create an entry point that is why I defined sugarEntry true.

    require_once('include/entryPoint.php');

    global $db; // Use this instead of creating new connection

    $rs = $db->query('SELECT id FROM users');

    while($arr = $db->fetchByAssoc($rs)){

            print $arr['id']."<br />";

    }

    This has worked for me. Hope this helps.

  • No dice.  Fatal error: Call to a member function query() on a non-object

    But I finally found another thread with a similar discussion and they had:

    $db = DBManagerFactory::getInstance();

    That did the trick.  I think global $db only works if you are running through index.php via web server, not Linux PHP command line execution.

    They also had:

    require_once('include/SugarObjects/SugarConfig.php');

    I haven't tested if this is required but it's in my script now too.