Symfony Algolia Search Engine Association Indexing - symfony

I have a product entity, and each product is assigned to a category which is a separate category entity which is joined with a categoryId property in my product Entity. I have indexed all of the properties that I need such as name price etc, but I can't get my categoryId to index correctly:
When this it is indexed I get an array but not with the actual category Id or other information associated in that category:
So my question is, what is the correct way of going about this so that the category my product is assigned to is indexed correctly?
Thanks

You have two problems:
First:
You put on property and method, you must chose one.
Second:
With symfony when you make a ManyToOne like this
$this->categoryId
return an object, you can't map entire object with algolia attribute.
I suggest you to just make one method like this:
/*
* #Algolia\Attribute
*/
function getCategoryName() {
return $this->categoryId->getName();
}

Related

symfony 4 easyadmin add association field and select other field in this entity

I want to use a field in an entity that is linked in another entity that I use in my controller. I use easyadmin 3 and symfony 4. How to make an AssociationField in this condition? Can someone give me a hand? thanks
Example:
In a "payment" table, there is a "debt_id" field (related to the payment table).
And in the "debt" table, I want to select another field (for example, the paid field).
Let's say that you added in your payment CrudController a field like that:
AssociationField::new('debt_id'),
So now you should add this code in your Debt Entity:
public function __toString(): string
{
return $this->yourPaidField;
}

Doctrine QueryBuilder custom field, constructed from two different

I've searching for the solution to my problem, I wouldn't call it a problem, but I just can't find any documentation.
Basically what I want to do is:
I have two entities: Gallery & PhotoItem
PhotoItem has these fields: id, image, updatedAt
image field holds the name of the photo item with its extension.
When I get certain Gallery with joins to PhotoItem. I would also like to get additional field, which would be a CONCAT of %certain.parameter.with.baseurl.of.website% and the image field. So my API would return full path to the asset.
Is it possible to do it in QueryBuilder? Because right now I would loop through elements in PHP and add additional property.

Doctrine2 Many To Many Polymorphic Relations

I try to create a Many To Many Polymorphic Relations with Doctrine2 in Symfony2.
I would like a single entity that is associate dynamically with multiples entities.
I want to get this following schema:
posts
id: integer,
name: string
======
videos
id - integer
name - string
======
tags
id - integer
name - string
======
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
In taggables entity :
tag_id is the of the associate tag
taggable_id is the id of associate post
taggable_type is the type of associate entity ie 'Posts"
And I would like it to be the same with the "video" where:
tag_id represents the id of associate tag
taggable_id is the id of associate videos
taggable_type is the name of associate entity ie 'Videos'
and all this without duplicating table.
i'ved test multiple solutions but i never got this result :/
Thank you in advance for your help.
You could solve this with OOP, using inheritance.
Define an abstract class Taggable, and make Post and Video extend that class. Then create a OneToMany from Tag to Taggable.
Doctrine will take care of everithing, supposed that you choose between Single Table Inheritance or Class Table Inheritance.
I would choose Class Table, though.
More on this subject here.

Symfony2 : datatransformer for child object

Let's say I have Category hasMany Product relation.
Product entity has name and price columns.
The problem:
I want to edit Category entity with its collection of Product entities using data transformers. For example:
Edit form is empty. I click on "add product", javascript would call ajax and new Product will be created. That newly created product has id that will be injected into the main form (as hidden), together with field for name and price.
So I need transformer that will understand the hidden ID and edit name and price on that object.
What I have is kinda big code and Symfony tries to create new object.
NOTE:
I am aware how to work with collections but my real relation is not as in this example and javascript/ajax must create new Product.

Specifying index with Symfony2 for Doctrine's PersistentCollection

Can anybody tell me how I can configure Symfony2/Doctrine to index objects within a PersistentCollection, from a specified column in the entity?
For example assume we have two tables; categories and products, with many products to one category. When I use the repository to load a category, and then call getProducts() to load the products, I want them to be returned indexed by the product ID, or whatever column I specify. Currently they're just indexed incrementally from zero.
Is there anyway to do this, or do I need to loop through and manually set the keys myself?
Mapping OneToMany associations allows you to define an indexBy property. With annotation mappings, it would look like following
class Category
{
// ...
/**
* #OneToMany(targetEntity="Products", indexBy="productId", mappedBy="product")
*/
protected $products;
// ...
}
XML, YAML and PHP mappings allow this too, and this works also with ManyToMany.

Resources