Code Monkey home page Code Monkey logo

cakephp-salesforce's Introduction

Salesforce Datasource plugin for CakePHP 3.x

Latest Stable Version Total Downloads Latest Unstable Version License

For earlier versions of CakePHP (2.x)

Please use the 2.x branch and follow the readme instructions under that for usage instructions

Installation

This plugin is now in Beta

  • API Compatible Saving & Reading is working
  • Schema & Connection Caching is working
  • Bear in mind that any API interaction is expensive, you should be using this with a deferred execution method.

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require voycey/cakephp-salesforce

Information

This has been somewhat of a learning curve for me, due to the nature of how Datasources are created in 3.x. I have tried to follow the patterns of the other SQL-like datasources, Eager loading is used (as in SQL sources) and there is a limited dialect.

I would have liked to do some processing on the WSDL as I did in my 2.x datasource in order to create the schema however this wasn't possible as I now needed to see the status of certain fields (readable / updateable) which has caused a need for a further call to the API.

Any API connections are cached for 1 hour (Salesforce timeout is 2 hours), after this time the connection will be refreshed (but this shouldn't matter as you are using this with a deferred execution method right?)

Notes

  1. This uses the PHP-Force.com toolkit as a dependency
  2. This uses SOAP and NOT REST (Because of Reasons™)
  3. I have only tested this with the Contact and Account SObjects, the Contact example is included in the plugin, to create more models to interact with other SObjects copy SalesforceContactTable as a template (see instructions below).
  4. Feel free to submit pull requests - here are a few examples of things I'd like to implement / test
    1. Associations between native Cake Tables & API Tables (I'm not sure this is actually possible though!)
    2. Tests (Most can probably be ripped from the core tests I assume) - I don't plan to do many as to test properly would require access to the API.
    3. Testing with all SObjects (currently I have only tested with a few but from experience with my version 2.x datasource this is usually enough to work with all SObjects)
    4. Efficiency increases.

Usage

  1. Do composer reqire as above
  2. Add Plugin::load('Salesforce', ['bootstrap' => true, 'routes' => true]); to your bootstrap.php
  3. Create the connection in app.php like this:
  'salesforce' => [
     'className' => 'Salesforce\Database\SalesforceConnection',
     'driver' => 'Salesforce\Database\Driver\Salesforce',
     'persistent' => false,
     'username' => getenv("SF_USER"),
     'password' => getenv("SF_PASSWORD"),
     'quoteIdentifiers' => false,
     'my_wsdl' => 'enterprise.wsdl.xml'
  ],
    **Your SF_PASSWORD should be your password + security token**
  1. Get your Enterprise WSDL and place it in the app config directory
  2. Create a test controller that looks something like this
 <?php
    namespace App\Controller;
       
    use App\Controller\AppController;
    use Cake\Event\Event;
       
    class SalesforcesController extends AppController 
    {
       
       public function beforeFilter(Event $event)
       {
           parent::beforeFilter($event);
       }
       
       public function index()
       {
           $this->autoRender = false;
           $this->loadModel('Salesforce.SalesforceContact');
           $query = $this->SalesforceContact->find('all')
                       ->select(['Id','Email','LastName'])
                       ->where(['Email' => "[email protected]"]
                   );
   
           foreach ($query as $row) {
             echo "<pre>";
             print_r($row);
             echo "</pre>";
           }
   
       }
    }

Then browse to /salesforces and you should have a couple of the standard Salesforce records. If not then go back and repeat these steps. If you get an interesting error message then.... well sorry, I'm sure it will get fixed as I use it more.

Interfacing with other Salesforce Items

This should simply be a case of extending "SalesforcesTable" rather than Table with your chosen Item (e.g. Account)

 <?php
  namespace App\Model\Table;
  
  use Salesforce\Model\Entity\Salesforce;
  use Salesforce\Model\Table\SalesforcesTable;
  
  class SalesforceAccountTable extends SalesforcesTable
  {
      public $name = "Account";
  
      /**
       * Initialize method
       *
       * @param  array $config The configuration for the Table.
       * @return void
       */
      public function initialize(array $config)
      {
          parent::initialize($config);
  
          $this->table('Account');
          $this->displayField('Name');
          $this->primaryKey('Id');
      }
  }

cakephp-salesforce's People

Contributors

voycey avatar zuluru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cakephp-salesforce's Issues

Cake 3.3 compatibility

Looks like some bits of this don't work with Cake 3.3. I've done a PR for some easy fixes, but have additional things I'm not sure how to fix. Happy to work towards resolving those, but don't have experience with ORM internals, so I have some questions to get me going in the right direction. What's the best way to ask them? Here? Email? Skype?

table salesforce_contacts missing

Hello there,

first off thank you for this quick and slick easy-to-install plugin; however, it seems that the correct datasource is not being utilized although i am assigning SalesforceSource in the app/Config/database.php as datasource. i'm getting:

Error: Table salesforce_contacts for model SalesforceContact was not found in datasource default.

am i missing anything? i'm sure i've followed all instructions on the readme. is it possible that the datasource variable name $sflive should be renamed to $_baseConfig? (first time i install a plugin on Cakephp 2.0 - im upgrading an existing cakephp site).

thanks,
/avz

Help on TLS

Hi Dan,

We need your help in solving TLS upgrading issue with CakePHP and Soap.

Does your CakePHP 2.x salesforce plugin is compatible with TLS 1.2?

If yes then please let us know what are the other places where we need to make code change for upgrading TSL 1.0 to TLS 1.2

Thanks,
Inderjeet

Error when using example code

Hi,

I am trying this plugin with CakePHP 3.x but I am stuck at this error while using the example code you have in readme. Below is the error message I am getting

Error: Uncaught Error: Using $this when not in object context in /home/jj/testproject/vendor/voycey/cakephp-salesforce/src/Database/SalesforceQuery.php:54 Stack trace: #0 /home/jj/testproject/vendor/voycey/cakephp-salesforce/src/ORM/SalesforceQuery.php(63): Salesforce\Database\SalesforceQuery::sql(NULL, Object(Salesforce\ORM\SalesforceQuery)) #1 /home/jj/testproject/vendor/voycey/cakephp-salesforce/src/Database/Statement/SalesforceStatement.php(37): Salesforce\ORM\SalesforceQuery->sql() #2 /home/jj/testproject/vendor/cakephp/cakephp/src/Database/Statement/StatementDecorator.php(174): Salesforce\Database\Statement\SalesforceStatement->execute(NULL) #3 /home/jj/testproject/vendor/cakephp/cakephp/src/Database/Log/LoggingStatement.php(56): Cake\Database\Statement\StatementDecorator->execute(NULL) #4 /home/jj/testproject/vendor/voycey/cakephp-salesforce/src/Database/SalesforceConnection.php(64): Cake\Database 
File /home/jj/testproject/vendor/voycey/cakephp-salesforce/src/Database/SalesforceQuery.php 
Line: 54

Any help is much appreciated.

Thanks for developing this package.

Record ID not returned in the result

Hello there,

first off i can't thank you enough for this plugin, it was easy to integrate and pretty straight forward. However, although it's not a bug, but probably something im incapable of doing.

we both know Salesforce returns the ID of the record once we create a new record on the SF database, however, i get null. in the logs, i find that this ID is dumped from the file /Plugin/Salesforce/Model/Datasource/SalesforceSource.php line 207.

try {
                $response = $this->client->create(array($data), $model->name);` 
                $this->log("before response");
                $this->log($response);
                $this->log("after response");
            } catch (Exception $e) {
                $response->errorMessages = array($e->faultstring, $this->client->getLastRequest());
                $this->log("Error in Create: ". $response->errorMessages);
                return false;
            }

            return ($response); //This should return the ID of the created record?

The returned value is in fact the ID of the inserted record, but in my controller when i add a new Contact, the result is a null:

//now, add the user as Contact on Salesforce
			$this->loadModel('Salesforce.SalesforceContact');
			$this->SalesforceContact->create();
            $crm_data = array(
                "SalesforceContact" =>
	                array(
	                    'FirstName' => $data['User']['first_name'],
	                    'LastName' => $data['User']['last_name'],
	                    'Phone' => $data['User']['phone'],
	                    'MailingStreet' => $data['User']['address'],
	                    'MailingCountry' => "Country",
	                    'Email' => $data['User']['email'],
	                    'LeadSource' => 'website',
	                    'Stage__c' => 'Lead',
	                    'TDHC_Webpage_Source__c' => 'contact',
	                    'Website_Counter__c' => $insert_id,
	                    'UTM_Campaign__c' => $data['User']['UTM_Campaign__c'],
	                    'UTM_Content__c' => $data['User']['UTM_Content__c'],
	                    'UTM_Medium__c' => $data['User']['UTM_Medium__c'],
	                    'UTM_Source__c' => $data['User']['UTM_Source__c'],
	                    'UTM_Term__c' => $data['User']['UTM_Term__c'],
	                )
            );
            $crm_result = $this->SalesforceContact->saveAll($crm_data);
            $this->log($crm_result);

what could be wrong? the result in the LOG file is as per the below:

2016-12-16 18:38:15 Error: before response
2016-12-16 18:38:15 Error: Array
(
    [0] => stdClass Object
        (
            [id] => 0032XXXXXXXXb0qAAA
            [success] => 1
        )

)

2016-12-16 18:38:15 Error: after response
2016-12-16 18:38:15 Error: 

See the last line? that's the empty $crm_result, but the previous Datasource file is dumping the correct value.

thanks,
Amjo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.