如何连接到 Heroku connect 的表,该表在蛋糕 php v3.x 中的 salesforce 中作为自定义字段创建



我正在尝试通过CakePHP 3连接Heroku连接表。出于某种原因,当我试图连接一个名称以'__c'结尾的表时,我会出现以下错误

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

之前,我在CakePHP中解决了基本的连接问题Heroku Connect与Cakephp v3.0.12表单.

所以我可以连接到一个表名中没有'__c'的表。从错误消息中,我知道由于某种原因,我的蛋糕应用程序无法连接到我想要连接的表。

在我的App/Model/Table/someFieldTable.php中,我有

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

我的表Controller.php 中也有以下内容

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

我还是CakePHP和Heroku连接的新手。如果有人知道如何在CakePHP中使用后缀'__c'连接这些字段(表),请帮助我。

使用TableRegistry类是一个有效的答案,下面是让控制器中的自动连接表工作的正确方法:

如您所知,您的文件命名方案不正确,但这并不是使用Heroku格式化表名的完整解决方案。$this->table()中的条目不应该是带点数据库的名称空间,因为数据库是通过当前连接(很可能是在app.php中定义的默认数据源)附加的

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

感谢ndm Cake在我使用这些特殊类时对其类名和字母大小写很敏感。

我也在晚上解决了这个问题。在我的控制器中,我向表类添加了单独的实体类。

use AppModelEntitySomeFields;
use CakeORMTableRegistry;

当我创建数据对象时,我使用手动构造这些类,而不是使用newEntity()

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

现在我可以手动将变量分配给数据对象例如

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

为了保存数据,我现在必须手动调用保存功能

$someFieldTable->save($someField)

而且。。。哇!这是我的肮脏类型的解决方案,我应该正确地修复类和文件的名称。再次感谢ndm的帮助!

最新更新