本文整理汇总了PHP中Model::find方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::find方法的具体用法?PHP Model::find怎么用?PHP Model::find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Model
的用法示例。
在下文中一共展示了Model::find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: depreciate
/**
* Handle depreciation
*/
public function depreciate()
{
$depreciation_id = Model::find($this->model_id)->depreciation_id;
if ($depreciation_id) {
$depreciation_term = Depreciation::find($depreciation_id)->months;
if ($depreciation_term > 0) {
$purchase_date = strtotime($this->purchase_date);
$todaymonthnumber = date("Y") * 12 + (date("m") - 1);
//calculate the month number for today as YEAR*12 + (months-1) - number of months since January year 0
$purchasemonthnumber = date("Y", $purchase_date) * 12 + (date("m", $purchase_date) - 1);
//purchase date calculated similarly
$diff_months = $todaymonthnumber - $purchasemonthnumber;
// fraction of value left
$current_value = round(($depreciation_term - $diff_months) / $depreciation_term * $this->purchase_cost, 2);
if ($current_value < 0) {
$current_value = 0;
}
return $current_value;
} else {
return $this->purchase_cost;
}
} else {
return $this->purchase_cost;
}
}
示例2: read
/**
* Method used to read from a database session.
*
* @param int|string $id The key of the value to read
* @return mixed The value of the key or false if it does not exist
*/
public function read($id)
{
$row = $this->_model->find('first', array('conditions' => array($this->_model->primaryKey => $id)));
if (empty($row[$this->_model->alias]['data'])) {
return false;
}
return $row[$this->_model->alias]['data'];
}
示例3: find
/**
* Find model by id
*
* @param int $id
* @return Response
*/
public function find($id)
{
$model = $this->model->find($id);
if (!$model) {
array_push($this->errors, 'Could not find record with id = ' . $id . '.');
return false;
}
return $model;
}
示例4: read
/**
* Method used to read from a database session.
*
* @param int|string $id The key of the value to read
* @return mixed The value of the key or false if it does not exist
*/
public function read($id)
{
$row = $this->_model->find('first', array('conditions' => array($this->_model->alias . '.' . $this->_model->primaryKey => $id)));
if (empty($row[$this->_model->alias])) {
return '';
}
if (!is_numeric($row[$this->_model->alias]['data']) && empty($row[$this->_model->alias]['data'])) {
return '';
}
return (string) $row[$this->_model->alias]['data'];
}
示例5: fixOrder
public function fixOrder(Model $model, $conditions)
{
$count = $model->find('count', array('conditions' => $conditions));
if ($count > 0) {
$list = $model->find('list', array('conditions' => $conditions, 'order' => array($model->alias . '.order')));
$order = 1;
foreach ($list as $id => $name) {
$model->id = $id;
$model->saveField('order', $order++);
}
}
}
示例6: routineDrop
/**
* routineDrop
*
*/
public function routineDrop(Model $model, $id, $conditions = array())
{
$conditions["{$model->alias}.{$model->primaryKey}"] = $id;
$current = $model->find('first', array('conditions' => $conditions));
if (empty($current)) {
throw new NotFoundException(__('Invalid Access'));
}
// for SoftDeletable
$model->set($current);
$model->delete($id);
$count = $model->find('count', array('conditions' => array("{$model->alias}.{$model->primaryKey}" => $id)));
return $count === 0;
}
示例7: duplicate
/**
* Clone model's data.
*
* @param Model $Model
* @param array $options
* @return [type]
*/
public function duplicate(Model $Model, $data, $config = array())
{
$config = array_merge($this->settings[$Model->alias], $config);
if (empty($Model->id) && empty($data[$Model->alias][$Model->primaryKey])) {
throw new Exception(__d('common', "Missing primary key for duplicatable '%s' data", $Model->name));
}
$id = $Model->id ? $Model->id : $data[$Model->alias][$Model->primaryKey];
$conditions = array($Model->primaryKey => $id) + (array) $this->settings[$Model->alias]['scope'];
if (!empty($this->settings[$Model->alias]['scope']) && !$Model->find('count', compact('conditions'))) {
return false;
}
$duplicateData = array($config['duplicatedKey'] => $id, $config['duplicatedModel'] => (!empty($Model->plugin) ? $Model->plugin . '.' : '') . $Model->name);
$DuplicateModel = ClassRegistry::init($config['model']);
$DuplicateModel->create();
$duplicateRecord = $DuplicateModel->find('first', array('conditions' => $duplicateData, 'recursive' => -1));
if (!empty($duplicateRecord)) {
$DuplicateModel->id = $duplicateRecord[$DuplicateModel->alias][$DuplicateModel->primaryKey];
}
foreach ((array) $config['mapFields'] as $field => $path) {
$value = Hash::extract($data, $path);
$duplicateData[$field] = array_pop($value);
if (!empty($duplicateRecord[$DuplicateModel->alias][$field]) && $duplicateRecord[$DuplicateModel->alias][$field] === $duplicateData[$field]) {
unset($duplicateData[$field]);
}
}
if ((empty($duplicateRecord) || 1 < count($duplicateData)) && (!empty($DuplicateModel->id) || $DuplicateModel->create($duplicateData)) && !$DuplicateModel->save()) {
return false;
}
return true;
}
示例8: find
function find($type, $options = array())
{
switch ($type) {
case 'concatList':
if (!isset($options['fields']) || count($options['fields']) < 2) {
return parent::find('list', $options);
}
if (!isset($options['separator'])) {
$options['separator'] = ' - ';
}
$options['recursive'] = -1;
$list = parent::find('all', $options);
for ($i = 1; $i <= 2; $i++) {
$field[$i] = str_replace($this->alias . '.', '', $options['fields'][$i]);
}
/*
return Set::combine($list, '{n}.'.$this->alias.'.'.$this->primaryKey,
array('%s'.$options['separator'].'%s',
'{n}.'.$this->alias.'.'.$field[1],
'{n}.'.$this->alias.'.'.$field[2]));
*/
return Set::combine($list, '{n}.' . $this->alias . '.' . $this->{$field}[1], array('%s' . $options['separator'] . '%s', '{n}.' . $this->alias . '.' . $field[1], '{n}.' . $this->alias . '.' . $field[2]));
break;
default:
return parent::find($type, $options);
break;
}
}
示例9: makeModelStateCity
public function makeModelStateCity($id)
{
$this->layout->body_class = '';
$make = Make::find(explode("-", $id)[0]);
$model = Model::find(explode("-", $id)[1]);
$state = null;
foreach ($this->getStates() as $entity) {
if (explode("-", $id)[2] == $entity['code']) {
$state = $entity;
break;
}
}
$columns = array(array(), array(), array());
$cities = DB::select(DB::raw("SELECT DISTINCT city FROM vehicle WHERE make = :make AND model = :model AND state = :state ORDER BY city"), array('make' => $make->id, 'model' => $model->id, 'state' => $state['code']));
foreach ($cities as $key => $value) {
$location = Location::where('state', '=', $state['code'])->where('city', '=', $value->city);
if ($location->count()) {
$zip = $location->first()->zip_code;
$search = $make->make . ' ' . $model->model;
$link = '/search?make=' . $make->id . '&model=' . $model->id . '&zip_code=' . $zip . '&search_text=' . $search . '&distance=50&page=1&sort=price-1';
$title = $make->make . ' ' . $model->model . ' for sale near ' . $value->city . ', ' . $state['code'];
$city = array('link' => $link, 'title' => $title);
array_push($columns[$key % 3], $city);
}
}
$data = array('search_text' => '', 'make' => $make, 'model' => $model, 'state' => $state, 'columns' => $columns);
$this->layout->contents = View::make('browse/make-model-state-city', $data);
}
示例10: find
/**
* 重构find方法,增加缓存的调用.
*
* @author 笨笨天才@20101127
*
* $params['cache']=array('use'=>true,'config'=>'short');
* short是core.php定义的Cache:Config,默认为null
*
* @param string $type 输入类型
* @param mixed $params 输入参数
* @param bool $params['cache']['use'] 是否使用缓存标志
* @param string $params['cache']['config'] 缓存类型
*
* @return mixed $data 返回值
*/
public function find($type, $params = array())
{
//!isset($params['cache'])
if (isset($params['cache']) && $params['cache']['use'] !== false) {
//判断是否不读取缓存数据
$cache_config = isset($params['cache']['config']) ? $params['cache']['config'] : null;
$model = isset($this->name) ? '_' . $this->name : 'appmodel';
$paramsHash = md5(serialize($params));
$cache_key = $model . '_' . $type . '_' . $this->locale . '_' . $paramsHash;
//根据模型名称,type参数,多语言,params组成缓存唯一序号
// echo $params['cache']['config']."----".$cache_key."<br />";
//pr($cache_config);
$data = cache::read($cache_key, $cache_config);
//print_r($params);
if ($data) {
// echo "cached:".$cache_key."<br /><pre>";
return $data;
} else {
//echo "not find cached:".$cache_key."<br /><pre>";
//print_r($params);
$data = parent::find($type, $params);
cache::write($cache_key, $data, $cache_config);
//将数据库的值写入缓存
return $data;
}
} else {
//echo "no cache:".$cache_key."<br /><pre>";
return parent::find($type, $params);
}
}
示例11: find
function find($conditions = null, $fields = array(), $order = null, $recursive = null)
{
if ($conditions != 'range') {
return parent::find($conditions, $fields, $order, $recursive);
}
$file = Set::extract($fields, 'file');
if (empty($file)) {
$file = ROOT . DS . APP_DIR . DS . 'plugins/mobileip/config/mobile_ips.yml';
}
if (!file_exists($file)) {
return false;
}
$cacheDir = $this->getCacheDir();
$folder = new Folder($cacheDir);
$folder->create($cacheDir, 0777);
$cacheFile = $this->getCacheFile();
if (file_exists($cacheFile) && $this->_getLastModified($file) <= filemtime($cacheFile)) {
return include $cacheFile;
}
$mobile_ips =& Spyc::YAMLLoad($file);
if (!is_array($mobile_ips)) {
return false;
}
$data = $this->_get_ranges($mobile_ips);
$file = new File($cacheFile, true);
$file->write($data);
$file->close();
return include $cacheFile;
}
示例12: edit
public function edit($id)
{
if (IS_POST) {
//获取数据
if ($this->model->create() !== false) {
//判断数据是否正确
if ($this->model->save() !== false) {
$this->success('修改成功', cookie('__forward__'));
return;
//防止后面代码执行
}
}
$this->error('修改失败');
} else {
//调用钩子函数
$this->_before_edit_view();
//给页面分配树的数据
$row = $this->model->find($id);
//页面分配数据
$this->assign('meta_title', '编辑' . $this->meta_title);
$this->assign('row', $row);
$this->assign('id', $id);
//加载视图
$this->display('add');
}
}
示例13: refresh
public function refresh($record = null, $alias = null)
{
if ($record) {
if (!$alias) {
$alias = $this->_Model->alias;
}
if (isset($record[$alias])) {
$this->_Record = $record[$alias];
} else {
$this->_Record = $record;
}
foreach ($this->_associations as $associationName => $association) {
if (isset($record[$associationName])) {
$association->refresh($record[$associationName]);
} else {
$association->setInitialized(false);
}
}
$this->_resetState();
} elseif (!empty($this->_Record[$this->getPrimaryKey()])) {
$record = $this->_Model->find('first', array('recursive' => -1, 'conditions' => array($this->getPrimaryKey() => $this->_Record[$this->getPrimaryKey()]), 'activeRecord' => false));
if (!$record) {
return $this;
}
$this->_Record = $record[$this->_Model->alias];
foreach ($this->_associations as $association) {
$association->setInitialized(false);
}
$this->_resetState();
}
return $this;
}
示例14: beforeDelete
/**
* Tries do delete the versions that were generated from the file associated with the record.
*
* @param Model $Model Model using this behavior
* @param boolean $cascade If true records that depend on this record will also be deleted
* @return mixed False if the operation should abort. Any other result will continue.
*/
public function beforeDelete(Model $Model, $cascade = true)
{
if (!$cascade) {
return true;
}
$result = $Model->find('first', array('conditions' => array($Model->primaryKey => $Model->id), 'fields' => array('dirname', 'basename'), 'recursive' => -1));
if (empty($result)) {
return false;
}
if (empty($result[$Model->alias]['basename']) && empty($result[$Model->alias]['dirname'])) {
return true;
}
$pattern = MEDIA_FILTER . '*' . DS . $result[$Model->alias]['dirname'] . DS;
$pattern .= pathinfo($result[$Model->alias]['basename'], PATHINFO_FILENAME) . '.*';
$files = glob($pattern);
// TODO There should be some safety precautions
/*
$versions = array_keys($Model->Generator->filter($Model, $result[$Model->alias]['basename']));
if (count($files) > count($versions)) {
$message = 'GeneratedDeletable::beforeDelete - ';
$message .= "Pattern `{$pattern}` matched more than number of versions. ";
$message .= "Failing deletion of versions and record for `Media@{$Model->id}`.";
CakeLog::write('warning', $message);
return false;
}
*/
foreach ($files as $file) {
$file = new File($file);
if (!$file->delete()) {
return false;
}
}
return true;
}
示例15: process
/**
* Main processing logic for DataTable
*
* @param mixed $object
* @param mixed $scope
*/
public function process($object = null, $scope = array())
{
if (is_array($object)) {
$scope = $object;
$object = null;
}
$settings = $this->_parseSettings($object);
if (isset($settings['scope'])) {
$scope = array_merge($settings['scope'], $scope);
}
if (isset($settings['findType'])) {
$settings['type'] = $settings['findType'];
}
$query = array_diff_key($settings, array('conditions', 'columns', 'trigger', 'triggerAction', 'viewVar', 'maxLimit'));
$total = $this->_object->find('count', $query);
$this->_sort($settings);
$this->_search($settings);
$this->_paginate($settings);
$this->settings[$this->_object->alias] = $settings;
$results = parent::paginate($this->_object, $scope);
$totalDisplayed = $this->Controller->request->params['paging'][$this->_object->alias]['count'];
$dataTableData = array('iTotalRecords' => $total, 'iTotalDisplayRecords' => $totalDisplayed, 'sEcho' => isset($this->_params['sEcho']) ? intval($this->_params['sEcho']) : 0, 'aaData' => array());
$this->Controller->viewClass = 'DataTable.DataTableResponse';
$this->Controller->set($settings['viewVar'], $results);
$this->Controller->set(compact('dataTableData'));
if (isset($settings['view'])) {
$this->Controller->view = $settings['view'];
}
}