dongtang6775 2014-02-05 18:53
浏览 31
已采纳

CakePHP xmlview序列化创建额外的节点

I'm new to CakePHP and am trying to set up a basic controller that returns json or xml data, by following this guide on the cakephp website. http://book.cakephp.org/2.0/en/development/rest.html

The problem is it returns a bunch of excess markup in the case of both json and xml. I'm trying to figure out what's going on with just the xml for now since it's most obvious to see what's broken. It says it should return something like:

<recipes>
    <recipe id="234" created="2008-06-13" modified="2008-06-14">
        <author id="23423" first_name="Billy" last_name="Bob"></author>
        <comment id="245" body="Yummy yummmy"></comment>
    </recipe>
    <recipe id="3247" created="2008-06-15" modified="2008-06-15">
        <author id="625" first_name="Nate" last_name="Johnson"></author>
        <comment id="654" body="This is a comment for this tasty dish."></comment>
    </recipe>
</recipes>

But instead returns something like:

<resources>
    <recipes>
        <recipe id="234" created="2008-06-13" modified="2008-06-14">
            <author id="23423" first_name="Billy" last_name="Bob"></author>
            <comment id="245" body="Yummy yummmy"></comment>
        </recipe>
    </recipes>
    <recipes>
        <recipe id="3247" created="2008-06-15" modified="2008-06-15">
            <author id="625" first_name="Nate" last_name="Johnson"></author>
            <comment id="654" body="This is a comment for this tasty dish."></comment>
        </recipe>
    </recipes>
</resources>

Looking into the way the Model find function returns data it might be due to the structure of the arrays that come back. I can change the structure around, but wanted to make sure I didn't miss something.

  • 写回答

1条回答 默认 最新

  • dqhdpppm02183 2014-02-06 15:31
    关注

    If you have something like

        $recipes = $this->Recipe->find('all');
    

    try to add

        $recipes = Hash::extract($recipes, "{n}.Recipe");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊