當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Table::query方法代碼示例

本文整理匯總了PHP中Cake\ORM\Table::query方法的典型用法代碼示例。如果您正苦於以下問題:PHP Table::query方法的具體用法?PHP Table::query怎麽用?PHP Table::query使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Cake\ORM\Table的用法示例。


在下文中一共展示了Table::query方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: moveNode

 /**
  * Move a node under the same parent node or under a new node.
  * New position of the node can be specified
  *
  * @param int $id ID of the node to move
  * @param int $parent_id ID of the (new) parent node
  * @param int $position New position of the node. Position is zero based.
  * @return boolean
  */
 public function moveNode($id, $parent_id, $position = null)
 {
     $primaryKey = $this->_table->schema()->primaryKey();
     $primaryKey = count($primaryKey) == 1 ? $primaryKey[0] : $primaryKey;
     $parent_id_fieldname = $this->config('model_parent_id_fieldname');
     $sort_fieldname = $this->config('model_sort_fieldname');
     $connection = $this->_table->connection();
     $connection->begin();
     $result = true;
     /*
      * Get moved node
      */
     $node = $this->_table->get($id);
     /*
      * Get current nodes positions of (new) siblings
      */
     $current_children = $this->_table->query()->where([$parent_id_fieldname => $parent_id])->order([$sort_fieldname => 'asc']);
     $new_sort_children = [];
     foreach ($current_children as $current_position => $current_child) {
         if ($current_child->{$primaryKey} != $id) {
             $new_sort_children[] = $current_child;
         }
     }
     /*
      * Default position is after all siblings
      */
     $position = isset($position) ? $position : $current_children->count();
     $position = $position >= 0 ? $position : 0;
     $position = $position <= count($new_sort_children) ? $position : count($new_sort_children);
     /*
      * Insert moved node at position
      */
     array_splice($new_sort_children, $position, 0, array($node));
     /*
      * If node has a new parent -> save it
      */
     if ($node->{$parent_id_fieldname} != $parent_id) {
         $query = $this->_table->query()->update()->set([$parent_id_fieldname => $parent_id])->where([$primaryKey => $id]);
         if (!$query->execute()) {
             $result = false;
         }
     }
     /*
      * Update positions
      */
     foreach ($new_sort_children as $index => $new_sort_child) {
         $query = $this->_table->query()->update()->set([$sort_fieldname => $index * 10])->where([$primaryKey => $new_sort_child->{$primaryKey}]);
         if (!$query->execute()) {
             $result = false;
         }
     }
     /***********/
     if ($result) {
         $connection->commit();
     } else {
         $connection->rollback();
     }
     return $result;
 }
開發者ID:alaxos,項目名稱:cakephp3-libs,代碼行數:68,代碼來源:AncestorBehavior.php

示例2: _sync

 /**
  * Auxiliary function used to automatically alter the value of both the left and
  * right columns by a certain amount that match the passed conditions
  *
  * @param int $shift the value to use for operating the left and right columns
  * @param string $dir The operator to use for shifting the value (+/-)
  * @param string $conditions a SQL snipped to be used for comparing left or right
  * against it.
  * @param bool $mark whether to mark the updated values so that they can not be
  * modified by future calls to this function.
  * @return void
  */
 protected function _sync($shift, $dir, $conditions, $mark = false)
 {
     $config = $this->config();
     foreach ([$config['left'], $config['right']] as $field) {
         $query = $this->_scope($this->_table->query());
         $mark = $mark ? '*-1' : '';
         $template = sprintf('%s = (%s %s %s)%s', $field, $field, $dir, $shift, $mark);
         $query->update()->set($query->newExpr()->add($template));
         $query->where("{$field} {$conditions}");
         $query->execute();
     }
 }
開發者ID:ripzappa0924,項目名稱:carte0.0.1,代碼行數:24,代碼來源:TreeBehavior.php


注:本文中的Cake\ORM\Table::query方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。