mark.nz
mark.nz

Reputation: 137

CakePHP object converting to array?

I'm trying to display names instead of ids on one of my tables. I have a WorkRequest which has many WorkRequestChecks of each Checkbox.

These are my table create statements

CREATE TABLE `work_requests` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
);
CREATE TABLE `work_request_checks` (
  `work_request_id` int(11) NOT NULL,
  `checkbox_id` int(11) NOT NULL,
  `value` int(3) DEFAULT NULL,
  PRIMARY KEY (`work_request_id`,`checkbox_id`)
);
CREATE TABLE `checkboxes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`id`)
);

From the WorkRequest page I am trying to show a list of WorkRequestChecks that have been entered. This is working fine apart from the fact it seems like cakephp isn't seeing the WorkRequestCheck as an object. It will display the ids of the Checkbox but when I try reference the ->name I get the error.

include - APP/Template/WorkRequests/view.ctp, line 193
Cake\View\View::_evaluate() - CORE/src/View/View.php, line 1010
Cake\View\View::_render() - CORE/src/View/View.php, line 971
Cake\View\View::render() - CORE/src/View/View.php, line 595
Cake\Controller\Controller::render() - CORE/src/Controller/Controller.php, line 623
Cake\Http\ActionDispatcher::_invoke() - CORE/src/Http/ActionDispatcher.php, line 125
Cake\Http\ActionDispatcher::dispatch() - CORE/src/Http/ActionDispatcher.php, line 93
Cake\Http\BaseApplication::__invoke() - CORE/src/Http/BaseApplication.php, line 78
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Routing\Middleware\RoutingMiddleware::__invoke() - CORE/src/Routing/Middleware/RoutingMiddleware.php, line 59
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Routing\Middleware\AssetMiddleware::__invoke() - CORE/src/Routing/Middleware/AssetMiddleware.php, line 88
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Error\Middleware\ErrorHandlerMiddleware::__invoke() - CORE/src/Error/Middleware/ErrorHandlerMiddleware.php, line 93
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
DebugKit\Middleware\DebugKitMiddleware::__invoke() - ROOT/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php, line 52
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65

This is strange as I have this working on WorkRequestChecks index page as displayed below. WorkRequestChecks Index Page

This a photo of the errors I get referencing Checkbox name on the WorkRequests page. WorkRequests View Page

This is my code for my WorkRequests view.ctp relating the WorkRequestChecks

<?php foreach ($workRequest->work_request_checks as $Checks): ?>
    <tr>
        <td><?= $this->Html->link($Checks->checkbox->name, ['controller' => 'Checkboxes', 'action' => 'view', $Checks->checkbox->id])?></td>
        <td><?= h($Checks->value) ?></td>
    </tr>
<?php endforeach; ?>

I tried printing the $workRequest object and this is the result

object(App\Model\Entity\WorkRequest) {

    'id' => (int) 80590,
    'invoice_id' => (int) 21412009,
    'item' => '1',
    'defect' => 'Calibration Due',
    'model_no' => 'TE135 30dB Attenuator',
    'part_no' => 'SA4N50-30',
    'quantity' => '1',
    'serial_no' => 'NSN',
    'status_id' => '3',
    'worksheet_id' => 'WSCC',
    'remarks' => '',
    'mm' => '',
    'cal_cert_ref' => '0',
    'test_equipment_id' => '',
    'ATA_chapter' => '',
    'pub' => '0',
    'pub_last_controlled' => '',
    'test_harness_used' => '',
    'closed' => (int) 1,
    'work_request_checks' => [
        (int) 0 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 1,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 1 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 2,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 2 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 3,
            'value' => (int) 0,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 3 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 4,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 4 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 5,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 5 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 6,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 6 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 8,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 7 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 17,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        },
        (int) 8 => object(App\Model\Entity\WorkRequestCheck) {

            'work_request_id' => (int) 80590,
            'checkbox_id' => (int) 19,
            'value' => (int) 1,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'WorkRequestChecks'

        }
    ],
    'work_tasks' => [],
    'test_equipment' => null,
    'worksheet' => null,
    'status' => object(App\Model\Entity\Status) {

        'id' => (int) 3,
        'name' => 'Calibrated',
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Statuses'

    },
    'invoice' => object(App\Model\Entity\Invoice) {

        'id' => (int) 21412009,
        'start_date' => object(Cake\I18n\FrozenDate) {

            'time' => '2014-12-01T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'close_date' => object(Cake\I18n\FrozenDate) {

            'time' => '2014-12-01T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'invoice_to_id' => '1504',
        'contact_name' => '',
        'phone_number' => '',
        'aircraft_reg_id' => '',
        'ship_to_address' => '',
        'ship_via_id' => '89',
        'notes' => 'Item 1:  TE135 30DB ATTENUATOR  P/N: SA4N50-30  S/N: NSN.  Defect Reported:  Calibration Due., Calibration carried out and found serviceable.',
        'worksheet_notes' => 'Item 1:  TE135 30DB ATTENUATOR  P/N: SA4N50-30  S/N: NSN.  Defect Reported:  Calibration Due., Calibration carried out and found serviceable.',
        'closed' => true,
        'times_printed' => '1',
        'payment_due' => '1',
        'opening_notes' => '',
        'activity_counter' => '0',
        'last_viewed' => object(Cake\I18n\FrozenTime) {

            'time' => '2014-12-15T08:24:00+13:45',
            'timezone' => 'NZ-CHAT',
            'fixedNowTime' => false

        },
        'last_viewed_by_id' => '62',
        'courier_ticket_no' => '',
        'job_description' => 'Calibration',
        'spell_checked' => true,
        'authorisation_no' => '',
        'priority_id' => '',
        'job_type_id' => '',
        'opened_by_id' => '47',
        'assigned_to_id' => '47',
        'checked_out_to_id' => (int) 1,
        'exclued_from_unclosed_job_report' => false,
        'certification_required' => false,
        'requested_finish_date' => object(Cake\I18n\FrozenDate) {

            'time' => '2014-12-04T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'exclude_from_requested_finish_date' => false,
        'supervising_engineer_id' => '1',
        'quote_ref' => '1',
        'currency_id' => '1',
        'ent_MYOB' => true,
        'MYOB_amount' => '$113.85',
        'MYOB_batch' => '664',
        'exchange_rate' => '1',
        'payment_instructions_id' => '1',
        'invoice_emailed' => true,
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Invoices'

    },
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'WorkRequests'

}

Then I tried printing on of the $Checks

    object(App\Model\Entity\WorkRequestCheck) {

    'work_request_id' => (int) 80590,
    'checkbox_id' => (int) 1,
    'value' => (int) 1,
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'WorkRequestChecks'

}

This is my WorkRequest controller view method

public function view($id = null)
    {   
        $workRequest = $this->WorkRequests->get($id, [
            'contain' => ['WorkRequestChecks']
        ]);

        $this->set('workRequest', $workRequest);
        $this->set('_serialize', ['workRequest']);

Here's part of my WorkRequestsTable.php file

public function initialize(array $config)
    {
        parent::initialize($config);
        $this->setTable('work_requests');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');
        $this->hasMany('WorkRequestChecks', [
            'foreignKey' => 'work_request_id'
        ]);
    }

Here's part of my CheckboxesTable.php file

 public function initialize(array $config)
    {
        parent::initialize($config);
        $this->setTable('checkboxes');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');
        $this->belongsTo('CheckboxTypes', [
            'foreignKey' => 'checkbox_type_id',
            'joinType' => 'INNER'
        ]);
    }

Here's part of my WorkRequestChecksTable.php file public function initialize(array $config) { parent::initialize($config);

        $this->setTable('work_request_checks');
        $this->setDisplayField('work_request_id');
        $this->setPrimaryKey(['work_request_id', 'checkbox_id']);

        $this->belongsTo('WorkRequests', [
            'foreignKey' => 'work_request_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Checkboxes', [
            'foreignKey' => 'checkbox_id',
            'joinType' => 'INNER'
        ]);
    }

I'm not sure why this isn't working; it seems like it's converted the object $Check to an array.

Upvotes: 0

Views: 1138

Answers (1)

user8971373
user8971373

Reputation:

As in your CTP, you're trying to access $Checks->checkbox->name

But as your dump says, it contains just:

object(App\Model\Entity\WorkRequestCheck) {

'work_request_id' => (int) 80590,
'checkbox_id' => (int) 1,
'value' => (int) 1,
'[new]' => false,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'WorkRequestChecks'

}

So, there is no checkbox field at all and then will always be null and you'll get the error. You need to associate it in your model if you want to retrieve data from a checkbox with id. Also check the recursive, you might want to change it to 1 or 2.

Upvotes: 0

Related Questions