Phalcon BeforeSave在保存之前没有收到数据?

时间:2016-05-15 18:39:39

标签: php phalcon before-save

在保存模型之前,我想检查$ CONT_CEDULA是否符合要求。如果没有,那就不要保存。但是在保存时,就好像变量$ CONT_CEDULA没有任何数据一样。我想知道我做得好还是需要其他一些事件或功能。此外,echo不输出任何数据。

beforeSave方法

public function beforeSave() {

        echo $this->$CONT_CEDULA;
        switch (strlen($this->$CONT_CEDULA)) {
        case 10:
            return validarCI($this->$CONT_CEDULA);
            break;
        case 13:
            return validarRUC($this->$CONT_CEDULA);
            break;
        default:
           echo "Numero de caracteres invalidos"  ;
            return FALSE;
    }

SpmContacto模型

 <?php

class SpmContacto extends \Phalcon\Mvc\Model {

public $CONT_CODIGO;
public $CONT_CEDULA;
public $CONT_RUCIDE;
public $CONT_NOMBRE;
public $CON_ESTADO;
public $CONT_TELEFO;
public $CONT_DIRECC;
public $CONT_AREA;
public $CONT_CARGO;
public $CONT_TIPOXX;
public $CONT_EMAIL;
public $CONT_USUARIO;
public $CONT_CLAVE;
public $CONT_CLAVEE;
public $CONT_FECNACI;
public $CONT_FECINSC;
public $CONT_TIPOCODIGO;

/**
 * Initialize method for model.
 */
public function initialize() {
    $this->setSchema("SPOLS");
    $this->hasMany('CONT_CODIGO', 'SPMREFERENCIA', 'CONT_CODIGO', array('alias' => 'SPMREFERENCIA'));
    $this->hasMany('CONT_CODIGO', 'SPTDETALLE', 'CONT_CODIGO', array('alias' => 'SPTDETALLE'));
    $this->hasMany('CONT_CODIGO', 'SPTENCABEZADO', 'CONT_CODIGO', array('alias' => 'SPTENCABEZADO'));
}

function validarCI($strCedula) {
    $suma = 0;
    $strOriginal = $strCedula;
    $intProvincia = substr($strCedula, 0, 2);
    $intTercero = $strCedula[2];
    $intUltimo = $strCedula[9];
    if (!settype($strCedula, "float"))
        return FALSE;
    if ((int) $intProvincia < 1 || (int) $intProvincia > 23)
        return FALSE;
    if ((int) $intTercero == 7 || (int) $intTercero == 8)
        return FALSE;
    for ($indice = 0; $indice < 9; $indice++) {
        //echo $strOriginal[$indice],'</br>';
        switch ($indice) {
            case 0:
            case 2:
            case 4:
            case 6:
            case 8:
                $arrProducto[$indice] = $strOriginal[$indice] * 2;
                if ($arrProducto[$indice] >= 10)
                    $arrProducto[$indice] -= 9;
                //echo $arrProducto[$indice],'</br>';
                break;
            case 1:
            case 3:
            case 5:
            case 7:
                $arrProducto[$indice] = $strOriginal[$indice] * 1;
                if ($arrProducto[$indice] >= 10)
                    $arrProducto[$indice] -= 9;
                //echo $arrProducto[$indice],'</br>';
                break;
        }
    }
    foreach ($arrProducto as $indice => $producto)
        $suma += $producto;
    $residuo = $suma % 10;
    $intVerificador = $residuo == 0 ? 0 : 10 - $residuo;
    return ($intVerificador == $intUltimo ? TRUE : FALSE);
}

function validarRUC($strRUC) {
    if (strlen($strRUC) != 13)
        return FALSE;
    $suma = 0;
    $strOriginal = $strRUC;
    $intProvincia = substr($strRUC, 0, 2);
    $intTercero = $strRUC[2];
    if (!settype($strRUC, "float"))
        return FALSE;
    if ((int) $intProvincia < 1 || (int) $intProvincia > 23)
        return FALSE;
    if ((int) $intTercero != 6 && (int) $intTercero != 9) {
        if (substr($strRUC, 10, 3) == '001')
            return validarCI(substr($strRUC, 0, 10));
        return FALSE;
    }
    if ((int) $intTercero == 6) {
        $intUltimo = $strOriginal[8];
        for ($indice = 0; $indice < 9; $indice++) {
            //echo $strOriginal[$indice],'</br>';
            switch ($indice) {
                case 0:
                    $arrProducto[$indice] = $strOriginal[$indice] * 3;
                    break;
                case 1:
                    $arrProducto[$indice] = $strOriginal[$indice] * 2;
                    break;
                case 2:
                    $arrProducto[$indice] = $strOriginal[$indice] * 7;
                    break;
                case 3:
                    $arrProducto[$indice] = $strOriginal[$indice] * 6;
                    break;
                case 4:
                    $arrProducto[$indice] = $strOriginal[$indice] * 5;
                    break;
                case 5:
                    $arrProducto[$indice] = $strOriginal[$indice] * 4;
                    break;
                case 6:
                    $arrProducto[$indice] = $strOriginal[$indice] * 3;
                    break;
                case 7:
                    $arrProducto[$indice] = $strOriginal[$indice] * 2;
                    break;
                case 8:
                    $arrProducto[$indice] = 0;
                    break;
            }
        }
    } else {
        $intUltimo = $strOriginal[9];
        for ($indice = 0; $indice < 9; $indice++) {
            //echo $strOriginal[$indice],'</br>';
            switch ($indice) {
                case 0:
                    $arrProducto[$indice] = $strOriginal[$indice] * 4;
                    break;
                case 1:
                    $arrProducto[$indice] = $strOriginal[$indice] * 3;
                    break;
                case 2:
                    $arrProducto[$indice] = $strOriginal[$indice] * 2;
                    break;
                case 3:
                    $arrProducto[$indice] = $strOriginal[$indice] * 7;
                    break;
                case 4:
                    $arrProducto[$indice] = $strOriginal[$indice] * 6;
                    break;
                case 5:
                    $arrProducto[$indice] = $strOriginal[$indice] * 5;
                    break;
                case 6:
                    $arrProducto[$indice] = $strOriginal[$indice] * 4;
                    break;
                case 7:
                    $arrProducto[$indice] = $strOriginal[$indice] * 3;
                    break;
                case 8:
                    $arrProducto[$indice] = $strOriginal[$indice] * 2;
                    break;
            }
        }
    }
    foreach ($arrProducto as $indice => $producto)
        $suma += $producto;
    $residuo = $suma % 11;
    $intVerificador = $residuo == 0 ? 0 : 11 - $residuo;
    //echo "$intVerificador == $intUltimo";
    return ($intVerificador == $intUltimo ? TRUE : FALSE);
}

function validarID($strId) {
    switch (strlen($strId)) {
        case 10:
            return validarCI($strId);
            break;
        case 13:
            return validarRUC($strId);
            break;
        default:

            return FALSE;
    }
}

public function beforeSave() {

    echo $this->$CONT_CEDULA;
    switch (strlen($this->$CONT_CEDULA)) {
        case 10:
            return validarCI($this->$CONT_CEDULA);
            break;
        case 13:
            return validarRUC($this->$CONT_CEDULA);
            break;
        default:
           echo "Numero de caracteres invalidos";
         return FALSE;
    }

     //echo $op;
}

public function getSource() {
    return 'SPM_CONTACTO';
}

public static function find($parameters = null) {
    return parent::find($parameters);
}

public static function findFirst($parameters = null) {
    return parent::findFirst($parameters);
}

1 个答案:

答案 0 :(得分:0)

默认情况下,Phalcon不会输出您回复的任何数据&#34;在你的控制器或你的模型中。简单的解决方法&#39;是die('your output');

如果您在交换机中return一个值,则无需在案例结尾添加break;

public function beforeSave() 
{
    // check the content of $this->$CONT_CEDULA
    var_dump($this->$CONT_CEDULA);
    die;

    switch (strlen($this->$CONT_CEDULA)) {
       case 10:
          return validarCI($this->$CONT_CEDULA);
       case 13:
          return validarRUC($this->$CONT_CEDULA);
       default:
         // an example to test if your code enters this case.
         die("Numero de caracteres invalidos");
         return FALSE;
   }
}