如何在cakephp 3中打印_matchingData对象值

时间:2016-03-19 00:08:01

标签: cakephp cakephp-3.0

我有打印物品并试图打印Tenats.stage

<?php foreach ($tenancies as $tenancy): ?>

    <td><?= debug($tenancy); ?></td>

打印此=&gt;

object(App\Model\Entity\Tenancy) {

    'created' => object(Cake\I18n\FrozenTime) {

        'time' => '2016-03-18T15:57:40+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'tenants' => [],
    'property' => object(Cake\ORM\Entity) {

        'id' => (int) 4110,
        'address1' => '119 Alan Moss Road',
        'postcode' => 'le115ly',
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Properties'

    },
    '_matchingData' => [
        'Tenants' => object(Cake\ORM\Entity) {

            'stage' => (int) 2,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Tenants'

        }
    ],

我需要'阶段'价值

请帮助

1 个答案:

答案 0 :(得分:4)

如何打印_matchingData

<td><?= h($tenancy->_matchingData['Tenants']->stage); ?></td>

但是,如果您自动指定主要父母字段ID(Tenancy.id),您的数据将会更好看。例如,我的父母模型是&#34;租约&#34;现在我得到Tenant.id和Tenancy.id以及Property.id:

$tenancies = $this
            ->find()
            ->select([ 
                'Tenancy.id', 'Tenancy.created', 'Tenancy.stage',
                'Properties.id', 'Properties.address1', 'Properties.postcode',
                'Tenants.stage',
            ])
            ->contain('Properties', function(\Cake\ORM\Query $query) {
                return $query->where([
                    'Properties.active' => 1
                ]);
            })
            ->contain([
                'Tenants'  
            ])
            ->matching('Tenants', function(\Cake\ORM\Query $query) {
                return $query->where([
                    'Tenants.active' => 1
                ]);
            })
            ->where([
                'Tenancy.active' => 1,
                $conditions
            ])
            ->order([
                'Tenancy.created' => 'DESC',
                'Tenants.tenancy_id'
            ]); 

        return $tenancies;
    }

它打印出具有深层关联的数组,很酷,我可以得到我的租户属性:

 <td><?= h($tenancy->tenants->stage); ?></td>

打印=&GT;

0 => object(App\Model\Entity\Tenancy) {

    'id' => (int) 3923,
    'created' => object(Cake\I18n\FrozenTime) {

        'time' => '2016-03-19T13:12:32+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'stage' => (int) 2,
    'tenants' => [
        (int) 0 => object(Cake\ORM\Entity) {

            'id' => (int) 8903,
            'user_id' => (int) 15318,
            'tenancy_id' => (int) 3923,
            'needs_guarantor' => true,
            'guarantor_id' => null,
            'holding_fee' => (float) 50,

结果:确保始终正确编写查询,以便您可以轻松访问数据。