3

我正在尝试为 locations 安装一个捆绑包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()方法的弃用问题。?!

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

JulLocationBundle我将在几天内提供一个拉取请求来修复2.2

4

2 回答 2

2

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

阅读有关弃用的更多信息。

于 2013-06-10T05:47:11.140 回答
1

代替 :

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

你应该使用:

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

正如代码(分支 2.2)中所描述的那样。

于 2013-06-10T07:36:18.010 回答