symfony 2.2 getChild已弃用

时间:2013-06-10 05:37:16

标签: symfony symfony-2.2

我正在尝试为地点JulLocationBundle安装捆绑包。 我遇到了一些我设法解决的问题;然后出现错误call undefined function getChild

一些研究表明,这是由于方法的弃用(自2.2起)......

因此,以下是需要更改的代码部分:

            if( $locationForm->offsetExists( $locationType ) )
        {
            $topLevel = $locationType;
            $topLevelForm = $locationForm->getChild( $topLevel );

            break;
        }

完整的控制器代码:

namespace Jul\LocationBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class GooglemapsController extends Controller
{
    public function placesAutocompleteAction
    (
            $locationForm,
            $zoomDefault = null,
            $zoomResolved = 17,
            $latitude = null,
            $longitude = null,
            $mapDiv = 'map_canvas',
            $mapOptions = array(),
            $acFields = null,
            $addressFallback = false,
            $maxImageWidth = 200,
            $maxImageHeight = 200
    )
    {
        /*
         * Find top level entity
         */
        $locationTypes = array( 'location', 'city', 'state', 'country' );

        foreach( $locationTypes as $locationType )
        {
            if( $locationForm->offsetExists( $locationType ) )
            {
                $topLevel = $locationType;
                $topLevelForm = $locationForm->getChild( $topLevel );

                break;
            }

            if( $locationForm->getName() == 'Jul' . ucfirst( $locationType ) . 'Field' )
            {
                $topLevel = $locationType;
                $topLevelForm = $locationForm;

                break;
            }
        }

        /*
         * Top level not found
         */
        if( ! isset( $topLevel ) ) throw new \Exception( 'There is no location field in the form sent to the controller JulLocationBundle:Googlemaps:placesAutocomplete' );

        /*
         * Default map center and zoom
         */
        if( $topLevelForm->offsetExists( 'latitude' ) && ( $latForm = $topLevelForm->getChild( 'latitude' )->get( 'value' ) ) <> 0 )
        {
            /*
             * If the form has been sent with a location
             */
            $latitude = $latForm;
            $longitude = $topLevelForm->getChild( 'longitude' )->get( 'value' );

            $zoomDefault = $zoomResolved;
        }
        else
        {
            if( ! $latitude ) $latitude = 40.4230;
            if( ! $longitude ) $longitude = -98.7372;
            if( ! $zoomDefault ) $zoomDefault = 3;
        }

        /*
         * Default map options array
         */
        $mapOptions = array_merge( array(
            'zoom' => $zoomDefault
            ), $mapOptions );

        /*
         * Default autocomplete input field
         */
        if( ! isset( $acFields[ 0 ][ 'acInput' ] ) )
        {
            $acFields[ 0 ][ 'acInput' ] = ( $topLevelForm->offsetExists( 'long_name' ) ) ? $topLevelForm->getChild( 'long_name' )->get( 'id' ) : $topLevelForm->getChild( 'name' )->get( 'id' );
        }

        /*
         * Default autocomplete Types
         */
        if( ! isset( $acFields[ 0 ][ 'acOptions' ]['types'] ) )
        {
            switch( $topLevel )
            {
                case 'location': $acFields[ 0 ][ 'acOptions' ][ 'types' ] = array( 'establishment' ); break;
                case 'city': $acFields[ 0 ][ 'acOptions' ][ 'types' ] = array( '(cities)' ); break;
                default: $acFields[ 0 ][ 'acOptions' ][ 'types' ] = array( '(regions)' );
            }
        }

        /*
         * Address autocomplete fallback
         */
        if( $addressFallback && $topLevel == 'location' && ! isset( $acFields[ 1 ][ 'acInput' ] ) && $topLevelForm->offsetExists( 'long_address' ) )
        {
            $acFields[ 1 ][ 'acInput' ] = ( $topLevelForm->offsetExists( 'long_name' ) ) ? $topLevelForm->getChild( 'long_address' )->get( 'id' ) : $topLevelForm->getChild( 'address' )->get( 'id' );
            $acFields[ 1 ][ 'acOptions' ][ 'types' ] = array( 'geocode' );
        }

        /*
         * Build javascript field IDs array using JulLocationBundle config
         */

        $jsFieldIds = array();
        $tmpLevel = $locationForm;

        foreach( $this->container->parameters[ 'jul_location.options' ] as $level => $options )
        {
            $fields = $options['fields'];

            $tmpArray = array();

            if( $tmpLevel->offsetExists( $level ) )
            {
                $tmpLevel = $tmpLevel->getChild( $level );

                foreach( $fields as $field => $fieldArray )
                {
                    /*
                     * Check if field is active in config && exists in the form
                     */
                    if( $fieldArray[ 'enabled' ] && $tmpLevel->offsetExists( $field ) ) $tmpArray[ $field ] = $tmpLevel->getChild( $field )->get( 'id' );
                }
            }

            $jsFieldIds[ $level ] = $tmpArray;
        }

        return $this->render( 'JulLocationBundle:Googlemaps:placesAutocomplete.html.twig', array(
                'mapDiv' => $mapDiv,
                'mapOptions' => json_encode( $mapOptions ),
                'acFields' => json_encode( $acFields ),
                'topLevel' => $topLevel,
                'zoomResolved' => $zoomResolved,
                'latitude' => $latitude,
                'longitude' => $longitude,
                'jsFieldIds' => json_encode( $jsFieldIds ),
                'maxImageWidth' => $maxImageWidth,
                'maxImageHeight' => $maxImageHeight
                ));
    }
}

代码非常自我解释我们需要获得顶级实体的子代(如果您遵循设置,通常是位置,但问题在于如何解决formview::getchild()方法的弃用问题。

编辑: 对于任何需要捆绑的人。

我将在几天内提供拉取请求以解决2.2 {/ 1>的问题

2 个答案:

答案 0 :(得分:2)

您可以使用get方法$form->get('...')$form['...']

详细了解deprecations

答案 1 :(得分:1)

而不是:

$topLevelForm = $locationForm->getChild($topLevel);

您应该使用:

$topLevelForm = $locationForm->children[$topLevel];

作为it is described in the code(分支2.2)。