1

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.

Community
  • 1
  • 1
  • You should start with following the [CakePHP](http://book.cakephp.org/3.0/en/intro/conventions.html#file-and-class-name-conventions)/[PSR-4](http://www.php-fig.org/psr/psr-4/) naming conventions, `someFieldTable` is a no-go. Also http://stackoverflow.com/questions/31813722/what-means-call-to-a-member-function-on-boolean-and-how-to-fix – ndm Dec 23 '15 at 18:49
  • Thank you for the tips ndm! I wrote someFieldTable as an example of userTable.php etc in Table folder. I have more real variable names in my actual code. I just want to get a general idea of the solution. Since constructor function has an error so I assume there is an error at the initialize function. Particularly, the all the table that has "__c" as postfix of table name which is "custom fields" in salseforce has the problem. I think how the salseforce set up the table has a trick in cakephp. If you have more idea or comment please post in here. – Ryutaro Matsuda Dec 25 '15 at 02:15
  • It's not really about whether the name is real, `userTable` is just as wrong, it's about the lowercase letters and the singular case, by convention file/class names should be camel capsed, ie start with an uppercase letter, and table class names are ment to be plural. Also in order for the model to be automatically available in a controller, their names must match, otherwise you need to load it manually, which you don't show here. The problem is _not_ with the database table, at least not as far as the mentioned error is concerned – ndm Dec 25 '15 at 11:10
  • I see. so cake requires an uppercase letter and table name class when I make one and it has to be plural. I also tried some solution after comment last night and I'm write my solution in below. but I definitely try your solution once get a chance ! Thank you for your help ! – Ryutaro Matsuda Dec 25 '15 at 19:01

2 Answers2

0

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!

0

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());
        }
    }
}
Wes King
  • 627
  • 4
  • 7