Phalcon學習-model

Model:
表與表之間的關係:
hasOne 一對一( $fields, $referenceModel, $referencedFields : 當前表中的字段, 對應關係模型, 對應關係模型中表的字字段 )
hasMany 一對多 ( $fields, $referenceModel, $referencedFields : 當前表中的字段, 對應關係模型, 對應關係模型中表的字字段 )
hasManyToMany 多對多
belongsTo 多對一( 屬於 ) ( $fields, $referenceModel, $referencedFields : 當前表中的字段, 對應關係模型, 對應關係模型中表的字字段 )
*********** 如項目中存在命名空間 則 要在對應的關係中添加alias參數 array( 'alias' => 'namespace' )數組

$this->hasMany( 'id', 'Mp\pri\models\RolesUsers' , 'roleid', array( 'alias' => 'rolesusers' ));
$this->hasMany( 'id', 'Mp\pri\models\RolesMenus' , 'roleid', array( 'alias' => 'rolesmenus' ));session

Phalcon中設置容許數據動態更新:( 初始化的時候 )app

$this->useDynamicUpdate( true ); this

Phalocn中設置軟刪除標記:( 初始化的時候 )spa

use Phalcon\Mvc\Model\Behavior\SoftDelete;
$this->addBehavior( new SoftDelete(
  array(
    'field' => 'delsign',
    'value' => SystemEnums::DELSIGN_YES,
  )
) );
$res = Roles::findFirst( $where )->delete();
//當判斷是否刪除成功與否
if( empty( $res ) )
{//delete error
}
else
{//delete success
}對象

項目多模塊並存在有命名:(跨模塊取數據)get

因類存在命名空間問題 若是保存成對象, 在取出數據的時候因存在命名空間限制 會取不到session中的數據 ------ 解決方法 將數據保存成數組存入session中it

Phalcon 添加/更新數據:io

$id = $this->request->getPost('id');

if( isset( $id ) && FALSE != $id )  {
  $where = array(
    'conditions' => 'delsign=:del: and id=:optid:',
    'bind' => array( 'del' => SystemEnums::DELSIGN_NO,'optid' => $id ),
  );
  $cache = Cache::findFirst( $where );
  $cache->delsign = SystemEnums::DELSIGN_YES;
  $cache->modtime = TimeUtils::getFullTime();
  $cache->title = 'Login';
  $cache->action = 'loadding';
  $cache->seconds = 100;
  $cache->module_name = 'appmgr';
} module

//add 
else  {
  $cache = new Cache();
  $cache->title = 'Roles';
  $cache->module_name = 'pri';
  $cache->controller = 'Roles';
  $cache->action = 'list';
  $cache->seconds = 20;
  $cache->comment = 'Add Test';
  $cache->createtime = TimeUtils::getFullTime();
  $cache->modtime = TimeUtils::getFullTime();
  $cache->delsign = SystemEnums::DELSIGN_NO;
}
if (! $cache->save()) {
  foreach ($cache->getMessages() as $message) {
    echo "Message: ", $message->getMessage();
    echo "Field: ", $message->getField();
    echo "Type: ", $message->getType();
  }
}
else {
  echo 'No Error';
}
exit;

採用PHQL方式更新數據:

$query = $this->modelsManager->createQuery( 'update \Mp\sys\Models\Cache set title =:tit:,modtime=:time: where id = :id:' ); $res = $query->execute(array(   'tit' => $cache_name,   'id' => $id,   'time' => TimeUtils::getFullTime(), ));

相關文章
相關標籤/搜索