-2

Actually I'm doing a query using setParameters of Doctrine but if I try to get the query I see the parameters aren't setted.

The code is this:

$query = $this->createQueryBuilder('l')
            ->where('l.project = :projectId')
            ->andWhere('l.state.value != :deletedState')
            ->setParameters([
                'projectId' => $project->getId(),
                'deletedState' => LandingState::STATE_DELETED,
            ]);

And this is the query I get if I make a var_dump

'SELECT l0_.lan_id AS lan_id_0, l0_.lan_title AS lan_title_1, 
l0_.lan_url AS lan_url_2, l0_.lan_testimony AS lan_testimony_3, 
l0_.lan_testimony_content AS lan_testimony_content_4, 
l0_.lan_final_claim AS lan_final_claim_5, l0_.lan_service_ids AS 
lan_service_ids_6, l0_.lan_state AS lan_state_7, l0_.lan_index AS 
lan_index_8, l0_.lan_follow AS lan_follow_9, l0_.lan_script AS 
lan_script_10, l0_.lan_date_insert AS lan_date_insert_11, 
l0_.lan_date_update AS lan_date_update_12, l0_.pro_id AS pro_id_13, 
l0_.lan_image AS lan_image_14, l0_.lan_testimony_image AS 
lan_testimony_image_15 FROM landing l0_ WHERE l0_.pro_id = ? AND 
l0_.lan_state <> ?'

But if I try to get the value of $project->getId() or the LandingState::STATE_DELETED with an echo I get the value. Why in query aren't the values setteds?

Lluís Puig Ferrer
  • 1,128
  • 7
  • 19
  • 49

1 Answers1

0

the way i see there is nothing wrong, look at this simple example to understand:

the code in controller:

$search = 'test';
    $em = $this->getDoctrine()->getManager();
    $queryBuilder = $em->createQueryBuilder();
    $queryBuilder->select('d')
        ->from('BackBundle\Entity\disponibility', 'd')
        ->where('d.name like :search')
    ->setParameter('search', '%' .$search. '%');
    $query = $queryBuilder->getQuery();

    dump($query->getSQL());
    dump($query);

the dumped contents:

$query->getSQL();

"SELECT d0_.id AS id0, d0_.name AS name1 FROM disponibility d0_ WHERE d0_.name LIKE ?"

$query;

    Query {#789 ▼
-_state: 1
-_dql: "SELECT d FROM BackBundle\Entity\disponibility d WHERE d.name like :search"
-_parserResult: ParserResult {#793 ▼
    -_sqlExecutor: SingleSelectExecutor {#821 ▼
    #_sqlStatements: "SELECT d0_.id AS id0, d0_.name AS name1 FROM disponibility d0_ WHERE d0_.name LIKE ?"
    #queryCacheProfile: null
    }
    -_resultSetMapping: ResultSetMapping {#788 ▼
    +isMixed: false
    +aliasMap: array:1 [▼
        "d" => "BackBundle\Entity\disponibility"
    ]
    +relationMap: []
    +parentAliasMap: []
    +fieldMappings: array:2 [▼
        "id0" => "id"
        "name1" => "name"
    ]
    +scalarMappings: []
    +typeMappings: []
    +entityMappings: array:1 [▼
        "d" => null
    ]
    +metaMappings: []
    +columnOwnerMap: array:2 [▼
        "id0" => "d"
        "name1" => "d"
    ]
    +discriminatorColumns: []
    +indexByMap: []
    +declaringClasses: array:2 [▼
        "id0" => "BackBundle\Entity\disponibility"
        "name1" => "BackBundle\Entity\disponibility"
    ]
    +isIdentifierColumn: []
    +newObjectMappings: []
    +metadataParameterMapping: []
    }
    -_parameterMappings: array:1 [▼
    "search" => array:1 [▼
        0 => 0
    ]
    ]
}
-_firstResult: null
-_maxResults: null
-_queryCache: null
-_expireQueryCache: false
-_queryCacheTTL: null
-_useQueryCache: true
#parameters: ArrayCollection {#790 ▼
    -elements: array:1 [▼
    0 => Parameter {#791 ▼
        -name: "search"
        -value: "%test%"
        -type: 2
    }
    ]
}
#_resultSetMapping: null
#_em: EntityManager {#348 …10}
#_hints: []
#_hydrationMode: 1
#_queryCacheProfile: null
#_expireResultCache: false
#_hydrationCacheProfile: null
}

and finally the query from the symfony profiler:

    SELECT count(DISTINCT d0_.id) AS sclr0 FROM disponibility d0_ WHERE d0_.name LIKE ?

Parameters: ['%test%'] 

as you can see the SetParameter of doctrine is working just fine,but the dump function will not show the parameters. If you want to see the parameters look at them in the profiler