2

I get an Error-Message in a UnitTest in CakePHP 3.2 and the official documentation doesn't help me here anymore. I think the error has something todo with the SQL-Joins I try to use.

The Error-Message is the following:

`1) App\Test\TestCase\Controller\GetContentControllerTest::testIndex
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'contentmapper_test.CmDeviceclasses' doesn't exist`

In my Testclass GetContentControllerTest I load my fixtures that I need and that creates my Database-Tables on start:

`public $fixtures = [
        'app.cm_content_options', 
        'app.cm_content_addresses', 
        'app.cm_deviceclasses', 
        'app.cm_properties'
    ];`

In the setUp()-Method I load the Main-Table:

`public function setUp()
 {
    parent::setUp();
    $this->CmContentOptions = TableRegistry::get('CmContentOptions');      
 }`

My Test-Method testIndex() looks like this:

public function testIndex() 
{
    //find the belonging ContentOption to address data 
    //submitted by the client
    $this->testFindAllByUriAndDeviceclassAndBoxId();   

    assert($this->arrObjContentOptions->count() == 1);        
}

The testFindAllByUriAandDeviceclassAndBoxID() looks like shown in the following Image (the Editor is not able to prettyprint it correctly):

testFindAllByUriAandDeviceclassAndBoxID()

It's hard to describe the whole Context; I hope it is possible to understand.

The Error happens exactly on the statement shown in the image:

$result = $query->toArray()

I think I just forgot something to add in the setUp() Method or something like that.

I hope anyone can help.

Niklas
  • 95
  • 1
  • 9
  • Please don't just post images of code, you can add them additionally if need to point something out that doesn't work with the SO editor, but always add the actual code to the question! Also whenever receiving errors, please always post the complete error message _including the full stacktrace_! – ndm Feb 22 '16 at 17:10

1 Answers1

0

You joins are set up incorrectly, you're mixing up aliases and table names.

The alias is the key of the join array, and the table key should hold the actual database table name, not the table class name.

Given that you are following CakePHPs naming conventions for your database table names, your join setup should look more like this

[
    'CmDeviceclasses' => [ /* < this is the SQL alias */
        'table' => 'cm_deviceclasses', /* < this is the database table name */
        'type' => 'LEFT',
        'conditions' => [
            'CmDeviceclasses.classname' => $this->deviceclass
        ]
    ],
    'CmContentAddresses' => [
        'table' => 'cm_content_addresses',
        'type' => 'INNER',
        'conditions' => [
            'CmContentAddresses.uri' => $this->uri,
            'CmContentAddresses.boxid' => $this->boxid,
        ]
    ],
],
[
    'CmDeviceclasses.classname' => 'string',
    'CmContentAddresses.uri' => 'string',
    'CmContentAddresses.boxid' => 'string'
]

There is no technical need to follow the CamelCase conventions for the aliases, but for sure it doesn't hurt to generally stick to the conventions.

ps, if you setup the associations properly, then there should be no need to use manual joins, you could just use Query::contain() and Query::innerJoinWith() or Query::matching().

See

ndm
  • 59,784
  • 9
  • 71
  • 110