Yii Framework Forum: Soap Client And Beforesave Behavior Problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Soap Client And Beforesave Behavior Problem Rate Topic: -----

#1 User is offline   srizzi72 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 29-January 13
  • Location:Italy

Posted 01 February 2013 - 03:45 AM

Hi all,
I'm adding a web service to my Yii 1.1.13 application.
I have a model Persona with behavior afterFind and beforeSave. Using the browser, code inside those functions is processed, instead via SOAP client, is processed just the afterFind and not the beforeSave. This is my code:

model Persona:
class Persona extends ActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return '{{persona}}';
    }

    // and so on...

}


ActiveRecord:
abstract class ActiveRecord extends CActiveRecord
{
    protected function afterValidate() {
        $this->update_persona_id = Yii::app()->user->id;
        return parent::afterValidate();
    }

    public function behaviors()
    {
        return array(
            'activeRecordBehavior' => array(
                'class' => 'ActiveRecordBehavior',
            )
        );
    }
}


ActiveRecordBehavior:
class ActiveRecordBehavior extends CActiveRecordBehavior
{
    public function afterFind($event)
    {
        // just to do something
        $fp = fopen('afterFind.txt', 'a');
        fwrite($fp, "\n\n----------\n");
        fwrite($fp, print_r($event, true));
        fclose($fp);
    }

    public function beforeSave($event)
    {
        // just to do something
        $fp = fopen('beforeSave.txt', 'a');
        fwrite($fp, "\n\n----------\n");
        fwrite($fp, print_r($event, true));
        fclose($fp);
    }
}


Web service controller PersonaleController:
class PersonaleController extends WSController
{
    public function actions()
    {
        return CMap::mergeArray(
            parent::actions(),
            array(
                'api' => array(
                    'classMap' => array(
                        'WSPersona' => 'WSPersona'
                    ),
                ),
            )
        );
    }

    /**
     * Returns all Persona records
     * @return WSPersona[] the Persona records
     * @soap
     */
    public function getPersone()
    {
        return WSPersona::model()->findAll();
    }

    /**
     * Update or insert a persona.
     * If the ID is null, an insertion will be performed otherwise it updates the existing one.
     * @param Persona $persona the persona model
     * @return boolean whether saving is successful
     * @soap
     */
    public function savePersona($persona)
    {
        if ($persona->id > 0) // update
        {
            $persona->isNewRecord = false;
            if( ($dbPersona = Persona::model()->findByPk($persona->id)) !== null )
            {
                $dbPersona->attributes = $persona->attributes;
                return $dbPersona->save();
            }
            else
                return false;
        }
        else
        {
            $persona->isNewRecord = true;
            $persona->id = null;
            return $persona->save();
        }
    }
}

class WSPersona extends Persona
{
    /**
     * @var integer persona id
     * @soap
     */
    public $id;

    /**
     * @var string persona cognome
     * @soap
     */
    public $cognome;

    /**
     * @var string persona nome
     * @soap
     */
    public $nome;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Persona the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}


WSController:
abstract class WSController extends CController implements IWebServiceProvider
{
    public function actions()
    {
        return array(
            'api' => array(
                'class' => 'CWebServiceAction',
            ),
        );
    }

    /**
     * This method is required by IWebServiceProvider.
     * It makes sure the user is logged in.
     * @param CWebService $service the currently requested Web service.
     * @return boolean whether the remote method should be executed.
     */
    public function beforeWebMethod($service)
    {
        $safeMethods = array(
            'login',
        );
        $pattern = '/^(' . implode('|', $safeMethods) . ')$/i';
        if (!Yii::app()->user->isGuest || preg_match($pattern, $service->methodName))
            return true;
        else
            throw new CException('Login required.');
    }

    /**
     * This method is required by IWebServiceProvider.
     * @param CWebService $service the currently requested Web service.
     */
    public function afterWebMethod($service)
    {
    }

    /**
     * Authenticate the user to act via Web service.
     * @param string $username
     * @param string $password
     * @return boolean whether login is valid
     * @soap
     */
    public static function login($username, $password)
    {
        $identity = new UserIdentity($username, $password);
        if ($identity->authenticate())
            Yii::app()->user->login($identity);

        return $identity->isAuthenticated;
    }
}


Via browser, getting (index, view) and setting (create, update) a Persona, files afterFind.txt and beforeSave.txt are created, so the behavior seems to be written correctly and working.

I have a SOAP client (a php script using SoapClient running from another machine) that perform:
getPersone, and the afterFind.txt file is created on the webserver, all persona records is retrieved correctly;
savePersona, and the beforeSave.txt file is NOT created on the webserver, but the new Persona is successfully written in the database.

Is it a bug of Yii behavior? Is mine (probably as I'm new in this world)?
Can you help me please?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users