I use Nelmio to generate automatically my api doc. I would like to return an object in responseMap which is a simple class (entity without a database associated) like this :
/**
* #ApiDoc(
* description = "Get informations from user.",
* responseMap = {
* 200 = { "\AppBundle\Entity\MyUserInfos" },
* },
* )
*
* #Rest\View(statusCode=Response::HTTP_OK)
* #Rest\Get("/my_user_infos")
*/
public function getMyUserInfosAction(Request $request) {
...
}
namespace AppBundle\Entity;
/**
* MyUserInfos
*/
class MyUserInfos
{
/**
* #var string
*/
private $username;
/**
* #var string
*/
private $email;
+getters and setters
}
But response object is not displayed in my api doc. Can anyone help me ?
Thanks.
Remove the leading backslash on you class name
/**
* #ApiDoc(
* description = "Get informations from user.",
* responseMap = {
* 200 = { "AppBundle\Entity\MyUserInfos" },
* },
* )
*
* #Rest\View(statusCode=Response::HTTP_OK)
* #Rest\Get("/my_user_infos")
*/
public function getMyUserInfosAction(Request $request) {
...
}
See:https://github.com/nelmio/NelmioApiDocBundle/blob/2d70b0802144fd2c868783c46fa1be4a774967d4/Resources/doc/swagger-support.rst#multiple-response-models
Related
I was trying to create a Payload where one of the fields is Collection of another objects. So I created something like this.
use DateTimeInterface;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Collection;
class MyPayload
{
/**
* #var int
*
* #Serializer\Type("int")
* #Assert\Positive()
*/
private int $someNumber;
/**
* #var DateTimeInterface
*
* #Serializer\Type("DateTime<'Y-m-d'>")
*/
private DateTimeInterface $someDate;
/**
* #Assert\Collection(
* fields={
* "label"=#Assert\NotBlank(),
* "someNumber2"=#Assert\Positive(),
* }
* )
*/
private Collection $someCollection;
}
I'm sending something like this by postman (number of elements in some_collection is unknow, can be 1 can be 10):
{
"some_number": 123,
"some_date": "2021-01-01",
"some_collection": [
{
"label": "test1",
"some_number2": 10000
},
{
"label": "test2",
"some_number2": 15000
}
]
}
To my controller which looks like this:
/**
* #param MyPayload $myPayload
* #param ConstraintViolationListInterface $validationErrors
*
* #return JsonResponse
* #ParamConverter("myPayload", converter="fos_rest.request_body",
* class="App\Request\Payload\MyPayload")
*
*/
public function doSomething(
ContractPayload $myPayload,
ConstraintViolationListInterface $validationErrors
): JsonResponse {
try {
if (count($validationErrors) > 0) {
throw new ValidationException($validationErrors);
}
...
}
And the only answear I've got was :
'contractRate' - This value should be of type array|(Traversable&ArrayAccess).
I was looking for solution, but couldn't find any working one, do you know what to do to make it work?
your problem is exactly on the type you passed on the function, you passed ContractPayload instead of MyPayload,
suddenly you will encounter another problem with serialization compared to the collection on the MyPayload type .. like that I made a little evolution hope that is clear.
First create a new type for collection it's usefull instead of collection of type any
<?php
namespace App\Request\Payload;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
class SomeItem
{
/**
* #Assert\NotBlank()
* #Serializer\Type("string")
*/
private $label;
/**
* #Assert\Positive()
* #Serializer\Type("int")
*/
private $someNumber2;
/**
* #return string
*/
public function getLabel():string
{
return $this->label;
}
/**
* #param string $label
*/
public function setLabel(string $label)
{
$this->label = $label;
}
/**
* #return int
*/
public function getSomeNumber2():int
{
return $this->someNumber2;
}
/**
* #param mixed $someNumber2
*/
public function setSomeNumber2(int $someNumber2)
{
$this->someNumber2 = $someNumber2;
}
}
make a little change on MyPayload
<?php
namespace App\Request\Payload;
use DateTimeInterface;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Collection;
class MyPayload
{
/**
* #var int
*
* #Serializer\Type("int")
* #Assert\Positive()
*/
private int $someNumber;
/**
* #var DateTimeInterface
*
* #Serializer\Type("DateTime<'Y-m-d'>")
*/
private DateTimeInterface $someDate;
/**
* #Serializer\Type("ArrayCollection<App\Request\Payload\SomeItem>")
*/
private Collection $someCollection;
public function getSomeNumber():int
{
return $this->someNumber;
}
/**
* #param int $someNumber
*/
public function setSomeNumber(int $someNumber)
{
$this->someNumber = $someNumber;
}
/**
* #return DateTimeInterface
*/
public function getSomeDate():DateTimeInterface
{
return $this->someDate;
}
/**
* #param DateTimeInterface $someDate
*/
public function setSomeDate(DateTimeInterface $someDate)
{
$this->someDate = $someDate;
}
/**
* #return mixed
*/
public function getSomeCollection():Collection
{
return $this->someCollection;
}
// todo implmentation of add remove someItem ...;)
}
Last change ,Controller
use App\Request\Payload\MyPayload;
// ..
/**
* #param MyPayload $myPayload
* #param ConstraintViolationListInterface $validationErrors
*
* #return JsonResponse
* #ParamConverter("myPayload", converter="fos_rest.request_body", class="App\Request\Payload\MyPayload")
*
*/
public function doSomething(MyPayload $myPayload, ConstraintViolationListInterface $validationErrors): JsonResponse {
try {
if (count($validationErrors) > 0) {
throw new ValidationException($validationErrors);
}
...
}
I'm developing a rest api application with symfony in backend.
My problem is that when I try to lauch a GET request, the application runs without giving any response even after even after some ten minutes.
this is the request method:
/**
* CounterRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CounterRepository extends \Doctrine\ORM\EntityRepository
{
/**
* #param string $name
* #param int $date
* #return mixed
* #throws \Exception
*/
public function getValuesByNameAndDate(string $name, int $date)
{
$query = $this
->createQueryBuilder('c')
->leftJoin('c.values', 'v', 'WITH')
->addSelect('v')
->where('c.name = :name')
->andWhere('v.date = :date')
->setParameters(
array(
'date' => $date,
'name' => $name
)
)
->getQuery();
return $query->getResult();
}
}
The CounterValue Entity:
/**
* CounterValue
*
* #ORM\Table(name="counter_value")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CounterValueRepository")
*/
class CounterValue
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var float
*
* #ORM\Column(name="value", type="float")
*/
private $value;
/**
* #var int
*
* #ORM\Column(name="date", type="integer")
*/
private $date;
/**
* #var Counter
*
* #ORM\ManyToOne(targetEntity="Counter", inversedBy="values")
* #ORM\JoinColumn()
*/
private $counter;
/**
* #var City
*
* #ORM\ManyToOne(targetEntity="City", inversedBy="values"))
* #ORM\JoinColumn()
*/
protected $city;
public function __construct()
{
$date = new \DateTime('-1 day');
$this->date = strtotime($date->format('d-m-Y'));
}
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set value.
*
* #param float $value
*
* #return CounterValue
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value.
*
* #return float
*/
public function getValue()
{
return $this->value;
}
/**
* Set date.
*
* #param int $date
*
* #return CounterValue
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date.
*
* #return int
*/
public function getDate()
{
return $this->date;
}
/**
* #return Counter
*/
public function getCounter(): Counter
{
return $this->counter;
}
/**
* #param Counter $counter
*/
public function setCounter(Counter $counter): void
{
$this->counter = $counter;
}
/**
* #return City
*/
public function getCity(): City
{
return $this->city;
}
/**
* #param City $city
*/
public function setCity(City $city): void
{
$this->city = $city;
}
}
/**
* Counter
*
* #ORM\Table(name="counter")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CounterRepository")
*/
class Counter
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, unique=false)
*/
private $name;
/**
* #var XmlFile
*
* #ORM\ManyToOne(targetEntity="XmlFile", inversedBy="counters", cascade={"remove"})
* #ORM\JoinColumn(nullable=false)
*/
private $xmlFile;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="CounterValue", mappedBy="counter", cascade={"persist"})
*/
private $values;
public function __construct()
{
$this->values = new ArrayCollection();
}
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name.
*
* #param string $name
*
* #return Counter
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name.
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set xmlFile
*
* #param XmlFile $xmlFile
*/
public function setXmlFile(XmlFile $xmlFile)
{
$this->xmlFile = $xmlFile;
}
/**
* Get xmlFile
*
* #return XmlFile
*/
public function getXmlFile(): XmlFile
{
return $this->xmlFile;
}
/**
* #return ArrayCollection
*/
public function getValues(): ArrayCollection
{
return $this->values;
}
/**
* #param ArrayCollection $values
*/
public function setValues(ArrayCollection $values): void
{
$this->values = $values;
}
/**
* Add $value
*
* #param CounterValue $value
*/
public function addValue(CounterValue $value)
{
$value->setCounter($this);
if(!$this->values->contains($value)){
$this->values->add($value);
}
}
}
And finally my method in controller
$em = $this->get('doctrine.orm.entity_manager');
$counterRep = $em->getRepository('AppBundle:Counter');
return $counterRep->getValuesByNameAndDate('L.IRATHO.E2G.CSFB.ExecSuccOut', 1533600000);
Just return a Json response like this.
public function listAction(Request $request)
{
try
{
$em = $this->get('doctrine.orm.entity_manager');
$counterRep = $em->getRepository('AppBundle:Counter')->getValuesByNameAndDate('L.IRATHO.E2G.CSFB.ExecSuccOut', 1533600000);
return new JsonResponse(['success' => true, 'code' => Response::HTTP_OK, 'data' => $counterRep]);
}
catch (\Exception $exception)
{
return new JsonResponse(['success' => false,'code' => $exception->getCode() ,'message' => $exception->getMessage()]);
}
}
Controller action should return instance of
Symfony\Component\HttpFoundation\Response
You try to return result of query builder $query->getResult()
You need to change it to something like
public function counterValuesAction(Request $request) {
try {
$em = $this->get('doctrine.orm.entity_manager');
$counterRep = $em->getRepository('AppBundle:Counter');
$values = $counterRep->getValuesByNameAndDate('L.IRATHO.E2G.CSFB.ExecSuccOut', 1533600000);
} catch (\Exception $e) {
return new Response($values);
}
}
Solution for Symfony 4+:
<?php
namespace App\Controller;
class CounterController extends AbstractController
{
public function listAction(
CounterRepository $repository // DI works like this in Controllers...
) {
$result = $repository->getValuesByNameAndDate('L.IRATHO.E2G.CSFB.ExecSuccOut', 1533600000);
// use JsonResponse object to create JSON response
return $this->json($result, Response::HTTP_OK);
}
}
// routes.yaml
counter_list:
path: /counter
controller: App\Controller\CounterController::listAction
methods: GET
Clear cache and try to send GET request to http://your-dev-host/counter
How does it work?
Controller
You need to create controller CounterController class with action function listAction. It is good practice to add Action postfix to action function.
You can define services or other DI objects as parameters in action functions. DI will set it. For example:
class MyController extends AbstractController
{
public function listAction(
Request $request,
SerializerInterface $serializer,
ValidatorInterface $validator,
CounterRepository $repository
) {
// ...
}
}
In order to send good response with JSON and all headers that is needed you need to use AbstractController::json function. It creates JsonResponse object. You can check it implementation.
More information about symfony controllers you can find in this article https://symfony.com/doc/current/controller.html
Routing
Next step is to create route for your new controller action. You can do it in 2 ways:
Adding record to config/routes.yaml file as i did
Use annotations (Annotations is bad practice because comments should not break programm)
More information about routing you can find in this article https://symfony.com/doc/current/routing.html
It does not work for me! How can I debug it?
First, dont panic and read error message. Also, you can check if routing works well. You can do it using command
$ bin/console debug:router
----------------------- -------- -------- ------ -------------------------------------
Name Method Scheme Host Path
----------------------- -------- -------- ------ -------------------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
register_token POST ANY ANY /auth/register
refresh_token POST ANY ANY /auth/refresh
auth_login POST ANY ANY /auth/login
restroom_list GET ANY ANY /api/v1/restroom
----------------------- -------- -------- ------ -------------------------------------
In table you need to check your new url and how Symfony sees it.
Second, try to send request to this URL using curl and check response.
$ curl http://localhost/auth/login
New in SF3, I use API Platform and Sonata Media Bundle.
I'm blocked while getting Gallery entity of Sonata using API Platform GET request.
"A circular reference has been detected when serializing the object of class \"Application\\Sonata\\MediaBundle\\Entity\\Gallery\" (configured limit: 1)"
The admin of the entity works great, I can add a gallery to the entity.
When the entity have a gallery it cause this error, when it does not it's ok.
Entity Technic
GET /technics in API Platform
[
{
"id": 0,
"type": "string",
"comment": "string",
"links": [
"string"
],
"gallery": "string"
}
]
Entity Class
<?php
// src/AppBundle/Entity/Technic.php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* #ORM\Entity
* #ApiResource
*/
class Technic
{
/**
* #var int The id of this evaluation.
*
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* #var string $type TechnicType of the evaluation
*
* #ORM\OneToOne(targetEntity="TechnicType")
* #Assert\NotBlank
*/
public $type;
/**
* #var string $note Note of the evaluation
*
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $comment;
/**
* #var Link[] Link Links of this technic.
*
* #ORM\ManyToMany(targetEntity="Link", cascade={"persist"})
*/
private $links;
/**
* #ORM\OneToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Gallery",cascade={"persist"})
* #ORM\JoinColumn(name="gallery", referencedColumnName="id", nullable=true)
*/
private $gallery;
/**
* Constructor
*/
public function __construct()
{
$this->links = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set type
*
* #param \AppBundle\Entity\TechnicType $type
*
* #return Technic
*/
public function setType(\AppBundle\Entity\TechnicType $type = null)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return \AppBundle\Entity\TechnicType
*/
public function getType()
{
return $this->type;
}
/**
* Add link
*
* #param \AppBundle\Entity\Link $link
*
* #return Technic
*/
public function addLink(\AppBundle\Entity\Link $link)
{
$this->links[] = $link;
return $this;
}
/**
* Remove link
*
* #param \AppBundle\Entity\Link $link
*/
public function removeLink(\AppBundle\Entity\Link $link)
{
$this->links->removeElement($link);
}
/**
* Get links
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getLinks()
{
return $this->links;
}
/**
* Set comment
*
* #param string $comment
*
* #return Technic
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* #return string
*/
public function getComment()
{
return $this->comment;
}
/**
* Set gallery
*
* #param \Application\Sonata\MediaBundle\Entity\Gallery $gallery
*
* #return Technic
*/
public function setGallery(\Application\Sonata\MediaBundle\Entity\Gallery $gallery = null)
{
$this->gallery = $gallery;
return $this;
}
/**
* Get gallery
*
* #return \Application\Sonata\MediaBundle\Entity\Gallery
*/
public function getGallery()
{
return $this->gallery;
}
}
Thank a lot guys, I'm desesperate I try a lot of things in StackQ/A, annotations, seraliazer config...
You need to configure serialization correctly. Either setup serialization groups, so that on GETting some entity serializer would only pick (for example) IDs of related entities, or set up circualr reference handler in normalizer and inject this normalizer into serializer.
$normalizer = new GetSetMethodNormalizer();
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
There might be more specific answer for api-platform, which I don't know, because serialization of related entities is popular issue.
I try to filter my elastica query by language. My query works fine, but when i add the filter, i get 0 result.
My entity :
<?php
namespace Youmiam\RecipeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Product
*
* #ORM\Table(name="product")
* #ExclusionPolicy("all")
* #ORM\Entity(repositoryClass="Youmiam\RecipeBundle\Entity\ProductRepository")
*/
class Product
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #ORM\ManyToOne(targetEntity="Youmiam\RecipeBundle\Entity\Brand", inversedBy="products")
* #ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
private $brand;
/**
* #ORM\ManyToOne(targetEntity="Youmiam\RecipeBundle\Entity\Ingredient", inversedBy="products")
* #ORM\JoinColumn(name="ingredient_id", referencedColumnName="id")
* #Expose
*/
private $ingredient;
/**
* #ORM\ManyToMany(targetEntity="Youmiam\RecipeBundle\Entity\Recipe", inversedBy="products")
* #ORM\JoinTable(name="products__recipes")
*/
private $recipes;
/**
* #ORM\OneToMany(targetEntity="Youmiam\RecipeBundle\Entity\Quantity", mappedBy="product", orphanRemoval=true, cascade={"all"})
*/
private $quantities;
/**
* #var \stdClass
*
* #ORM\Column(name="photo", type="string", length=255, nullable=true)
* #Expose
*/
private $photo;
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Product
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set name
*
* #param string $name
* #return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set ingredient
*
* #param \Youmiam\RecipeBundle\Entity\Ingredient $ingredient
* #return Product
*/
public function setIngredient(\Youmiam\RecipeBundle\Entity\Ingredient $ingredient = null)
{
$this->ingredient = $ingredient;
return $this;
}
/**
* Get ingredient
*
* #return \Youmiam\RecipeBundle\Entity\Ingredient
*/
public function getIngredient()
{
return $this->ingredient;
}
/**
* Set brand
*
* #param \Youmiam\RecipeBundle\Entity\Brand $brand
* #return Product
*/
public function setBrand(\Youmiam\RecipeBundle\Entity\Brand $brand = null)
{
$this->brand = $brand;
return $this;
}
/**
* Get brand
*
* #return \Youmiam\RecipeBundle\Entity\Brand
*/
public function getBrand()
{
return $this->brand;
}
/**
* Constructor
*/
public function __construct()
{
$this->recipes = new \Doctrine\Common\Collections\ArrayCollection();
$this->quantities = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set photo
*
* #param string $photo
* #return Product
*/
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* #return string
*/
public function getPhoto()
{
return $this->photo;
}
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Add recipes
*
* #param \Youmiam\RecipeBundle\Entity\Recipe $recipes
* #return Product
*/
public function addRecipe(\Youmiam\RecipeBundle\Entity\Recipe $recipes)
{
$this->recipes[] = $recipes;
return $this;
}
/**
* Remove recipes
*
* #param \Youmiam\RecipeBundle\Entity\Recipe $recipes
*/
public function removeRecipe(\Youmiam\RecipeBundle\Entity\Recipe $recipes)
{
$this->recipes->removeElement($recipes);
}
/**
* Get recipes
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getRecipes()
{
return $this->recipes;
}
/**
* Add quantities
*
* #param \Youmiam\RecipeBundle\Entity\Quantity $quantities
* #return Product
*/
public function addQuantity(\Youmiam\RecipeBundle\Entity\Quantity $quantities)
{
$this->quantities[] = $quantities;
return $this;
}
/**
* Remove quantities
*
* #param \Youmiam\RecipeBundle\Entity\Quantity $quantities
*/
public function removeQuantity(\Youmiam\RecipeBundle\Entity\Quantity $quantities)
{
$this->quantities->removeElement($quantities);
}
/**
* Get quantities
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getQuantities()
{
return $this->quantities;
}
/**
* get the class Name
* #return string $className
*/
public function getClass()
{
return "Product";
}
/**
* #return Language
*/
public function getBrandLanguage()
{
return $this->brand->getLanguage();
}
}
My Config.yml
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
youmiam:
settings:
index:
analysis:
analyzer:
keyword_analyser:
type: custom
tokenizer: keyword
classic_analyser:
type: custom
tokenizer: lowercase
filter : [my_snow,asciifolding]
ingr_analyser:
type: custom
tokenizer: lowercase
filter : [my_ing_ngram,asciifolding]
recipe_analyser:
type: custom
tokenizer: lowercase
filter : [my_recipe_ngram,asciifolding]
testfollower:
type: stop
stopwords : [',']
filter:
my_snow:
type : "snowball"
language : "French"
my_ing_ngram:
type: "nGram"
min_gram: 3
max_gram: 8
my_recipe_ngram:
type: "nGram"
min_gram: 4
max_gram: 10
char_filter:
my_whtoa :
type : mapping
mappings : ["' '=>a",]
client: default
finder: ~
types:
product:
mappings:
name: { boost: 10, analyzer: classic_analyser }
brand: { boost: 10, analyzer: classic_analyser }
ingredient: { boost: 10, analyzer: classic_analyser }
brandLanguage: { boost: 10 }
persistence:
driver: orm
model: Youmiam\RecipeBundle\Entity\Product
provider: ~
listener: ~
finder: ~
repository: Youmiam\RecipeBundle\SearchRepository\ProductRepository
And then, in my fos elastica repository, i have this code :
$boolQuery = new \Elastica\Query\Bool();
$query = new \Elastica\Query;
$queryString = new \Elastica\Query\QueryString();
$queryString->setQuery($searchText);
$queryString->setAnalyzer('classic_analyser');
$queryString->setFields(array('product.name', 'product.brand', 'product.ingredient'));
$boolQuery->addMust($queryString);
$query->setQuery($boolQuery);
$filter = new \Elastica\Filter\Term();
$filter->setTerm('brandLanguage', 'fr');
$query->setPostFilter($filter);
return $this->find($query);
I tried to put my query directly in a controller, but same result. I really don't know why my filter return no result.
Hope someone could help, cause i'm really don't see the answer
First thing to do is testing your query outside Elastica, you can get the JSON from the Sf2 Profiler or from the logs.
Next, make sure your documents are sent in Elasticsearch the way you want (perform a simple GET /youmiam/product/_search request to see them, in a tool like Sense).
I see that your brandLanguage field is using the standard analyzer, you can see how Elasticsearch index it with a query like this: GET /youmiam/_analyze?field=brandLanguage&text=FR - is there a token with the exact value fr? If not, there will be no match.
Best practice is to set this kind of field as "not_analyzed" too.
Beside that there is no issue with your code (you could use a FilteredQuery instead of postFilter but that's not the issue), you should look more closely at what is indexed and how to query it directly via Sense, and then translate it with Elastica.
I have a problem while json_encodeing a Entity.
public function jsonvoteAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('KorumAGBundle:AGVote')->findOneById($id);
$response = new Response(json_encode($entity, 200));
$response->headers->set('Content-Type',' application/json');
return $response;
}
This code returns me a the users entity
{"users":{"__isInitialized__":false,"id":null,"nickname":null,"pwd":null,"email":null,"firstname":null,"lastname":null,"poste":null,"addr1":null,"addr2":null,"pc":null,"country":null,"phone":null,"province":null,"acess":null,"site":null,"crew":null,"utilisateur":null}}
And when I var dymp my $entity, it returns both my AGVote and USers entity.
Here is my AGVote Entity
<?php
namespace Korum\AGBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Korum\AGBundle\Entity\AGVote
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class AGVote
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
* #ORM\Column(type="text")
*/
private $question;
/**
* #ORM\Column(type="smallint")
*/
private $actif;
/**
* #ORM\ManyToOne(targetEntity="\Korum\KBundle\Entity\Users", cascade={"all"})
*/
public $users;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set question
* Nb : Only AG admin can set a question
* #param text $question
*/
public function setQuestion($question)
{
$this->question = $question;
}
/**
* Get question
*
* #return text
*/
public function getquestion()
{
return $this->question;
}
/**
* Set actif
*
* #param smallint $actif
*/
public function setActif($actif)
{
$this->actif = $actif;
}
/**
* Get actif
*
* #return smallint
*/
public function getActif()
{
return $this->actif;
}
/**
* Set Users
*
* #param Korum\KBundle\Entity\Province $Users
*/
public function setUsers(\Korum\KBundle\Entity\Users $users)
{
$this->users = $users;
}
/**
* Get Users
*
* #return Korum\KBundle\Entity\Users
*/
public function getUsers()
{
return $this->users;
}
}
Does anyone have an idea of what happened ?
I tried to install the JSMSerializerBundle but event with Metadata library at version 1.1.
When I want to clear my cache, it failed with error :
See :
JMSSerializerBundle Installation : Catchable Fatal Error: Argument 1 passed to JMSSerializerBundle\Twig\SerializerExtension::__construct()
By default, json_encode only uses public properties.
So it serialized the only public property of AGVote: $users. The content of $users was an instance of User; which public fields were serialized.
You could work around these by adding a toArray() method to your entities, and then doing json_encode($entity->toArray()), but i highly recommend you to have a look and use the JMSSerializedBundle.