FOF30\Model\DataModel::find PHP Method

find() public method

Find and load a single record based on the provided key values
public find ( array | mixed $keys = null ) : static
$keys array | mixed An optional primary key value to load the row by, or an array of fields to match. If not set the "id" state variable or, if empty, the identity column's value is used
return static Self, for chaining
    public function find($keys = null)
    {
        // Execute the onBeforeLoad event
        $this->triggerEvent('onBeforeLoad', array(&$keys));
        // If we are not given any keys, try to get the ID from the state or the table data
        if (empty($keys)) {
            $id = $this->getState('id', 0);
            if (empty($id)) {
                $id = $this->getId();
            }
            if (empty($id)) {
                $this->triggerEvent('onAfterLoad', array(false, &$keys));
                $this->reset();
                return $this;
            }
            $keys = array($this->idFieldName => $id);
        } elseif (!is_array($keys)) {
            if (empty($keys)) {
                $this->triggerEvent('onAfterLoad', array(false, &$keys));
                $this->reset();
                return $this;
            }
            $keys = array($this->idFieldName => $keys);
        }
        // Reset the table
        $this->reset();
        // Get the query
        $db = $this->getDbo();
        $query = $db->getQuery(true)->select('*')->from($db->qn($this->tableName));
        // Apply key filters
        foreach ($keys as $filterKey => $filterValue) {
            if ($filterKey == 'id') {
                $filterKey = $this->getIdFieldName();
            }
            if (array_key_exists($filterKey, $this->recordData)) {
                $query->where($db->qn($filterKey) . ' = ' . $db->q($filterValue));
            }
        }
        // Get the row
        $db->setQuery($query);
        try {
            $row = $db->loadAssoc();
        } catch (\Exception $e) {
            $row = null;
        }
        if (empty($row)) {
            $this->triggerEvent('onAfterLoad', array(false, &$keys));
            return $this;
        }
        // Bind the data
        $this->bind($row);
        $this->relationManager->rebase($this);
        // Execute the onAfterLoad event
        $this->triggerEvent('onAfterLoad', array(true, &$keys));
        return $this;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Gets the list of IDs from the request data
  *
  * @param DataModel $model      The model where the record will be loaded
  * @param bool      $loadRecord When true, the record matching the *first* ID found will be loaded into $model
  *
  * @return array
  */
 public function getIDsFromRequest(DataModel &$model, $loadRecord = true)
 {
     // Get the ID or list of IDs from the request or the configuration
     $cid = $this->input->get('cid', array(), 'array');
     $id = $this->input->getInt('id', 0);
     $kid = $this->input->getInt($model->getIdFieldName(), 0);
     $ids = array();
     if (is_array($cid) && !empty($cid)) {
         $ids = $cid;
     } else {
         if (empty($id)) {
             if (!empty($kid)) {
                 $ids = array($kid);
             }
         } else {
             $ids = array($id);
         }
     }
     if ($loadRecord && !empty($ids)) {
         $id = reset($ids);
         $model->find(array('id' => $id));
     }
     return $ids;
 }