dook0034 2018-08-13 00:49
浏览 64

CakePHP对象转换为数组?

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.

  • 写回答

1条回答 默认 最新

  • dongzhong8834 2018-08-13 20:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路