2014年7月31日星期四

Druapl6给CCK的field增加attributes属性

首先调用hook_form_alter
使用form的pre_render属性,调用方法
参考以下例子
 

/** 
 * hook_form_alter
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'mymodule_node_form') {
    $form['field_part_1']['#pre_render'] = array('field_part_1_readonly');
    $form['field_part_2']['#pre_render'] = array('field_part_2_disabled');
    $form['field_part_3']['#pre_render'] = array('field_part_3_sort');
  }
}

/**
 * Helper function to readonly the element.
 */
function field_part_1_readonly($element) {
  $element[0]['value']['#attributes'] = array('readonly' => 'readonly', 'class' => 'readonly');
  return $element;
}

/**
 * Helper function to disable the element.
 */
function field_part_2_disabled($element) {
  $element['nid']['nid']['#attributes'] = array('disabled' => 'disabled');
  return $element;
}

/**
 * Helper function to ksort the element.
 */
function field_part_3_sort($element) {
  ksort($element['nid']['nid']['#options']);
  return $element;
}

2014年7月29日星期二

mysql 联合索引详解

联合索引又叫复合索引。对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分。例如索引是key index (a,b,c). 可以支持a | a,b| a,b,c 3种组合进行查找,但不支持 b,c进行查找 .当最左侧字段是常量引用时,索引就十分有效。

两个或更多个列上的索引被称作复合索引。
利用索引中的附加列,您可以缩小搜索的范围,但使用一个具有两列的索引 不同于使用两个单独的索引。复合索引的结构与电话簿类似,人名由姓和名构成,电话簿首先按姓氏对进行排序,然后按名字对有相同姓氏的人进行排序。如果您知 道姓,电话簿将非常有用;如果您知道姓和名,电话簿则更为有用,但如果您只知道名不姓,电话簿将没有用处。
所以说创建复合索引时,应该仔细考虑列的顺序。对索引中的所有列执行搜索或仅对前几列执行搜索时,复合索引非常有用;仅对后面的任意列执行搜索时,复合索引则没有用处。
如:建立 姓名、年龄、性别的复合索引。

create table test(
a int,
b int,
c int,
KEY a(a,b,c)
);

优: select * from test where a=10 and b=50
差: select * from test where b=50

优: select * from test order by a
差: select * from test order by b
差: select * from test order by c

优: select * from test where a=10 order by a
优: select * from test where a=10 order by b
差: select * from test where a=10 order by c

优: select * from test where a=10 order by a
差: select * from test where a=10 order by b
差: select * from test where a=10 order by c

优: select * from test where a=10 and b=10 order by a
优: select * from test where a=10 and b=10 order by b
优: select * from test where a=10 and b=10 order by c

优: select * from test where a=10 and b=10 order by a
优: select * from test where a=10 and b=10 order by b
差: select * from test where a=10 and b=10 order by c


索引原则

1.索引越少越好
原因:主要在修改数据时,每个索引都要进行更新,降低写速度。
2.最窄的字段放在键的左边
3.避免file sort排序,临时表和表扫描.

mysql explain 每列说明

explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。

使用方法,在select语句前加上explain就可以了:

如:

    explain select surname,first_name form a,b where a.id=b.id

EXPLAIN列的解释:

table:显示这一行的数据是关于哪张表的

type:这是重要的列,显示连接使用了何种类型。从最好到最差的连接类型为const、eq_reg、ref、range、indexhe和ALL

possible_keys:显示可能应用在这张表中的索引。如果为空,没有可能的索引。可以为相关的域从WHERE语句中选择一个合适的语句

key: 实际使用的索引。如果为NULL,则没有使用索引。很少的情况下,MYSQL会选择优化不足的索引。这种情况下,可以在SELECT语句中使用USE INDEX(indexname)来强制使用一个索引或者用IGNORE INDEX(indexname)来强制MYSQL忽略索引

key_len:使用的索引的长度。在不损失精确性的情况下,长度越短越好

ref:显示索引的哪一列被使用了,如果可能的话,是一个常数

rows:MYSQL认为必须检查的用来返回请求数据的行数

Extra:关于MYSQL如何解析查询的额外信息。将在表4.3中讨论,但这里可以看到的坏的例子是Using temporary和Using filesort,意思MYSQL根本不能使用索引,结果是检索会很慢

extra列返回的描述的意义

Distinct:一旦MYSQL找到了与行相联合匹配的行,就不再搜索了

Not exists: MYSQL优化了LEFT JOIN,一旦它找到了匹配LEFT JOIN标准的行,就不再搜索了

Range checked for each Record(index map:#):没有找到理想的索引,因此对于从前面表中来的每一个行组合,MYSQL检查使用哪个索引,并用它来从表中返回行。这是使用索引的最慢的连接之一

Using filesort: 看到这个的时候,查询就需要优化了。MYSQL需要进行额外的步骤来发现如何对返回的行排序。它根据连接类型以及存储排序键值和匹配条件的全部行的行指针来排序全部行

Using index: 列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,这发生在对表的全部的请求列都是同一个索引的部分的时候

Using temporary 看到这个的时候,查询需要优化了。这里,MYSQL需要创建一个临时表来存储结果,这通常发生在对不同的列集进行ORDER BY上,而不是GROUP BY上

Where used 使用了WHERE从句来限制哪些行将与下一张表匹配或者是返回给用户。如果不想返回表中的全部行,并且连接类型ALL或index,这就会发生,或者是查询有问题不同连接类型的解释(按照效率高低的顺序排序)

system 表只有一行:system表。这是const连接类型的特殊情况

const:表中的一个记录的最大值能够匹配这个查询(索引可以是主键或惟一索引)。因为只有一行,这个值实际就是常数,因为MYSQL先读这个值然后把它当做常数来对待

eq_ref:在连接中,MYSQL在查询时,从前面的表中,对每一个记录的联合都从表中读取一个记录,它在查询使用了索引为主键或惟一键的全部时使用

ref:这个连接类型只有在查询使用了不是惟一或主键的键或者是这些类型的部分(比如,利用最左边前缀)时发生。对于之前的表的每一个行联合,全部记录都将从表中读出。这个类型严重依赖于根据索引匹配的记录多少—越少越好

range:这个连接类型使用索引返回一个范围中的行,比如使用>或<查找东西时发生的情况

index: 这个连接类型对前面的表中的每一个记录联合进行完全扫描(比ALL更好,因为索引一般小于表数据)

ALL:这个连接类型对于前面的每一个记录联合进行完全扫描,这一般比较糟糕,应该尽量避免

2014年7月10日星期四

Drupal theme_table order by field 不能正常使用

Drupal version 6.30


以下代码API地址
https://api.drupal.org/api/drupal/includes!tablesort.inc/function/tablesort_header/6
/**
 * Format a column header.
 *
 * If the cell in question is the column header for the current sort criterion,
 * it gets special formatting. All possible sort criteria become links.
 *
 * @param $cell
 *   The cell to format.
 * @param $header
 *   An array of column headers in the format described in theme_table().
 * @param $ts
 *   The current table sort context as returned from tablesort_init().
 * @return
 *   A properly formatted cell, ready for _theme_table_cell().
 */
function tablesort_header($cell, $header, $ts) {
  // Special formatting for the currently sorted column header.
  if (is_array($cell) && isset($cell['field'])) {
    $title = t('sort by @s', array('@s' => $cell['data']));
    if ($cell['data'] == $ts['name']) {
      $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
      if (isset($cell['class'])) {
        $cell['class'] .= ' active';
      }
      else {
        $cell['class'] = 'active';
      }
      $image = theme('tablesort_indicator', $ts['sort']);
    }
    else {
      // If the user clicks a different header, we want to sort ascending initially.
      $ts['sort'] = 'asc';
      $image = '';
    }

    if (!empty($ts['query_string'])) {

      $ts['query_string'] = '&'. $ts['query_string'];
    }
    $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], 'html' => TRUE));

    unset($cell['field'], $cell['sort']);

  }
  return $cell;
}


这里的   
$cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], 'html' => TRUE));

'&order='. urlencode($cell['data']) . $ts['query_string']

$cell['data']是header里面的data,而不是field。


不改核心代码的情况下解决办法
$order = trim($_GET['order']) ? trim($_GET['order']) : 'n.a';
$sort = trim($_GET['sort']) ? trim($_GET['sort']) : 'asc';

$header = array(
    array('data' => t('A1'), 'field' => 'n.a', 'sort' => 'asc'),
    array('data' => t('A2'), 'field' => 'n.b', 'sort' => 'asc'),
    array('data' => t('A3'), 'field' => 'n.c', 'sort' => 'asc'),
    array('data' => t('A4'), 'field' => 'n.d', 'sort' => 'asc'),
  );
  foreach ($header as $item) {
    if ($order == $item['data'] || $order == $item['field']) {
      $order = $item['field'];
    }
  }
  $sql .= " ORDER BY %s %s  ";
  $args[] = $order;
  $args[] = $sort;





上海松善实业有限公司

    上海松善实业有限公司是一家集多品牌销售于一体的电线电缆骨干企业,公司成立于2016年。 公司拥有国内各大品牌:起帆、远东、上上、江南、胜华等。     主要产品有:高低压电力电缆、橡套电缆、控制电缆、架空绝缘电缆、塑胶电缆、电子计算机电缆、通讯电缆、...