Bake\Shell\Task\ModelTask::findTableReferencedBy PHP Method

findTableReferencedBy() public method

Search tables in db for keyField; if found search key constraints for the table to which it refers.
public findTableReferencedBy ( Cake\Database\Schema\Table $schema, string $keyField ) : string | null
$schema Cake\Database\Schema\Table The table schema to find a constraint for.
$keyField string The field to check for a constraint.
return string | null Either the referenced table or null if the field has no constraints.
    public function findTableReferencedBy($schema, $keyField)
    {
        if (!$schema->column($keyField)) {
            return null;
        }
        foreach ($schema->constraints() as $constraint) {
            $constraintInfo = $schema->constraint($constraint);
            if (in_array($keyField, $constraintInfo['columns'])) {
                if (!isset($constraintInfo['references'])) {
                    continue;
                }
                return $constraintInfo['references'][0];
            }
        }
        return null;
    }

Usage Example

コード例 #1
0
ファイル: ModelTaskTest.php プロジェクト: rashmi/newrepo
 /**
  * test finding referenced tables using constraints.
  *
  * @return void
  */
 public function testFindTableReferencedBy()
 {
     $invoices = TableRegistry::get('Invitations');
     $schema = $invoices->schema();
     $result = $this->Task->findTableReferencedBy($schema, 'not_there');
     $this->assertNull($result);
     $result = $this->Task->findTableReferencedBy($schema, 'sender_id');
     $this->assertEquals('users', $result);
     $result = $this->Task->findTableReferencedBy($schema, 'receiver_id');
     $this->assertEquals('users', $result);
 }