From 980cff8db69808d010a186bcc07e50fd247aa69a Mon Sep 17 00:00:00 2001 From: Abdallah ARFFAK Date: Wed, 13 Apr 2016 18:07:35 +0100 Subject: [PATCH] Added query_method option to PropelParamConverter (#316) * Update PropelParamConverter.php add "query_method" option equivalence of "repository_method" in doctrine * FIX CS * FIX CS * \method_exists => && method_exists * and => && --- .../ParamConverter/PropelParamConverter.php | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/Request/ParamConverter/PropelParamConverter.php b/Request/ParamConverter/PropelParamConverter.php index 85d36e7..23de0ce 100644 --- a/Request/ParamConverter/PropelParamConverter.php +++ b/Request/ParamConverter/PropelParamConverter.php @@ -49,6 +49,12 @@ class PropelParamConverter implements ParamConverterInterface */ protected $withs; + /** + * name of method use to call a query method + * @var string + */ + protected $queryMethod; + /** * @var bool */ @@ -125,15 +131,24 @@ class PropelParamConverter implements ParamConverterInterface $this->withs = isset($options['with']) ? is_array($options['with']) ? $options['with'] : array($options['with']) : array(); - // find by Pk - if (false === $object = $this->findPk($classQuery, $request)) { - // find by criteria - if (false === $object = $this->findOneBy($classQuery, $request)) { - if ($configuration->isOptional()) { - //we find nothing but the object is optional - $object = null; - } else { - throw new \LogicException('Unable to guess how to get a Propel object from the request information.'); + $this->queryMethod = $queryMethod = isset($options['query_method']) ? $options['query_method'] : null; + + if ($this->queryMethod != null && method_exists($classQuery, $this->queryMethod)) { + // find by custom method + $query = $this->getQuery($classQuery); + // execute a custom query + $object = $query->$queryMethod($request->attributes); + } else { + // find by Pk + if (false === $object = $this->findPk($classQuery, $request)) { + // find by criteria + if (false === $object = $this->findOneBy($classQuery, $request)) { + if ($configuration->isOptional()) { + //we find nothing but the object is optional + $object = null; + } else { + throw new \LogicException('Unable to guess how to get a Propel object from the request information.'); + } } } }