cakephp对象转换为数组?

k4aesqcs  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(369)

我试图在我的一个表上显示名称而不是ID。我有一个workrequest,每个复选框都有许多workrequestchecks。
这些是我的table 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的列表。除了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索引页上工作,如下所示。

这是我在workrequests页面上引用checkbox name得到的错误的照片。

这是有关workrequestchecks的my workrequests view.ctp的代码

<?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'

}

然后我试着在一张美元支票上打印

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

我不知道为什么这不起作用;它似乎将对象$check转换为数组。

8ftvxx2r

8ftvxx2r1#

在ctp中,您尝试访问$checks->checkbox->name
但正如你的垃圾堆所说,它只包含:
对象(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'

}
因此,根本没有checkbox字段,然后将始终为null,您将得到错误。如果要从id为的复选框中检索数据,则需要在模型中将其关联起来。此外,请选中递归,您可能希望将其更改为1或2。

相关问题