CakePHP 3:如何获取表的所有关联?

时间:2016-12-20 15:52:54

标签: php cakephp cakephp-3.0

那是我的表对象的var_dump():

object(App\Model\Table\BairrosTable)[117]
  public 'registryAlias' => string 'Bairros' (length=7)
  public 'table' => string 'bairros' (length=7)
  public 'alias' => string 'Bairros' (length=7)
  public 'entityClass' => string 'App\Model\Entity\Bairro' (length=23)
  public 'associations' => 
    array (size=4)
      0 => string 'regioes' (length=7)
      1 => string 'cidades' (length=7)
      2 => string 'clientes' (length=8)
      3 => string 'areas' (length=5)
  public 'behaviors' => 
    array (size=0)
      empty
  public 'defaultConnection' => string 'default' (length=7)
  public 'connectionName' => string 'default' (length=7)

我想得到属性关联的数组,但是如果我尝试$this->_table()->associations(),我在受保护的变量上得到了一个带有itens的对象:

object(Cake\ORM\AssociationCollection)[205]
  protected '_items' => 
    array (size=4)
      'regioes' => 
        object(Cake\ORM\Association\BelongsTo)[206]
          protected '_validStrategies' => 
            array (size=2)
              ...
          protected '_name' => string 'Regioes' (length=7)
          protected '_className' => null
          protected '_bindingKey' => null
          protected '_foreignKey' => string 'regiao_id' (length=9)
          protected '_conditions' => 
            array (size=0)
              ...
          protected '_dependent' => boolean false
          protected '_cascadeCallbacks' => boolean false
          protected '_sourceTable' => 
            object(App\Model\Table\BairrosTable)[117]
              ...
          protected '_targetTable' => null
          protected '_joinType' => string 'INNER' (length=5)
          protected '_propertyName' => null
          protected '_strategy' => string 'join' (length=4)
          protected '_finder' => string 'all' (length=3)
          protected '_tableLocator' => null
      'cidades' => 
        object(Cake\ORM\Association\BelongsTo)[193]
          protected '_validStrategies' => 
            array (size=2)
              ...
          protected '_name' => string 'Cidades' (length=7)
          protected '_className' => null
          protected '_bindingKey' => null
          protected '_foreignKey' => string 'cidade_id' (length=9)
          protected '_conditions' => 
            array (size=0)
              ...
          protected '_dependent' => boolean false
          protected '_cascadeCallbacks' => boolean false
          protected '_sourceTable' => 
            object(App\Model\Table\BairrosTable)[117]
              ...
          protected '_targetTable' => null
          protected '_joinType' => string 'INNER' (length=5)
          protected '_propertyName' => null
          protected '_strategy' => string 'join' (length=4)
          protected '_finder' => string 'all' (length=3)
          protected '_tableLocator' => null
      'clientes' => 
        object(Cake\ORM\Association\HasMany)[200]
          protected '_joinType' => string 'INNER' (length=5)
          protected '_strategy' => string 'select' (length=6)
          protected '_validStrategies' => 
            array (size=2)
              ...
          protected '_saveStrategy' => string 'append' (length=6)
          protected '_name' => string 'Clientes' (length=8)
          protected '_className' => null
          protected '_bindingKey' => null
          protected '_foreignKey' => string 'bairro_id' (length=9)
          protected '_conditions' => 
            array (size=0)
              ...
          protected '_dependent' => boolean false
          protected '_cascadeCallbacks' => boolean false
          protected '_sourceTable' => 
            object(App\Model\Table\BairrosTable)[117]
              ...
          protected '_targetTable' => null
          protected '_propertyName' => null
          protected '_finder' => string 'all' (length=3)
          protected '_tableLocator' => null
          protected '_sort' => null
      'areas' => 
        object(Cake\ORM\Association\BelongsToMany)[199]
          protected '_joinType' => string 'INNER' (length=5)
          protected '_strategy' => string 'select' (length=6)
          protected '_junctionTable' => null
          protected '_junctionTableName' => string 'areas_bairros' (length=13)
          protected '_junctionAssociationName' => null
          protected '_junctionProperty' => string '_joinData' (length=9)
          protected '_saveStrategy' => string 'replace' (length=7)
          protected '_targetForeignKey' => string 'area_id' (length=7)
          protected '_through' => null
          protected '_validStrategies' => 
            array (size=2)
              ...
          protected '_dependent' => boolean true
          protected '_targetConditions' => null
          protected '_junctionConditions' => null
          protected '_name' => string 'Areas' (length=5)
          protected '_className' => null
          protected '_bindingKey' => null
          protected '_foreignKey' => string 'bairro_id' (length=9)
          protected '_conditions' => 
            array (size=0)
              ...
          protected '_cascadeCallbacks' => boolean false
          protected '_sourceTable' => 
            object(App\Model\Table\BairrosTable)[117]
              ...
          protected '_targetTable' => null
          protected '_propertyName' => null
          protected '_finder' => string 'all' (length=3)
          protected '_tableLocator' => null
          protected '_sort' => null

那么,我如何得到一个包含表的所有关联的数组?

2 个答案:

答案 0 :(得分:1)

解决。 $this->_table()->associations()\Cake\ORM\AssociationCollection个对象。所以我使用keys()方法来获取关联数组。

答案 1 :(得分:0)

CakePHP 3:getAssociation()

$articlesTable = \Cake\ORM\TableRegistry::get('Articles');
$association = $articlesTable->getAssociation('Tags')