Bluz\Db\Table::find PHP Method

find() public static method

This method accepts a variable number of arguments. If the table has a multi-column primary key, the number of arguments must be the same as the number of columns in the primary key. To find multiple rows in a table with a multi-column primary key, each argument must be an array with the same number of elements. The find() method always returns a array Row by primary key, return array Table::find(123); Row by compound primary key, return array Table::find([123, 'abc']); Multiple rows by primary key Table::find(123, 234, 345); Multiple rows by compound primary key Table::find([123, 'abc'], [234, 'def'], [345, 'ghi'])
public static find ( $keys ) : array
$keys The value(s) of the primary keys.
return array
    public static function find(...$keys)
    {
        $self = static::getInstance();
        $keyNames = array_values((array) $self->primary);
        $whereList = [];
        foreach ($keys as $keyValues) {
            $keyValues = (array) $keyValues;
            if (count($keyValues) < count($keyNames)) {
                throw new InvalidPrimaryKeyException("Too few columns for the primary key.\n" . "Please check " . static::class . " initialization or usage.\n" . "Settings described at https://github.com/bluzphp/framework/wiki/Db-Table");
            }
            if (count($keyValues) > count($keyNames)) {
                throw new InvalidPrimaryKeyException("Too many columns for the primary key.\n" . "Please check " . static::class . " initialization or usage.\n" . "Settings described at https://github.com/bluzphp/framework/wiki/Db-Table");
            }
            if (array_keys($keyValues)[0] === 0) {
                // for numerical array
                $whereList[] = array_combine($keyNames, $keyValues);
            } else {
                // for assoc array
                $whereList[] = $keyValues;
            }
        }
        return $self::findWhere(...$whereList);
    }

Usage Example

Beispiel #1
0
 /**
  * @dataProvider getFindWrongData
  * @expectedException Bluz\Db\Exception\InvalidPrimaryKeyException
  * @param $keyValues
  */
 public function testFindException($keyValues)
 {
     $this->table->find(...$keyValues);
 }