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
Related
My controller:
class LoggingController extends Controller
{
public function getLogAction()
{
$result = $this->getDoctrine()->getRepository(ChangeLog::class)->findAll();
if ($result === NULL) {
return new View("Log not found", Response::HTTP_NOT_FOUND);
}
return new View($result,Response::HTTP_OK);
}
}
My doctrine orm.yml:
AppBundle\Entity\ChangeLog:
type: entity
table: null
repositoryClass: AppBundle\Repository\ChangeLogRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
date:
type: datetime
#...and other
When I try to display my records from table, I receive this in date (date is empty):
But in my table in db everything are normal:
What is wrong? All another data displays normally...
I use FosRestBundle for displaying data in JSON format.
Im using FOSElasticaBundle to integrate my symfony3 project with elasticsearch. I have a mapping similar to:
company:
mappings:
id: { boost: 1, type: string, index: not_analyzed }
cik: { boost: 2, type: string, index: not_analyzed }
conformedName: { boost: 6, analyzer: custom_analyzer }
assignedSIC:
type: object
properties:
id: { type: string, index: not_analyzed }
code: { type: string, index: not_analyzed }
title: { analyzer: custom_analyzer }
businessAddress:
type: object
properties:
street1: { analyzer: custom_analyzer }
street2: { analyzer: custom_analyzer }
city: { analyzer: custom_analyzer }
state: { analyzer: custom_analyzer }
zip: { type: string, index: not_analyzed }
phone: { type: string, index: not_analyzed }
I want to filter by city, state and zip of the nested businessAddress property.
I have this query:
$boolQuery = new BoolQuery();
$cityQuery = new Match();
$cityQuery->setFieldQuery('businessAddress.city', array($city]));
$cityQuery->setFieldParam('businessAddress.city', 'analyzer', 'custom_analyzer');
$boolQuery->addMust($cityQuery);
$this->finder->find($boolQuery);
json query as
{"query":{"bool":{"must":[{"match":{"businessAddress.city":{"query":["NY"],"analyzer":"custom_analyzer"}}}]}}}
But have 0 results, i dont know if the sintax businessAddress.city will be handled automatically by the bundle or do i need to create a nested query. In case that is a nested query how i can build that?
EDIT
After some comments below i notice i was setting match term as array, now i change from:
$cityQuery->setFieldQuery('businessAddress.city', array($city]));
to
$cityQuery->setFieldQuery('businessAddress.city', $city]);
resulting on json query:
{"query":{"bool":{"must":[{"match":{"businessAddress.state":{"query":"NY","analyzer":"custom_analyzer"}}}]}}}
i have check over internet and found nothing.
Please help. Thanks
Here is how to implement it with a nested query.
First of all you need to update your mapping to make your businessAddressa type nested
...
businessAddress:
type: nested
...
Then you will be able to perform the following Nested query
$boolQuery = new BoolQuery();
$cityQuery = new Match();
$cityQuery->setFieldQuery('businessAddress.city', array($city]));
$cityQuery->setFieldParam('businessAddress.city', 'analyzer', 'custom_analyzer');
$boolQuery->addMust($cityQuery);
// nested query
$query = new Nested();
$query->setPath('businessAddress');
$query->setQuery($boolQuery);
// pass the nested query to your finder
$this->finder->find($query);
Hope this helps
I am working with a bundle called rss-atom-bundle. Through this bundle I should be able to fetch the RSS feeds and save them to the database.
I can get the bundle to work up till getting the feeds from the URL but when I try to presist I get the following error and I just cant figure it out why I am getting it.
The class 'Debril\RssAtomBundle\Protocol\Parser\Item' was not found in the chain configured namespaces Symfony\Bundle\AsseticBundle\Entity, AppBundle\Entity`
Looking online and at stackoverflow i came across questions that were related to this but all of them are talking about by default the Entity should be inside the Entity folder of the Bundle and if they are not then this error appears but my Entity files are inside Entity folder and I followed the instructions given by the bundle author but still i cant get passed this error
This is what my Controller looks like
$em = $this->getDoctrine()->getManager();
// fetch the FeedReader
$reader = $this->container->get('debril.reader');
// this date is used to fetch only the latest items
$unmodifiedSince = '01/01/2000';
$date = new \DateTime($unmodifiedSince);
// the feed you want to read
$url = 'http://example.com/feed/';
$feeds = $reader->getFeedContent($url, $date);
$items = $feeds->getItems();
dump($items);
foreach ( $items as $item ) {
$em->persist($item);
}
$em->flush();
As per bundle instructions I did implement FeedInterface to my Feed Entity and ItemInInterface, ItemOutInterface to my Item Entity
This is what my orm.yml looks like for Feed and Item Entity
Feed
AppBundle\Entity\Feed:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
description:
type: text
link:
type: text
publicId:
type: text
lastModified:
type: datetime
lastViewed:
type: datetime
oneToMany:
items:
targetEntity: AppBundle\Entity\Item
mappedBy: feed
cascade: ["persist"]
lifecycleCallbacks: { }
Item
AppBundle\Entity\Item:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
title:
type: text
nullable: true
summary:
type: text
nullable: true
description:
type: text
nullable: true
medias:
type: text
nullable: true
updated:
type: string
nullable: true
publicId:
type: string
nullable: true
link:
type: string
nullable: true
comment:
type: string
nullable: true
author:
type: string
nullable: true
manyToOne:
feed:
targetEntity: AppBundle\Entity\Feed
inversedBy: items
joinColumn:
name: feed_id
referencedColumnName: id
Any help will be really appreciated as I am clueless why i am getting the error?
Try to add RssAtomBundle to the list with mappings in app/config/config.yml
orm:
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
AsseticBundle: ~
RssAtomBundle: ~
My title might not be the best description, but it's best I could come up with.
I have 4 entities; Page, PageElement, Image and an entity called TextWithImage. Page holds pageElements (array of PageElement entities). Those pageElements can be of numerous types, but for now I have only one called TextWithImage that's holding additional data to the data the PageElement entity holds.
The PageElement can be included on numerous pages and so, I have a ManyToMany in the PageElement.orm.yml. The TextWithImage has a manyToOne to reference to Image.
(More information: Another Entity ImageGallery might have a manyToMany relationship with the Image entity, while TextOnly shouldn't have any reference to the Image entity.)
I want to be able to get the Page and retrieve the PageElements with all their "attributes". So let's say I request to get a Page with only one TextWithImage type of PageElement, I want to return the following.
Page -> pageElements = array (
[0] => TextWithImage -> image = Image -> filename = "image.png"
-> alt = "An image!"
-> text = "There's an image too!"
)
All seems simple enough, but I need doctrine to understand that this PageElement is a TextWithImage type. Can I do this with a DiscriminatorColumn, say (rough sketch);
Table: pageelement
id | attributes | discr | TextWithImageId
Table: textwithimage
id | attributes
Keep in mind that I'll have more than just one type of PageElement, not only TextWithImage.
Is this possible and if so, how?
I've found the solution to my problem. These are the doctrine YML files. You can generate all entities with php app/console doctrine:generate:entities AppBundle/Entity. Make sure that the PageTextImageElement class extends the PageElement class.
Page.orm.yml
AppBundle\Entity\Page:
type: entity
table: null
repositoryClass: AppBundle\Repositories\PageRepository
manyToMany:
pageElements:
targetEntity: PageElement
cascade: ["all"]
joinTable:
name: null
joinColumns:
page_id:
referencedColumnName: id
onDelete: CASCADE
inverseJoinColumns:
page_element_id:
referencedColumnName: id
unique: true
onDelete: CASCADE
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: '255'
unique: true
lifecycleCallbacks: { }
PageElement.orm.yml
AppBundle\Entity\PageElement:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
discriminatorMap:
pageTextImageElement: PageTextImageElement
table: null
repositoryClass: AppBundle\Repositories\PageElementRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
sortOrder:
type: integer
attributes:
type: array
nullable: true
lifecycleCallbacks: { }
PageTextImageElement.orm.yml
AppBundle\Entity\PageTextImageElement:
type: entity
table: null
oneToOne:
image:
targetEntity: AppBundle\Entity\Image
joinColumn:
name: imageId
referencedColumnName: id
fields:
passage:
type: string
length: '255'
lifecycleCallbacks: { }
Image.orm.yml
AppBundle\Entity\Image:
type: entity
table: null
repositoryClass: AppBundle\Repositories\ImageRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: '255'
unique: true
description:
type: string
length: '255'
lifecycleCallbacks: { }
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