Seo tools

Neo Inspiration

  • Search

    • About Me

      • inspi 改め
        jakk@webアーキテクト(自称)
        php,javascript,seoなど
        得意技は extract();



  • Categories

  • Ranking

  • Comments

  • Others


  • cakephp find findall read の違い

    すっかりだまされていました。
    最小単位である read が一番早いと思っていたんですが。。

    ソースがこんなになってました。
    model_php4.php

    function findAll($conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {

     $db =& ConnectionManager::getDataSource($this->useDbConfig);
     $this->id = $this->getID();
     $offset = null;

     if ($page > 1 && $limit != null) {
      $offset = ($page - 1) * $limit;
     }

     if ($order == null) {
      $order = array();
     } else {
      $order = array($order);
     }

     $queryData = array(’conditions’ => $conditions,
                ’fields’ => $fields,
                ’joins’ => array(),
                ’limit’ => $limit,
                ’offset’ => $offset,
                ’order’ => $order
     );

     $ret = $this->beforeFind($queryData);
     if (is_array($ret)) {
      $queryData = $ret;
     } elseif ($ret === false) {
     return null;
     }

     $return = $this->afterFind($db->read($this, $queryData, $recursive));

     if (!empty($this->__backAssociation)) {
      $this->__resetAssociations();
     }

     return $return;
    }

    function find($conditions = null, $fields = null, $order = null, $recursive = null) {
     $data = $this->findAll($conditions, $fields, $order, 1, null, $recursive);

     if (empty($data[0])) {
      return false;
     }

     return $data[0];
    }

    function read($fields = null, $id = null) {
     $this->validationErrors = array();

     if ($id != null) {
      $this->id = $id;
     }

     $id = $this->id;

     if (is_array($this->id)) {
      $id = $this->id[0];
     }

     if ($this->id !== null && $this->id !== false) {
        $db =& ConnectionManager::getDataSource($this->useDbConfig);
        $field = $db->name($this->name) . ‘.’ . $db->name($this->primaryKey);
        return $this->find($field . ‘ = ‘ . $db->value($id, $this->getColumnType($this->primaryKey)), $fields);
     } else {
        return false;
     }
    }

    そう 実はおおもとのDBアクセスは findAll() なんです。。。
    read にいたっては
    read() にきたものをバラして find() に投げて
    find() はそれをさらに解析して findall に投げるわけです。

    いや だったら使い分けないで全部 findAll で細かく指定するよ。。。

    ちなみに findcount() も findAll() してます。
    findAllBy とか findBy 系はどうなんだろう。
    他のとこで camelclass をばらしてるのでチョット動き違うかもしれません。


    Leave a Reply

    2008/11/22 04:18:18