dongtaochan0777 2015-12-23 05:44
浏览 40
已采纳

如何连接到Heroku connect的表,该表在cake php 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.

  • 写回答

2条回答 默认 最新

  • doudi5892 2015-12-28 21:27
    关注

    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());
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀