Cakephp对象转换为数组?

时间:2018-08-13 00:49:52

标签: php mysql cakephp cakephp-3.0

我试图在我的一张桌子上显示名称而不是ID。 我有一个 WorkRequest ,其中每个复选框中都有许多 WorkRequestChecks

这些是我的表create语句

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`)
);

WorkRequest 页面上,我试图显示已输入的 WorkRequestChecks 的列表。除了cakecake似乎没有将 WorkRequestCheck 作为对象之外,这还不错。它将显示复选框的ID,但是当我尝试引用->名称时,我得到了错误。

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

这很奇怪,因为我在 WorkRequestChecks 索引页面上进行此操作,如下所示。 WorkRequestChecks Index Page

这是我在 WorkRequests 页面上引用复选框名称的错误的照片。 WorkRequests View Page

这是我的 WorkRequests view.ctp与 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; ?>

我尝试打印 $ workRequest 对象,这就是结果

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'

}

然后我尝试打印 $ 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'

}

这是我的 WorkRequest 控制器视图方法

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

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

这是我的 WorkRequestsTable .php文件的一部分

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'
        ]);
    }

这是我的 CheckboxesTable .php文件的一部分

 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'
        ]);
    }

这是我的 WorkRequestChecksTable .php文件的一部分     公共函数initialize(数组$ 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'
        ]);
    }

我不确定为什么它不起作用,好像它已将对象$ Check转换为数组一样。任何帮助将不胜感激,

谢谢!

1 个答案:

答案 0 :(得分:0)

与在CTP中一样,您正在尝试访问$ 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'

}

因此,根本没有复选框字段,然后始终为null,您将收到错误消息。 如果要从具有ID的复选框中检索数据,则需要将其与模型关联。还要检查递归,您可能要将其更改为1或2。