如何在cakephp 3中打印_matchingData对象值 [英] How to print _matchingData object value in cakephp 3

查看:19
本文介绍了如何在cakephp 3中打印_matchingData对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已打印对象,正在尝试打印Tenats.Stage

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

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

打印此信息=>

object(AppModelEntityTenancy) {

    'created' => object(CakeI18nFrozenTime) {

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

    },
    'tenants' => [],
    'property' => object(CakeORMEntity) {

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

    },
    '_matchingData' => [
        'Tenants' => object(CakeORMEntity) {

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

        }
    ],

我需要‘暂存’值

请帮忙

推荐答案

这就是打印_MatchingData的方法

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

但是,如果您自动指定主Parents字段id(Tenancy.id),您的数据看起来会好得多。例如,我的父模型是"Tenancy",现在我得到了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(CakeORMQuery $query) {
                return $query->where([
                    'Properties.active' => 1
                ]);
            })
            ->contain([
                'Tenants'  
            ])
            ->matching('Tenants', function(CakeORMQuery $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>

Prints=>

0 => object(AppModelEntityTenancy) {

    'id' => (int) 3923,
    'created' => object(CakeI18nFrozenTime) {

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

    },
    'stage' => (int) 2,
    'tenants' => [
        (int) 0 => object(CakeORMEntity) {

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

结果:确保始终正确编写查询,以便更好、更整洁地访问数据。

这篇关于如何在cakephp 3中打印_matchingData对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆