brightecapital/salesforce-orm


README

Software license Version Download Build status Coverage

配置

composer.json

"require": {
        "brightecapital/salesforce-orm": "^1.4.2"
    }

运行

composer require brightecapital/salesforce-orm

示例代码

实体管理器

$config = [
            'clientId' => 'yourSalesforceClientId'
            'clientSecret' => "yourSalesforceSecret"
            'path' => 'yourSalesforcePath'
            'username' => 'yourSalesforceUsername'
            'password' => 'yourSalesforcePassword'
            'apiVersion' => 'yourSalesforceApiVersion'
          ];
$conn = new \Salesforce\Client\Connection($config);          
$entityManager = new \Salesforce\ORM\EntityManager($conn);

仓库

/* @var \Salesforce\ORM\Repository */
$accountRepository = $entityManager->getRepository(Account::class);
class AccountRepository extends Repository
{
    protected $className = Account::class;
}

$accountRepository = new AccountRepository($entityManager);

对象

/* @var Account $account */
$account = $entityManager->getRepository(Account::class)->find('0010p000002Wam9AAC');
$account = $accountRepository->find('0010p000002Wam9AAC');
$account = new Account();

创建|更新对象

$account = new Account();
$account->setName('Your Name);
$account->setWebsite('YourWebsite);
$accountRepository->save($account); // this will create a new Account entity

$account = $entityManager->getRepository(Account::class)->find('0010p000002Wam9AAC');
$account->setWebsite('YourWebsite);
$account->setName('YourName');
$accountRepository->save($account); // this will update a the current Account entity

实体和字段

/**
 * Salesforce Account
 *
 * @package Salesforce\Entity
 * @SF\Object(name="Account")
 */
class Account extends Entity
{
    /**
     * @var string
     * @SF\Field(name="Name", required=true)
     */
    protected $name;

    /**
     * @var string
     * @SF\Field(name="Website")
     */
    protected $website;
 }
  • @SF\Object(name="Account"): 指示此类映射到 Salesforce Account 对象
  • @SF\Field(name="Name") : 指示属性映射到 Salesforce Account 对象中的 'Name' 字段

关系,必需,验证

/**
 * Salesforce Account
 *
 * @package Salesforce\Entity
 * @SF\Object(name="Account")
 */
class Account extends Entity
{
    /**
     * @var string
     * @SF\Field(name="Name", required=true)
     */
    protected $name;

    /**
     * @var string
     * @SF\Field(name="Website")
     * @SF\Url(value=true)
     */
    protected $website;
    
    /**
      * @var array
      * @SF\OneToMany(name="Contacts", targetClass="App\Domain\Marketing\Salesforce\Entity\Contact", field="Id", targetField="AccountId", lazy=false)
      */
     protected $contacts;

 }
  • @SF\OneToMany(name="Contacts", targetClass="App\Domain\Marketing\Salesforce\Entity\Contact", field="Id", targetField="AccountId", lazy=false): 指示一个 Account 对象有多个 Contact 对象
  • targetClass : Contact 的实现类
  • field: Account 对象的字段/列
  • targetField: 目标 Contact 对象的字段/列
  • lazy: 如果 lazy = false,当您进行操作时,仓库将自动加载 Account 的 Contact 列表(默认 = true)
 $account = $accountRepository->find('0010p000002Wam9AAC');
  • @SF\Required(value=true): 指示此字段是必需的。如果保存实体时未设置此属性,将抛出异常
  • @SF\Url(value=true): 指示此字段是 URL。如果此属性的值不是 URL,将抛出异常

可用验证:Url, Email, Date

查找和计数

// Find Account by conditions, by default lazy loading = false (will load relations)
$accounts = $accountRepo->findBy(['Company_Name__c = Adant Services Group Pty Ltd']);
// Find all Account, by default lazy loading = true (will not load relations)
$accounts = $accountRepo->findAll();
// Find total number of Account
$count = $accountRepository->count();

作业(批量上传)