lithium\data\model\Query::alias PHP Method

alias() public method

Get or Set a unique alias for the query or a query's relation if $relpath is set.
public alias ( mixed $alias = true, string $relpath = null ) : string
$alias mixed The value of the alias to set for the passed `$relpath`. For getting an alias value set alias to `true`.
$relpath string A dotted relation name or `null` for identifying the query's model.
return string An alias value or `null` for an unexisting `$relpath` alias.
    public function alias($alias = true, $relpath = null)
    {
        if ($alias === true) {
            if (!$relpath) {
                return $this->_config['alias'];
            }
            $return = array_search($relpath, $this->_paths);
            return $return ?: null;
        }
        if ($relpath === null) {
            $this->_config['alias'] = $alias;
        }
        if ($relpath === null && ($model = $this->_config['model'])) {
            $this->_models[$alias] = $model;
        }
        $relpath = (string) $relpath;
        unset($this->_paths[array_search($relpath, $this->_paths)]);
        if (!$alias && $relpath) {
            $last = strrpos($relpath, '.');
            $alias = $last ? substr($relpath, $last + 1) : $relpath;
        }
        if (isset($this->_alias[$alias])) {
            $this->_alias[$alias]++;
            $alias .= '__' . $this->_alias[$alias];
        } else {
            $this->_alias[$alias] = 1;
        }
        $this->_paths[$alias] = $relpath;
        return $alias;
    }

Usage Example

コード例 #1
0
 public function testAliasAndPaths()
 {
     $model = 'lithium\\tests\\mocks\\data\\model\\MockQueryComment';
     $query = new Query(compact('model'));
     $this->assertIdentical('MockQueryComment', $query->alias());
     $this->assertIdentical('MockQueryComment', $query->alias(true));
     $this->assertIdentical('MockQueryComment2', $query->alias('MockQueryComment2'));
     $this->assertIdentical('MockQueryComment2', $query->alias());
     $this->assertIdentical('MockQueryComment2', $query->alias(true));
     $this->assertIdentical('MockQueryComment__2', $query->alias('MockQueryComment', 'Model1'));
     $this->assertIdentical('MockQueryComment2__2', $query->alias('MockQueryComment2', 'Model2'));
     $this->assertIdentical('MockQueryComment__2', $query->alias(true, 'Model1'));
     $this->assertIdentical('MockQueryComment2__2', $query->alias(true, 'Model2'));
     $query = new Query(compact('model') + array('source' => 'my_custom_table', 'alias' => 'MyCustomAlias'));
     $result = $query->export($this->db);
     $this->assertIdentical('{my_custom_table}', $result['source']);
     $this->assertIdentical('AS {MyCustomAlias}', $result['alias']);
     $this->assertIdentical('MyCustomAlias__2', $query->alias('MyCustomAlias', 'Relation1'));
     $this->assertIdentical('MyCustomAlias__3', $query->alias('MyCustomAlias', 'Other.Relation2'));
     $this->assertIdentical('MyCustomAlias2', $query->alias('MyCustomAlias2', 'Other.Other.Relation3'));
     $this->assertIdentical('MyCustomAlias', $query->alias());
     $this->assertIdentical('MyCustomAlias__2', $query->alias(true, 'Relation1'));
     $this->assertIdentical('MyCustomAlias__3', $query->alias(true, 'Other.Relation2'));
     $this->assertIdentical('MyCustomAlias2', $query->alias(true, 'Other.Other.Relation3'));
     $this->assertIdentical('Relation4', $query->alias(null, 'Relation4'));
     $this->assertIdentical('Relation5', $query->alias(null, 'Other.Relation5'));
     $this->assertIdentical('Relation5__2', $query->alias(null, 'Other.Other.Relation5'));
     $this->assertIdentical('Relation4', $query->alias(true, 'Relation4'));
     $this->assertIdentical('Relation5', $query->alias(true, 'Other.Relation5'));
     $this->assertIdentical('Relation5__2', $query->alias(true, 'Other.Other.Relation5'));
     $expected = array('MyCustomAlias' => null, 'MyCustomAlias__2' => 'Relation1', 'MyCustomAlias__3' => 'Other.Relation2', 'MyCustomAlias2' => 'Other.Other.Relation3', 'Relation4' => 'Relation4', 'Relation5' => 'Other.Relation5', 'Relation5__2' => 'Other.Other.Relation5');
     $this->assertEqual($expected, $query->paths($this->db));
     $model = 'lithium\\tests\\mocks\\data\\model\\MockQueryPost';
     $query = new Query(compact('model'));
     $query->alias(null, 'MockQueryComment');
     $query->alias('MockQueryPost2', 'MockQueryComment.MockQueryPost');
     $expected = array('MockQueryPost' => null, 'MockQueryComment' => 'MockQueryComment', 'MockQueryPost2' => 'MockQueryComment.MockQueryPost');
     $this->assertEqual($expected, $query->paths($this->db));
 }
All Usage Examples Of lithium\data\model\Query::alias