简体   繁体   中英

How to connect to Heroku connect's table that is created as custom field in salesforce in cake php v3.x

I am trying to connect with Heroku connect table via CakePHP 3. For some reason, I get the following error when I try to connect with a table whom name ends in '__c'

 PHP Fatal error:  Call to a member function newEntity() on boolean

Previously, I solved fundamental connection problem that I had in CakePHP at Heroku Connect with Cakephp v3.0.12 form.

So I could connect with one that doesn't have '__c' in its table name. From the error msg, I understand for some reason my cake app failed to connect with the table I want to connect.

In my App/Model/Table/someFieldTable.php, I have

 public function initialize(array $config)
{
    parent::initialize($config);
    $this->table('salesforce.some_field__c');
    $this->displayField('name');
    $this->primaryKey('id');
}

I also have the following in my tableController.php

$somefield = $this->someField->newEntity();
   // variables are assigned to $somefield 
if($this->someField->save($someField)){
   // error handling here 
}

I am still new to CakePHP and Heroku connect. If anybody knows how to connect with these field (table) with postfix '__c' in CakePHP, Please help me.

Thanks to ndm Cake is sensitive to its class name and letter cases when I use these special classes.

I also solved the problem over the night. In my controller, I added individual entity classes additionally to table class.

use App\Model\Entity\SomeFields;
use Cake\ORM\TableRegistry;

and When I create data object, I use manually construct these classes instead to use newEntity()

$someFieldTable = TableRegistry::get('BottomSheet');
$someField = new SomeFileds();

Now I can assign variable to data object manually For instance

$someField->fieldName = data['fieldName'];

In order to save the data, I now have to manually call the save function

$someFieldTable->save($someField)

and ... Voila! This is my dirty type of solution, I should fix the name of classes and files properly tho. Thank you again for the help ndm!

Using the TableRegistry class is an effective answer, here is the correct method of getting the autowired table in the controller working:

As you've already been informed, your file naming scheme is incorrect, but that's not the full solution to your table name formatting with Heroku. Your entry within $this->table() should not be namespace to a database with a dot, as the database is appending via the current connection (which is most likely the default datasource defined in app.php) you are making queries on. The entire fix would consist of:

// 1. Change the file name to the correct scheme: SomeFieldTable.php

// 2. In order for the controller to autowire the table, you must correctly
// name the controller file as well: SomeFieldController.php

// 3. Change the table name within SomeFieldTable.php to the appropriate
// name: 'some_field_c'

public function initialize(array $config)
{
    parent::initialize($config);
    $this->table('some_field__c');
    $this->displayField('name');
    $this->primaryKey('id');
}

// 4. Finally, in the controller, the table is accessed via the camel capsed name

class SomeFieldController extends AppController
{
    public function getEndpoint()
    {
        $result_set = $this->SomeField->find()->all();
        $this->set('result_set', $result_set);
    }

    public function saveEndpoint()
    {
        $new_some_field = $this->SomeField->newEntity($this->request->data);
        if ($this->SomeField->save($new_some_field)) {
            $this->set('new_some_field', $new_some_field);
        } else {
            $this->set('errors', $new_some_field->errors());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM