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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退