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();

  }

  }

?>

Parents Reply Children
  • 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();

      }

      }

    ?>