如何在cakephp 3中使用与TableRegistry的关联

时间:2015-08-23 16:51:48

标签: php cakephp cakephp-3.0

正如您在第一个Business \ Model \ Entity \ Business Object中看到的那样,当在BusinessController中调用时,存在类别关联, 但是,当使用TableRegistry在CategoriesController中调用它时,它会丢失。为什么 ?任何想法如何解决这个问题? 我在BusinessesController中调用了以下调用

$this->autoRender = false;
        $this->paginate = [
            'contain' => ['Cities', 'Categories' ]
        ];
        foreach( $this->paginate( $this->Businesses ) as $business ) {
            pr( $business );
        }

我得到了这个结果

Business\Model\Entity\Business Object
(
    [id] => 1
    [city_id] => 414
    [postal_id] => 780299
    [slug] => coatings-2000-lloydminster-ab-ca-1
    [business_name] => Coatings 2000
    [street_address] => 2B Production Ave
    [address_locality] => Lloydminster
    [address_region] => AB
    [postal_code] => T9V 0Y4
    [last_modified] => Cake\I18n\Time Object
        (
            [time] => 2015-03-12T00:00:00+0530
            [timezone] => Asia/Calcutta
            [fixedNowTime] => 
        )

    [status] => 1
    [categories] => Array
        (
            [0] => Business\Model\Entity\Category Object
                (
                    [id] => 2276
                    [parent_id] => 2212
                    [category_slug] => protective-coatings
                    [category_name] => Protective Coatings
                    [lft] => 4449
                    [rght] => 4450
                    [level] => 2
                    [_joinData] => Cake\ORM\Entity Object
                        (
                            [category_id] => 2276
                            [business_id] => 1
                            [[new]] => 
                            [[accessible]] => Array
                                (
                                    [*] => 1
                                )

                            [[dirty]] => Array
                                (
                                )

                            [[original]] => Array
                                (
                                )

                            [[virtual]] => Array
                                (
                                )

                            [[errors]] => Array
                                (
                                )

                            [[repository]] => BusinessesCategories
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [*] => 1
                        )

                    [[dirty]] => Array
                        (
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[errors]] => Array
                        (
                        )

                    [[repository]] => Business.Categories
                )

        )

    [city] => Business\Model\Entity\City Object
        (
            [id] => 414
            [city_slug] => lloydminster
            [city_name] => Lloydminster
            [[new]] => 
            [[accessible]] => Array
                (
                    [*] => 1
                )

            [[dirty]] => Array
                (
                )

            [[original]] => Array
                (
                )

            [[virtual]] => Array
                (
                )

            [[errors]] => Array
                (
                )

            [[repository]] => Business.Cities
        )

    [[new]] => 
    [[accessible]] => Array
        (
            [*] => 1
        )

    [[dirty]] => Array
        (
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
        )

    [[errors]] => Array
        (
        )

    [[repository]] => Business.Businesses
)

当我在类别控制器中调用它时,它显示缺少的类别关联

$this->autoRender = false;
        $this->paginate = [
            'contain' => ['Cities', 'Categories' ]
        ];
        $businessTable = \Cake\ORM\TableRegistry::get( 'Business.Businesses' );
        foreach( $this->paginate( $businessTable ) as $business ) {
            pr( $business );
        }

我得到了缺少类别关联的结果

Business\Model\Entity\Business Object
(
    [id] => 1
    [city_id] => 414
    [postal_id] => 780299
    [slug] => coatings-2000-lloydminster-ab-ca-1
    [business_name] => Coatings 2000
    [street_address] => 2B Production Ave
    [address_locality] => Lloydminster
    [address_region] => AB
    [postal_code] => T9V 0Y4
    [last_modified] => Cake\I18n\Time Object
        (
            [time] => 2015-03-12T00:00:00+0530
            [timezone] => Asia/Calcutta
            [fixedNowTime] => 
        )

    [status] => 1
    [categories] => Array
        (
        )

    [city] => Business\Model\Entity\City Object
        (
            [id] => 1
            [city_slug] => albany
            [city_name] => Albany
            [[new]] => 
            [[accessible]] => Array
                (
                    [*] => 1
                )

            [[dirty]] => Array
                (
                )

            [[original]] => Array
                (
                )

            [[virtual]] => Array
                (
                )

            [[errors]] => Array
                (
                )

            [[repository]] => Business.Cities
        )

    [[new]] => 
    [[accessible]] => Array
        (
            [*] => 1
        )

    [[dirty]] => Array
        (
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
        )

    [[errors]] => Array
        (
        )

    [[repository]] => Business.Businesses
)

1 个答案:

答案 0 :(得分:0)

我想当你在类别控制器中并为contain定义this时,你正在引用 Categories 模型,而不是 Business paginate没有取contain的原因,所以这就是为什么。

你可以试试这个:

$this->paginate['Businesses'] = [
        'contain' => ['Cities', 'Categories' ]
    ];