Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing SOQL relationship query with eloquent #34

Open
scottgrayson opened this issue Jan 11, 2020 · 5 comments
Open

Replacing SOQL relationship query with eloquent #34

scottgrayson opened this issue Jan 11, 2020 · 5 comments

Comments

@scottgrayson
Copy link

scottgrayson commented Jan 11, 2020

I can get the relationships using the model's columns array using dot notation and nested selects. But I have not been able to refactor to using eloquent relations

The following works

Mandate::first();

with the supporting code...

<?php

namespace App\SaleforceModels;

use Lester\EloquentSalesForce\Model;
use Lester\EloquentSalesForce\Database\SOQLBuilder;

class Mandate extends Model
{
    protected $table = 'Company__c';

    public $columns = [
        'Id',
        'Name',
        'Opportunity__r.Id',
        'Opportunity__r.Name',
        '(Select Id, Name from Shareholders__r)',
    ];
}

My attempt to use relations does not. Could you point out what I'm doing wrong here?

return Mandate::with('shareholders')->find($this->salesforce_id);

Omniphx\Forrest\Exceptions\SalesforceException [ { "errorCode": "NOT_FOUND", "message": "The requested resource does not exist" } ]

supporting code below

<?php

namespace App\SaleforceModels;

use Lester\EloquentSalesForce\Model;
use Lester\EloquentSalesForce\Database\SOQLBuilder;
use App\SaleforceModels\Shareholder;
use App\SaleforceModels\Opportunity;

class Mandate extends Model
{
    protected $table = 'Company__c';

    public $columns = [
        'Id',
        'Name',
    ];

    public function shareholders()
    {
        return $this->belongsToMany(Shareholder::class);
    }

    public function opportunity()
    {
        return $this->belongsTo(Opportunity::class);
    }
}
<?php

namespace App\SaleforceModels;

use Lester\EloquentSalesForce\Model;
use Lester\EloquentSalesForce\Database\SOQLBuilder;

class Shareholder extends Model
{
    protected $table = 'Shareholder__c';

    public $columns = [
        'Id',
        'Name',
    ];
}
<?php

namespace App\SaleforceModels;

use Lester\EloquentSalesForce\Model;
use Lester\EloquentSalesForce\Database\SOQLBuilder;

class Opportunity extends Model
{
    protected $table = 'Opportunity__c';

    public $columns = [
        'Id',
        'Name',
    ];
}
@roblesterjr04
Copy link
Owner

Some things to note first:

  1. You do not need to include use ... SOQLBuilder; - this is already part of the model.
  2. You should not need to include use App\SalesforceModels... when used in a model that already exists there.
  3. Your opportunity model is referencing Opportunity__c - assuming you are trying to access the standard SFDC Opportunity table, you do not need to use __c and thats probably why you're getting the "resource not found error"

@scottgrayson
Copy link
Author

@roblesterjr04 1 and 2 I'll remove the extra code
3. Opportunity__c is the correct model. It is a custom table not the standard table

I'm wondering how to access the relations. It works with an SOQL query and it works by using dot notation and nested selects inside the $columns variable. but it does not work using belongsTo belongsToMany

@roblesterjr04
Copy link
Owner

Thats really weird, i use relationships all over the place in my own use, haven't experienced this.

@roblesterjr04
Copy link
Owner

My guess is you probably need to add the foreign keys to the relationships, and make sure any associated foreign/primary keys are included in your column selections.

For example, here's a model I used. I had to specify which column i was using as the foreign key.

<?php

namespace App\SalesForce;

use Lester\EloquentSalesForce\Model;

class AccountTeamMember extends Model
{

    public function account()
    {
        return $this->belongsTo(Account::class, 'AccountId');
    }

}

Where AccountId is the column on AccountTeamMember that references the Id on the Account object.

This is standard Eloquent relationship practice if the column IDs can't be assumed by the framework, which rarely works on Salesforce tables.

@roblesterjr04
Copy link
Owner

Oh I see whats happening now - you're specifying columns, and when you do that, you're basically causing the relationship columns not to be selected, and they are needed. Make sure you are adding those relationship ID columns to the $columns array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants