web service + AR关联查询的问题

Hi All,

我参照doc帮助文档的"关系型 Active Record例子"实现关联查询,但是总实现不了,遇见如下问题,麻烦大家帮忙看看.

我只有两个model: user, post,er关系图可参照例子的图,一对多关系。

以下是web service server端的代码:

User.php:




class User extends CActiveRecord

{

    /**

     * @var integer ID

     * @soap

     */

    public $id;


    /**

     * @var string username

     * @soap

     */

    public $username;


    /**

     * @var string password

     * @soap

     */

    public $password;


    /**

     * @var string email

     * @soap

     */

    public $email;


    /**

     * Returns the static model of the specified AR class.

     * @return User the static model class

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * @return string the associated database table name

     */

    public function tableName()

    {

        return 'user';

    }


    /**

     * @return array relational rules.

     */

    public function relations()

    {

        return array(

            'post'=>array(self::HAS_MANY,'Post','author_id'),

        );

    }

}



Post.php:




class Post extends CActiveRecord

{

    /**

     * @var integer ID

     * @soap

     */

    public $id;


    /**

     * @var string title

     * @soap

     */

    public $title;


    /**

     * @var string content

     * @soap

     */

    public $content;


    /**

     * @var integer author_id

     * @soap

     */

    public $author_id;


    /**

     * Returns the static model of the specified AR class.

     * @return User the static model class

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * @return string the associated database table name

     */

    public function tableName()

    {

        return 'post';

    }


    /**

     * @return array relational rules.

     */

    public function relations()

    {

        return array(

            'author'=>array(self::BELONGS_TO,'User','author_id'),

        );

    }

}



controller : GoodsController.php




class GoodsController extends CController

{


    /**

     * Declares class-based actions.

     */

    public function actions()

    {

        return array(

            'goods'=>array(

                'class'=>'CWebServiceAction',

                'classMap'=>array(

                    'User'=>'User'

                )

            ),

        );

    }


    /**

     *@return User[]  // <= return类型为User和object,输出结果会有差别

     *@soap

     */

    public function getUser()

    {

        return User::model()->with('post')->findAll();

    }

}



web service client:




<?php

ini_set("soap.wsdl_cache_enabled", "0");


$client=new SoapClient('url');

$t = $client->getUser();

var_dump($t);

?>




@return [color="#8B0000"]User[][/color]输出结果为:




array(1) {

  [0]=>

  object(stdClass)#2 (4) {

    ["id"]=>

    int(1)

    ["username"]=>

    string(12) "vv4444444488"

    ["password"]=>

    string(32) "74b87337454200d4d33f80c4663dc5e5"

    ["email"]=>

    string(0) ""

  }

}



@return [color="#8B0000"]object[/color]输出结果(多余的表结构信息已去掉):




array(1) {

  [0]=>

  object(stdClass)#2 (16) {

    ["id"]=>

    string(1) "1"

    ["username"]=>

    string(12) "vv4444444488"

    ["password"]=>

    string(32) "74b87337454200d4d33f80c4663dc5e5"

    ["email"]=>

    string(0) ""

    ["_related"]=>

    array(1) {

      ["post"]=>   // <= post结果被加到"_related"属性里,如何做到可以访问$user->post

      array(1) {

        [0]=>

        object(stdClass)#12 (16) {

          ["id"]=>

          string(1) "1"

          ["title"]=>

          string(10) "test title"

          ["content"]=>

          string(4) "sdsd"

          ["author_id"]=>

          string(1) "1"

        }

      }

    }

    ["_c"]=>

    NULL

    ["_pk"]=>

    string(1) "1"

    ["_alias"]=>

    string(1) "t"

    ["_errors"]=>

    array(0) {

    }

    ["_validators"]=>

    NULL

    ["_scenario"]=>

    string(6) "update"

    ["_e"]=>

    NULL

    ["_m"]=>

    NULL

  }

}



返回的User实例里并没有"post"属性,不能实现关联查询,而且返回的对象似乎不是User,而是object(stdClass),经过多次调试,依然无果,是不是哪些地方没搞对?还是因为用了web service?

自己顶上,哪位好人帮忙看看,或者提供可行的完整的例子参考,谢谢

是因为你用了soap。你需要把Post也放到classMap里。

加上post还是一样的结果, :(




return array(

    'goods'=>array(

        'class'=>'CWebServiceAction',

        'classMap'=>array(

            'User'=>'User',

            'Post'=>'Post'

        )

    ),

);



期待高人的指点…

顶起