【问题标题】:How to connect to Heroku connect's table that is created as custom field in salesforce in cake php v3.x如何连接到在 cake php v3.x 中的 salesforce 中作为自定义字段创建的 Heroku connect 表
【发布时间】:2016-03-29 12:22:29
【问题描述】:

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

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

以前,我解决了我在 CakePHP 中遇到的基本连接问题 Heroku Connect with 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');
}

我的 tableController.php 中也有以下内容

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

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

【问题讨论】:

  • 您应该从遵循CakePHP/PSR-4 命名约定开始,someFieldTable 是不行的。还有stackoverflow.com/questions/31813722/…
  • 感谢 ndm 的提示!我在 Table 文件夹中写了 someFieldTable 作为 userTable.php 等的示例。我的实际代码中有更多真实的变量名。我只是想大致了解解决方案。由于构造函数有错误,所以我假设初始化函数有错误。特别是,所有具有“__c”作为表名后缀的表,即salseforce中的“自定义字段”都有问题。我认为 salseforce 如何设置表格在 cakephp 中有一个技巧。如果您有更多想法或评论,请在此处发布。
  • 这不是关于名称是否真实,userTable 也是错误的,它是关于小写字母和单数大小写,按照惯例文件/类名应该是驼峰式大写,即以一个大写字母,并且表类名称是复数。此外,为了使模型在控制器中自动可用,它们的名称必须匹配,否则您需要手动加载它,此处未显示。问题是不是数据库表,至少就提到的错误而言不是
  • 我明白了。所以当我制作蛋糕时,蛋糕需要一个大写字母和表名类,并且它必须是复数。昨晚发表评论后,我也尝试了一些解决方案,并将我的解决方案写在下面。但是一旦有机会,我肯定会尝试您的解决方案!感谢您的帮助!

标签: php postgresql cakephp heroku salesforce


【解决方案1】:

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

我也通宵解决了这个问题。 在我的控制器中,我将单个实体类另外添加到表类中。

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

当我创建数据对象时,我使用手动构造这些类来代替使用 newEntity()

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

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

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

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

$someFieldTable->save($someField)

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢 LeWestopher 的解决方案!
    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    相关资源
    最近更新 更多