本文整理汇总了PHP中Model::exists方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::exists方法的具体用法?PHP Model::exists怎么用?PHP Model::exists使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Model
的用法示例。
在下文中一共展示了Model::exists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: beforeSave
/**
* Callback
*
* Requires `file` field to be present if a record is created.
*
* Handles deletion of a record and corresponding file if the `delete` field is
* present and has not a value of either `null` or `'0'.`
*
* Prevents `dirname`, `basename`, `checksum` and `delete` fields to be written to
* database.
*
* Parses contents of the `file` field if present and generates a normalized path
* relative to the path set in the `baseDirectory` option.
*
* @param Model $Model
* @return boolean
*/
function beforeSave(&$Model)
{
if (!$Model->exists()) {
if (!isset($Model->data[$Model->alias]['file'])) {
unset($Model->data[$Model->alias]);
return true;
}
} else {
if (isset($Model->data[$Model->alias]['delete']) && $Model->data[$Model->alias]['delete'] !== '0') {
$Model->delete();
unset($Model->data[$Model->alias]);
return true;
}
}
$blacklist = array('dirname', 'basename', 'checksum', 'delete');
$whitelist = array('id', 'file', 'model', 'foreign_key', 'created', 'modified', 'alternative');
foreach ($Model->data[$Model->alias] as $key => $value) {
if (in_array($key, $whitelist)) {
continue;
}
if (in_array($key, $blacklist)) {
unset($Model->data[$Model->alias][$key]);
}
}
extract($this->settings[$Model->alias]);
if (isset($Model->data[$Model->alias]['file'])) {
$File = new File($Model->data[$Model->alias]['file']);
/* `baseDirectory` may equal the file's directory or use backslashes */
$dirname = substr(str_replace(str_replace('\\', '/', $baseDirectory), null, str_replace('\\', '/', Folder::slashTerm($File->Folder->pwd()))), 0, -1);
$result = array('dirname' => $dirname, 'basename' => $File->name);
$Model->data[$Model->alias] = array_merge($Model->data[$Model->alias], $result);
}
return true;
}
示例2: logicalDelete
/**
* 指定されたレコードの論理削除を行う
* @param Model $Model 削除を行うモデルのインスタンス
* @param type $id 削除レコードのID
* @return boolean 論理削除の結果(true:成功、false:失敗)
* @throws NotFoundException 指定されたデータが存在しない
* @throws FatalErrorException テーブルに削除フラグが無い
*/
public function logicalDelete(Model $Model, $id = null)
{
if (empty($id)) {
$id = $Model->id;
}
// 対象データが存在するか
if (!$Model->exists($id)) {
throw new NotFoundException('そんなIDないよ');
}
// このテーブルに削除フラグがあるか
$tableConst = $Model->getDataSource()->describe($Model);
//debug($tableConst);
if (!isset($tableConst['delete_flg'])) {
throw new FatalErrorException('削除フラグがないよ');
}
// 更新データ組立
$data = array($Model->alias => array('id' => $id, 'delete_flg' => true));
//debug($data);
// 更新
$sts = $Model->save($data, array('validate' => false));
if ($sts === false) {
return false;
} else {
return true;
}
}
示例3: changeStatus
public function changeStatus(Model $Model, $status, $id = null, $force = false)
{
if ($id === null) {
$id = $Model->getID();
}
if ($id === false) {
return false;
}
$force = true;
$Model->id = $id;
if (!$Model->exists()) {
throw new NotFoundException();
}
if ($force !== true) {
$modelData = $Model->read();
if ($modelData[$Model->alias]['status'] === $status) {
CakeLog::write(LOG_WARNING, __d('webshop', 'The status of %1$s with id %2$d is already set to %3$s. Not making a change', strtolower(Inflector::humanize(Inflector::underscore($Model->name))), $id, $status), array('webshop'));
return false;
}
} else {
CakeLog::write(LOG_WARNING, __d('webshop', 'Status change of %1$s with id %2$d is being forced to %3$s', strtolower(Inflector::humanize(Inflector::underscore($Model->name))), $id, $status), array('webshop'));
}
$Model->saveField('status', $status);
CakeLog::write(LOG_INFO, __d('webshop', 'Changed status of %1$s with id %2$d to %3$s', strtolower(Inflector::humanize(Inflector::underscore($Model->name))), $id, $status), array('webshop'));
$eventData = array();
$eventData[Inflector::underscore($Model->name)]['id'] = $id;
$eventData[Inflector::underscore($Model->name)]['status'] = $status;
$overallEvent = new CakeEvent($Model->name . '.statusChanged', $this, $eventData);
$specificEvent = new CakeEvent($Model->name . '.statusChangedTo' . Inflector::camelize($status), $this, $eventData);
CakeEventManager::instance()->dispatch($overallEvent);
CakeEventManager::instance()->dispatch($specificEvent);
return true;
}
示例4: beforeSave
/**
* beforeSave is called before a model is saved. Returning false from a beforeSave callback
* will abort the save operation.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False if the operation should abort. Any other result will continue.
* @see Model::save()
*/
public function beforeSave(Model $model, $options = array())
{
if (!empty($model->hasMany['Audit'])) {
if ($model->id = !empty($model->id) ? $model->id : (!empty($model->data[$model->alias]['id']) ? $model->data[$model->alias]['id'] : '')) {
$this->data[$model->id]['Audit']['entity'] = (!empty($model->plugin) ? $model->plugin . '.' : '') . $model->name;
$this->data[$model->id]['Audit']['entity_id'] = $model->id;
$this->data[$model->id]['Audit']['event'] = $model->exists() ? 'UPDATE' : 'CREATE';
$this->data[$model->id]['Audit']['new'] = $this->data[$model->id]['Audit']['old'] = array();
$this->data[$model->id]['Audit']['creator_id'] = !empty($model->data['Creator']['id']) ? $model->data['Creator']['id'] : null;
$model->data = Hash::remove($model->data, 'Creator');
if ($this->data[$model->id]['Audit']['event'] == 'CREATE') {
$this->data[$model->id]['Audit']['old'] = json_encode(array());
$this->data[$model->id]['Audit']['new'] = json_encode($model->data[$model->alias]);
} else {
if ($this->data[$model->id]['Audit']['event'] == 'UPDATE') {
$old = $model->find('first', array('conditions' => array($model->alias . '.id' => $model->id), 'recursive' => -1));
foreach ($model->data[$model->alias] as $field => $value) {
if ($field == 'updated') {
continue;
}
if ($model->hasField($field) && $old[$model->alias][$field] != $value) {
$this->data[$model->id]['Audit']['old'][$field] = $old[$model->alias][$field];
$this->data[$model->id]['Audit']['new'][$field] = $value;
}
}
if (!empty($this->data[$model->id]['Audit']['old']) && !empty($this->data[$model->id]['Audit']['new'])) {
$this->data[$model->id]['Audit']['old'] = json_encode($this->data[$model->id]['Audit']['old']);
$this->data[$model->id]['Audit']['new'] = json_encode($this->data[$model->id]['Audit']['new']);
}
}
}
}
}
return parent::beforeSave($model, $options);
}
示例5: exists
public function exists($id = null)
{
if ($this->Behaviors->attached('SoftDelete')) {
return $this->existsAndNotDeleted($id);
} else {
return parent::exists($id);
}
}
示例6: active
/**
* Informa se o registro existe e esta ativo (não deletado).
* Caso modelo não tenha o campo relacionado ao SoftDelete, apenas
* retorna um Model::exists para o registro.
*
* @param Model $Model
* @param int $id ID do registro
* @return bool
*/
public function active($Model, $id)
{
$schema = $Model->schema();
if (!isset($schema[$this->settings[$Model->alias]['field']])) {
return $Model->exists($id);
}
return (bool) $Model->find('count', array('conditions' => array($Model->alias . '.' . $Model->primaryKey => $id, $Model->alias . '.' . $this->settings[$Model->alias]['field'] => false), 'recursive' => -1, 'callbacks' => false));
}
示例7: __autoload
function __autoload($modelname)
{
if (Model::exists($modelname)) {
include Model::getFullPath($modelname);
}
if (Form::exists($modelname)) {
include Form::getFullPath($modelname);
}
}
示例8: beforeSave
/**
* Callback
*
* Adds metadata to be stored in table if a record is about to be created.
*
* @param Model $Model
* @return boolean
*/
function beforeSave(&$Model)
{
if ($Model->exists() || !isset($Model->data[$Model->alias]['file'])) {
return true;
}
extract($this->settings[$Model->alias]);
$Model->data[$Model->alias] += $this->metadata($Model, $Model->data[$Model->alias]['file'], $level);
return true;
}
示例9: isLikedBy
public function isLikedBy(Model $Model, $article_id, $user_id)
{
// If the item does not exist
if (!$Model->exists($article_id)) {
throw new NotFoundException();
}
$count = $Model->Like->find('count', array('conditions' => array('Like.article_id' => $article_id, 'Like.user_id' => $user_id)));
return $count == 1;
}
示例10: deactivate
/**
* @param Model $Model
* @param $id
*
* @return bool
*/
public function deactivate(Model $Model, $id)
{
if (empty($id)) {
return false;
}
$Model->id = $id;
if (!$Model->exists()) {
return false;
}
if ($Model->saveField('active', false)) {
return true;
}
return false;
}
示例11: beforeSave
function beforeSave(Model $model)
{
if ($this->settings[$model->alias]['has_created_by'] || $this->settings[$model->alias]['has_modified_by']) {
$AuthSession = $this->settings[$model->alias]['auth_session'];
$UserSession = $this->settings[$model->alias]['user_model'];
$userId = Set::extract($_SESSION, $AuthSession . '.' . $UserSession . '.' . 'id');
if ($userId) {
$data = array($this->settings[$model->alias]['modified_by_field'] => $userId);
if (!$model->exists()) {
$data[$this->settings[$model->alias]['created_by_field']] = $userId;
}
$model->set($data);
}
}
return true;
}
示例12: beforeSave
/**
* beforeSave
*
* @param Model $model
* @param Model $options
* @return void
* @access public
*/
function beforeSave(&$model, $options)
{
if (!$model->exists()) {
$ret = $this->PluginContent->find(array('PluginContent.name' => $model->data[$model->alias]['name']));
if ($ret) {
// 新規登録で既に登録されている場合は、重複エラーとする
$model->invalidate('name', '既に登録されています。');
return false;
}
$pluginContent = $this->_generatePluginContentData($model);
$this->PluginContent->create($pluginContent);
} else {
$pluginContent = $this->_generatePluginContentData($model, $model->data[$model->alias]['id']);
$this->PluginContent->set($pluginContent);
}
// バリデーション
return $this->PluginContent->validates();
}
示例13: beforeSave
/**
* Before save callback
*
* @param object $model Model using this behavior
* @return boolean True if the operation should continue, false if it should abort
* @access public
*/
function beforeSave(Model $model, $options = array())
{
//if($model->alias != 'History' && $model->alias != 'User' && $model->alias != 'UserSession')$this->redirect(array('controller'=>'user','action'=>'dashboard'));
//$this->redirect(array('action'=>'index'));
if ($this->settings[$model->alias]['has_created_by'] || $this->settings[$model->alias]['has_modified_by']) {
// We can't use this as there is no Auth / Sessions in CMS,
$AuthSession = $this->settings[$model->alias]['auth_session'];
$UserSession = $this->settings[$model->alias]['user_model'];
if (isset($_SESSION['User'])) {
$userId = $_SESSION['User']['id'];
$dept_id = $_SESSION['User']['department_id'];
$bran_id = $_SESSION['User']['branch_id'];
$comp_id = $_SESSION['User']['company_id'];
if ($userId) {
$data = array($this->settings[$model->alias]['modified_by_field'] => $userId);
if (!$model->exists()) {
$data[$this->settings[$model->alias]['created_by_field']] = $userId;
$data[$this->settings[$model->alias]['department_id_field']] = $dept_id;
$data[$this->settings[$model->alias]['branch_id_field']] = $bran_id;
$data[$this->settings[$model->alias]['company_id_field']] = $comp_id;
}
$model->set($data);
}
} else {
$userId = '0';
$dept_id = '0';
$bran_id = '0';
$comp_id = '0';
if ($userId) {
$data = array($this->settings[$model->alias]['modified_by_field'] => $userId);
if (!$model->exists()) {
$data[$this->settings[$model->alias]['created_by_field']] = $userId;
$data[$this->settings[$model->alias]['department_id_field']] = $dept_id;
$data[$this->settings[$model->alias]['branch_id_field']] = $bran_id;
$data[$this->settings[$model->alias]['company_id_field']] = $comp_id;
}
$model->set($data);
}
}
}
return true;
}
示例14: edit
public function edit(Model $model, $id = 0)
{
$model->render = 'auto';
if ($model->exists($id)) {
$this->recursive = 0;
$data = $model->findById($id);
if ($model->request->is(array('post', 'put'))) {
if ($model->save($model->request->data)) {
$model->Message->alert('general.edit.success');
return $model->redirect(array('action' => get_class($model), 'view', $id));
} else {
$model->Message->alert('general.edit.failed');
}
} else {
$model->request->data = $data;
}
} else {
$model->Message->alert('general.view.notExists');
return $model->redirect(array('action' => get_class($model)));
}
}
示例15: isOwner
/**
* [isOwner description]
*
* @param Model $Model Model using this behavior.
* @return boolean
*/
public function isOwner(Model $Model)
{
if (!$Model->exists()) {
return false;
}
if (!($owner = Hash::get($Model->data, $Model->alias . '.' . $this->settings[$Model->alias]['owner']['path']))) {
$requireAuth = $Model->requireAuth;
$Model->requireAuth = false;
$owner = $Model->field($this->settings[$Model->alias]['owner']['path'], array($Model->alias . '.' . $Model->primaryKey => $Model->getID()));
$Model->requireAuth = $requireAuth;
}
return $this->getCurrentUser($Model, $this->settings[$Model->alias]['owner']['auth']) == $owner;
}