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(), ));

相关文章
相关标签/搜索