我如何使用带有soap的cakephp调用数据库中的数据?

时间:2012-05-08 04:35:34

标签: cakephp soap wsdl

每个人

我是cakephp框架的新手。我想调用数据库中的数据 使用cakephp肥皂。

我在此链接中使用了一个组件>> http://bakery.cakephp.org/articles/char101/2009/06/05/a-component-to-help-creating-soap-services

这是我的soap.php(SOAP组件)

<?php  
App::import('Vendor', 'IPReflectionClass', array('file' => 'wshelper' . DS . 'lib' . DS . 'soap' . DS . 'IPReflectionClass.class.php'));
App::import('Vendor', 'IPReflectionCommentParser', array('file' => 'wshelper' . DS . 'lib' . DS . 'soap' . DS . 'IPReflectionCommentParser.class.php'));
App::import('Vendor', 'IPXMLSchema', array('file' => 'wshelper' . DS . 'lib' . DS . 'soap' . DS . 'IPXMLSchema.class.php'));
App::import('Vendor', 'IPReflectionMethod', array('file' => 'wshelper' . DS . 'lib' . DS . 'soap' . DS . 'IPReflectionMethod.class.php'));
App::import('Vendor', 'WSDLStruct', array('file' => 'wshelper' . DS . 'lib' . DS . 'soap' . DS . 'WSDLStruct.class.php'));
App::import('Vendor', 'WSDLException', array('file' => 'wshelper' . DS . 'lib' . DS . 'soap' . DS . 'WSDLException.class.php'));

/** 
 * Class SoapComponent 
 * 
 * Generate WSDL and handle SOAP calls 
 */ 
class SoapComponent extends Component 
{ 
    var $params = array(); 

    function initialize(&$controller) 
    { 
        $this->params = $controller->params; 
    } 

    /** 
     * Get WSDL for specified model. 
     * 
     * @param string $modelClass : model name in camel case 
     * @param string $serviceMethod : method of the controller that will handle SOAP calls 
     */ 
    function getWSDL($modelId, $serviceMethod = 'call') 
    { 
        $modelClass = $this->__getModelClass($modelId); 
        $expireTime = '+1 year'; 
        $cachePath = $modelClass . '.wsdl'; 

        // Check cache if exist 
        $wsdl = cache($cachePath, null, $expireTime); 

        // If DEBUG > 0, compare cache modified time to model file modified time 
        if ((Configure::read() > 0) && (! is_null($wsdl))) { 

            $cacheFile = CACHE . $cachePath; 
            if (is_file($cacheFile)) { 
                $modelMtime = filemtime($this->__getModelFile($modelId)); 
                $cacheMtime = filemtime(CACHE . $cachePath); 
                if ($modelMtime > $cacheMtime) { 
                    $wsdl = null; 
                } 
            } 

        } 

        // Generate WSDL if not cached 
        if (is_null($wsdl)) { 

            $refl = new IPReflectionClass($modelClass); 

            $controllerName = $this->params['controller']; 
            $serviceURL = Router::url("/$controllerName/$serviceMethod", true); 

            $wsdlStruct = new WSDLStruct('http://schema.example.com',  
                                         $serviceURL . '/' . $modelId,  
                                         SOAP_RPC,  
                                         SOAP_LITERAL); 
            $wsdlStruct->setService($refl); 
            try { 
                $wsdl = $wsdlStruct->generateDocument(); 
                // cache($cachePath, $wsdl, $expireTime); 
            } catch (WSDLException $exception) { 
                if (Configure::read() > 0) { 
                    $exception->Display(); 
                    exit(); 
                } else { 
                    return null; 
                } 
            } 
        } 

        return $wsdl; 
    } 

    /** 
     * Handle SOAP service call 
     * 
     * @param string $modelId : underscore notation of the called model 
     *                          without _service ending 
     * @param string $wsdlMethod : method of the controller that will generate the WSDL 
     */ 
    function handle($modelId, $wsdlMethod = 'wsdl') 
    { 
        $modelClass = $this->__getModelClass($modelId); 
        $wsdlCacheFile = CACHE . $modelClass . '.wsdl'; 

        // Try to create cache file if not exists 
        if (! is_file($wsdlCacheFile)) { 
            $this->getWSDL($modelId); 
        } 

        if (is_file($wsdlCacheFile)) { 
            $server = new SoapServer($wsdlCacheFile); 
        } else { 
            $controllerName = $this->params['controller']; 
            $wsdlURL = Router::url("/$controllerName/$wsdlMethod", true); 
            $server = new SoapServer($wsdlURL . '/' . $modelId); 
        } 
        $server->setClass($modelClass); 
        $server->handle(); 
    } 

    /** 
     * Get model class for specified model id 
     * 
     * @access private 
     * @return string : the model id 
     */ 
    function __getModelClass($modelId) 
    { 
        $inflector = new Inflector; 
        return ($inflector->camelize($modelId) . 'Service'); 
    } 

    /** 
     * Get model id for specified model class 
     * 
     * @access private 
     * @return string : the model id 
     */ 
    function __getModelId($modelClass) 
    { 
        $inflector = new Inflector; 
        return $inflector->underscore(substr($class, 0, -7)); 
    } 

    /** 
     * Get model file for specified model id 
     * 
     * @access private 
     * @return string : the filename 
     */ 
    function __getModelFile($modelId) 
    { 
        $modelDir = dirname(dirname(dirname(__FILE__))) . DS . 'models'; 
        return $modelDir . DS . $modelId . '_service.php'; 
    } 
} 
?>

这是我的service_controller.php

<?php  
class ServiceController extends AppController 
{ 
    public $name = 'Service'; 
    public $uses = array('TestService');
    public $helpers = array(); 
    public $components = array('Soap');


    /** 
     * Handle SOAP calls 
     */ 
    function call($model) 
    { 
        $this->autoRender = FALSE; 
        $this->Soap->handle($model, 'wsdl'); 
    } 

    /** 
     * Provide WSDL for a model 
     */ 
    function wsdl($model) 
    { 
        $this->autoRender = FALSE; 
        header('Content-Type: text/xml; charset=UTF-8'); // Add encoding if this doesn't work e.g. header('Content-Type: text/xml; charset=UTF-8');  
        echo $this->Soap->getWSDL($model, 'call'); 
    }

} 
?>

这是我的test_service.php

<?php  
class TestService extends AppModel 
{ 
    var $name = 'TestService';
    var $useTable = 'Test'; 

    /** 
    * Get one record 
    * @param string
    * @return string
    */ 
    function view() { 
        $this->set('text', $this->TestService->find('all'));
        return $text ;
    } 
}
?>

我想在数据库中调用数据,但我的代码无法从数据库调用。 这是soapUi中的我的错误

Call to a member function find() on a non-object 
Undefined property: TestService::$TestService 

1 个答案:

答案 0 :(得分:1)

更改

$this->set('text', $this->TestService->find('all'));

$this->set('text', $this->find('all'));

虽然在模型中调用->set()没有意义,但我认为您实际上想要返回:

return $this->find('all');