dpymrcl269187540 2015-10-28 20:39
浏览 17

找到许多对象CakePHP 3.0

I have on my CakePHP project some Categories that hasMany Subcategories that hasMany Texts that hasMany and belongToMany tags. To rephrase it, the Texts can be tagged and belong to a subcategory. A subcategory belong to a category.

I'm looking for a clean and efficient way to get these informations out of the table. I want for all Categories, all of these Subcategories, the Texts that depends on it and their tags.

I think in the first time that my controller should return an array like :

['categories'=>[[
    'name'=>'cat1' 'subcategories'=>[[
        'name'='sub1', texts=>[[
            'name'=>'text1', 'tags'=>['tag1', 'tag2']
        ]]
    ]]
]]]

So i tried to build this structure by hand and I made a loop in a loop in a loop... The code was so horrible I knew my way was wrong.

I tried to join all the tables together with the 'find' method but it doesn't seem to work. here is the beginning of the code :

        $query = $categories->find('all', [
        'where'=>['Categorie.enabled ='=>true],
        'join'=>[
        [
            'table' => 'Subcategories',
            'type' => 'LEFT',
            'conditions' => 'Subcategories.categorie_id = Categories.id'

        ],[
            'table' => 'Texts',
            'type' => 'LEFT',
            'conditions' => 'Texts.subcategorie_id = Subcategories.id'

        ]
        ],
        'fields' => ['Subcategories.name', 'Categories.name']
        ]);

I got two row, one for each subcategory but I can't see the texts.

Am I doing right using joins ? Why the texts I created are not visible on my test page ? Is my orthographic mistake on the singular of categories the reason of this malfunction ?

I hope I'm understandable.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害