gpt4 book ai didi

php - CakePHP 对象转换为数组?

转载 作者:行者123 更新时间:2023-11-29 17:12:28 25 4
gpt4 key购买 nike

我正在尝试在我的一张表上显示名称而不是 ID。我有一个 WorkRequest,其中每个 Checkbox 有许多 WorkRequestChecks

这些是我的表创建语句

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列表。除了 cakephp 似乎没有将 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 页面上引用 Checkbox 名称时遇到的错误的照片。 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 Controller View 方法

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 文件的一部分 公共(public)函数初始化(数组$config) { 父级::初始化($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 转换为数组。

最佳答案

与您的 CTP 中一样,您正在尝试访问 $Checks->checkbox->name

但正如你的转储所说,它只包含:

对象(应用程序\模型\实体\WorkRequestCheck){

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

}

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

关于php - CakePHP 对象转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51814004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com