src/Kunstmaan/TranslatorBundle/Service/Migrations/MigrationsService.php
public function buildInsertSql($entities, $entityClassName, $ignoreFields = array())
    {
        if (count($entities) <= 0) {
            return null;
        }

        $fieldNames = $this->entityManager->getClassMetadata($entityClassName)->getFieldNames();

        $tableName = $this->entityManager->getClassMetadata($entityClassName)->getTableName();
        $tableName = $this->entityManager->getConnection()->quoteIdentifier($tableName);
        $fieldNames = array_diff($fieldNames, $ignoreFields);

        $values = array();

        foreach ($entities as $entity) {
            $insertValues = array();

            foreach ($fieldNames as $fieldName) {
                $value = $entity->{'get'.$fieldName}();

                if ($value instanceof \DateTime) {
                    $value = $value->format('Y-m-d H:i:s');
                }

                $insertValues[] = $this->entityManager->getConnection()->quote($value);
            }

            $values[] = '(' . implode(',', $insertValues) . ')';
        }

        foreach ($fieldNames as $key => $fieldName) {
            $columnName = $this->entityManager->getClassMetadata($entityClassName)->getColumnName($fieldName);
            $fieldNames[$key] = $this->entityManager->getConnection()->quoteIdentifier($columnName);
        }

        $sql = sprintf('INSERT INTO %s (%s) VALUES %s', $tableName, implode(',', $fieldNames), implode(', ', $values));

        return $sql;
    }
public function buildInsertSql($entities, $entityClassName, $ignoreFields = array())
    {
        if (count($entities) <= 0) {
            return null;
        }

        $fieldNames = $this->entityManager->getClassMetadata($entityClassName)->getFieldNames();

        $tableName = $this->entityManager->getClassMetadata($entityClassName)->getTableName();
        $tableName = $this->entityManager->getConnection()->quoteIdentifier($tableName);
        $fieldNames = array_diff($fieldNames, $ignoreFields);

        $values = array();

        foreach ($entities as $entity) {
            $insertValues = array();

            foreach ($fieldNames as $fieldName) {
                $value = $entity->{'get'.$fieldName}();

                if ($value instanceof \DateTime) {
                    $value = $value->format('Y-m-d H:i:s');
                }

                $insertValues[] = $this->entityManager->getConnection()->quote($value);
            }

            $values[] = '(' . implode(',', $insertValues) . ')';
        }

        foreach ($fieldNames as $key => $fieldName) {
            $columnName = $this->entityManager->getClassMetadata($entityClassName)->getColumnName($fieldName);
            $fieldNames[$key] = $this->entityManager->getConnection()->quoteIdentifier($columnName);
        }

        $sql = sprintf('INSERT INTO %s (%s) VALUES %s', $tableName, implode(',', $fieldNames), implode(', ', $values));

        return $sql;
    }
public function buildInsertSql ( $ entities , $ entityClassName , $ ignoreFields = array ( ) ) { if ( count ( $ entities ) <= 0 ) { return null ; } $ fieldNames = $ this -> entityManager -> getClassMetadata ( $ entityClassName ) -> getFieldNames ( ) ; $ tableName = $ this -> entityManager -> getClassMetadata ( $ entityClassName ) -> getTableName ( ) ; $ tableName = $ this -> entityManager -> getConnection ( ) -> quoteIdentifier ( $ tableName ) ; $ fieldNames = array_diff ( $ fieldNames , $ ignoreFields ) ; $ values = array ( ) ; foreach ( $ entities as $ entity ) { $ insertValues = array ( ) ; foreach ( $ fieldNames as $ fieldName ) { $ value = $ entity -> { 'get' . $ fieldName } ( ) ; if ( $ value instanceof \ DateTime ) { $ value = $ value -> format ( 'Y-m-d H:i:s' ) ; } $ insertValues [ ] = $ this -> entityManager -> getConnection ( ) -> quote ( $ value ) ; } $ values [ ] = '(' . implode ( ',' , $ insertValues ) . ')' ; } foreach ( $ fieldNames as $ key => $ fieldName ) { $ columnName = $ this -> entityManager -> getClassMetadata ( $ entityClassName ) -> getColumnName ( $ fieldName ) ; $ fieldNames [ $ key ] = $ this -> entityManager -> getConnection ( ) -> quoteIdentifier ( $ columnName ) ; } $ sql = sprintf ( 'INSERT INTO %s (%s) VALUES %s' , $ tableName , implode ( ',' , $ fieldNames ) , implode ( ', ' , $ values ) ) ; return $ sql ; }
Build an sql insert into query by the paramters provided

@param ORM\Entity $entities        Result array with all entities to create an insert for
@param string     $entityClassName Class of the specified entity (same as entities)
@param array      $ignoreFields    fields not to use in the insert query

@return string an insert sql query, of no result nul
Build an sql insert into query by the paramters provided