I have a Product entity, and a ProductList entity.
Is it possible to serialize (and deserialize) a ProductList object to json in a way that the json contains the Product objects related to that ProductList?
The expected output is:
[{
'product_list_name': 'List',
'product_list_created': '2013-07-04',
'products' : {
'product': {...},
'product': {...},
'product': {...},
'product': {...}
}]
I'm using the Symfony2 built-in serializer and JMS\Serializer but I am not having any luck.
Any way to do this?
If you are using YML, ensure you have a YML file for both Product and ProductList.
Entity.ProductList.yml
AppBundle\Entity\ProductList:
exclusion_policy: ALL
properties:
products:
expose: true
Entity.Product.yml
AppBundle\Entity\Product:
exclusion_policy: ALL
properties:
id:
expose: true
Related
I need to make a referene with different entities within one reference. Is it possible to do this with MikroORM ?
Example of what I need:
export const userSchema = new EntitySchema<any>({
class: userEntity,
tableName: 'users',
properties: {
uuid: { type: 'uuid', primary: true },
bills: {
reference: '1:m',
entity: () => BillsFromBank || BillsFromAnotherBank,
mappedBy: (a) => a._user,
},
},
});
Is it possible to create some conditions for such a connection 1 reference field with different entities?
I have a item entity, stock entity and color entity
Item has oneToMany stocks and stock have ManytoOne association with color
now I am mapping item entity and stocks with elastic search. I am mapping stocks as nested of item
but it gives an error when color_id is null in stock table, how I can solve this exception
item:
mappings:
id: { type: integer }
title: { index: analyzed, analyzer: autocomplete, search_analyzer: autocomplete }
description: { index: analyzed, analyzer: simple }
stock:
type: nested
include_in_root: true
properties:
id: { type: integer }
color: { type: object}
Type error: Return value of AppBundle\Entity\Stock::getColor() must be an instance of AppBundle\Entity\Color, null returned
I am stuck from couple of days, if anyone have idea please share
As the title states, I am trying to add a custom property to the serialized object I return.
Let's take a User with the following methods:
getFirstname, setFirstname
getLastname, setLastname
getUsername, setUsername
...
Now in the serialization I would like to add a property fullName: Firstname + Lastname.
I have a getter method in my entity like so:
/**
* get name
*
* #return string
*/
public function getName()
{
return $this->getFirstname()." ".$this->getLastname();
}
My serialization file looks something like this:
AppBundle\Entity\User:
exclusion_policy: ALL
properties:
id:
expose: false
username:
expose: true
groups: [list, details]
email:
expose: true
groups: [details]
name:
expose: true
groups: [list, details]
I have tried with
name:
expose: true
groups: [list, details]
access_type: public_method
type: string
serialized_name: fullName
accessor:
getter: getName
and other variants but I can't seem to get it right.
Note: Yes I've cleared my cache and tried it again.
Anyone able to tell me what I am missing ?
Thanks in advance !
Since your full name is not a property at all you have to define a virtual property:
AppBundle\Entity\User:
exclusion_policy: ALL
properties:
# All properties but not name
virtual_properties:
getName:
groups: [list, details]
serialized_name: fullName
I have a problem with query building with FOQElasticaBundle
I have 3 entities
User
Hotel
Ambiance
Users can have 1 or more Hotels, and each Hotel has only 1 Ambiance.
In my config file, I have:
foq_elastica:
clients:
default: { host: %elasticsearch.host%, port: %elasticsearch.port% }
indexes:
MyBundle:
client: default
finder:
types:
user:
mappings:
id:
boost: 10
analyzer: fr_case_analyzer
name:
boost: 5
analyzer: fr_case_analyzer
hotels:
type: "nested"
properties:
name:
boost: 10
analyzer: fr_case_analyzer
ambiance:
boost: 1
I want to be able to search for User by typing his name or the name of his hotels, and possibly add a filter on the Ambiance type.
So the query should look like something like this :
$mainQuery = new \Elastica_Query_Bool();
$nameQuery = new \Elastica_Query_Bool();
$filtersQuery = new \Elastica_Query_Bool();
//searching in Users' names
$nameQuery = new \Elastica_Query_Text();
$nameQuery->setFieldQuery('name', $searchName);
$nameQuery->setFieldParam('name', 'boost', 5);
$nameQuery->setFieldParam('name', 'type', 'phrase_prefix');
//searching in Hotels' names
$hotelNameQuery = new \Elastica_Query_Text();
$hotelNameQuery->setFieldQuery('name', $searchName);
$hotelNameQuery->setFieldParam('name', 'boost', 3);
$hotelNameQuery->setFieldParam('name', 'type', 'phrase_prefix');
$nestedHotelNameQuery = new \Elastica_Query_Nested();
$nestedHotelNameQuery->setPath('hotels');
$nestedHotelNameQuery->setQuery($hotelNameQuery);
$nameQuery->addShould($nameQuery);
$nameQuery->addShould($nestedHotelNameQuery);
//if filter on ambiance
$ambianceQuery = new \Elastica_Query_Term();
$ambianceQuery->setTerm('ambiance', $arrFilters['ambiance']);
$nestedAmbianceQuery = new \Elastica_Query_Nested();
$nestedAmbianceQuery->setPath('hotels');
$nestedAmbianceQuery->setQuery($ambianceQuery);
$filtersQuery->addMust($nestedAmbianceQuery);
//adding the parameters to the main query
$mainQuery->addMust($nameQuery);
$mainQuery->addMust($filtersQuery);
Unfortunately this doesn't work and returns no result if the Ambiance filter is activated, but works perfectly if I only search with the name.
What do I do wrong ?
I found why it wouldn't work.
The bundle actually uses __toString() on the object.
So, instead of querying on the "id" of the ambiance, I modified my html inputs so the value is the ambiance's name.
Here's my own version of the solution :
According to the elasticsearch documentation we should implement a structure that's smilar to the json bellow :
{
"query": {
"bool": {
"must": [
{ "match": { "title": "eggs" }},
{
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{ "match": { "comments.name": "john" }},
{ "match": { "comments.age": 28 }}
]
}}}}
]
}}}
So to do this with symfony 2 and according to the FOSElasticaBundle bundle we will make the following code lines :
//if filter on ambiance
$ambianceQuery = new \Elastica_Query_Term();
$ambianceQuery->setTerm('ambiance', $arrFilters['ambiance']);
// We will add the the term to the query bool
$filtersQuery->addMust($ambianceQuery)
$nestedAmbianceQuery = new \Elastica_Query_Nested();
$nestedAmbianceQuery->setPath('hotels');
// we will set the result as argument in the setQuery method.
$nestedAmbianceQuery->setQuery($filtersQuery);
// And finally we add the nested query to the main query bool through addMust.
$mainQuery->addMust($nestedAmbianceQuery);
Hope that will help others.
A+
Just had the same Problem. This works for me:
..
$ambianceQuery->setTerm('hotels.ambiance', $arrFilters['ambiance']);
..
Coudn't find any examples on this in FOQElasticaBundle (now FOSElasticaBundle) for Symfony2 but it should end in an elasticsearch query like this one here:
http://www.elasticsearch.org/guide/reference/query-dsl/nested-query/
One can do/test raw elasticsearch queries like this:
$queryString = '{..JSON..}';
$boolQuery = new \Elastica_Query_Builder($queryString);
I'm trying to read entities from a Drupal Services endpoint into my Sencha Touch 2 application. The JSON output looks like this (simplified):
{
nid: 1
title: 'Test'
body: {
'en': [
'This is a test.'
]
}
}
Thats the model's coffeescript code:
Ext.define 'Node',
extend: 'Ext.data.Model'
config:
idProperty: 'nid'
fields: [
{ name: 'nid', type: 'integer' }
{ name: 'title', type: 'string' }
{ name: 'language', type: 'string' }
{ name: 'body', type: 'auto', convert: convertField }
]
proxy:
type: 'jsonp'
url: 'http://www.mydomain.com/rest/node'
convertField = (value, record) ->
console.log value # always "undefined"
return 'test'
Loading the model with a jsonp proxy works, but only the atomic fields (like "nid" and "title") are populated. I tried to add a "convert" function to the models body field, but the value parameter is always undefined.
Is there a way to load complex json data into a models field? Or do I have to use the Model-relations system (which would be a lot of mess ...). I also thought about overriding a Ext.data.Reader, but i don't really know where to start.