Running function on Sonata Admin List field - symfony

In my application, backend users can segment frontend members through criteria like age, gender etc. These criteria are saved in a class called Segment.
AppBundle\Entity\Segment:
type: entity
table: segments
fields:
id:
id: true
type: integer
generator:
strategy: IDENTITY
name:
type: string
length: 300
created_at:
type: datetime
updated_at:
type: datetime
nullable: true
oneToMany:
parameters:
targetEntity: SegmentFilter
mappedBy: segment
cascade: [persist]
manyToOne:
owner:
targetEntity: Application\Sonata\UserBundle\Entity\User
inversedBy: null
joinColumn:
created_by:
referencedColumnName: id
The SegmentFilter class holds the parameters for filtering the members eg. criteria can be 'age', 'description' can be null but its for clarity purposes for whoever is reading the criteria and finally value can be '>= 30'
AppBundle\Entity\SegmentFilter:
type: entity
table: segment_filters
fields:
id:
id: true
type: integer
generator:
strategy: IDENTITY
criteria:
type: string
length: 50
description:
type: string
length: 300
value:
type: string
length: 300
manyToOne:
segment:
targetEntity: Segment
inversedBy: paramemters
joinColumn:
segmentation_id:
referencedColumnName: id
Here's the member's class to be segmented based on the parameters defined for each segment:
AppBundle\Entity\Member:
type: entity
table: members
fields:
id:
id: true
type: integer
generator:
strategy: IDENTITY
first_name:
type: string
length: 20
last_name:
type: string
length: 20
mobile:
type: string
length: 11
address:
type: string
length: 255
zip:
type: string
length: 6
town:
type: string
length: 25
email:
type: string
length: 50
dob:
type: date
In the list of segments, I would like to add an extra field that shows how many members currently fall in each segment created. How would I go about this?

Related

Symfony2 > Doctrine > double oneToMany combined key

I'm trying to setup the relation between 'Products' and 'Merchants' through a linking table 'MerchantProduct' where I can store/override additional information. So the primary key consists of a combined key. I've tried to simplify the amount of fields per Entity to enhance the readability. The three Entities already worked fine before I tried to add the relations in YML, but I would like to go 'all the way' with Doctrine.
The error I get is:
[Doctrine\ORM\ORMException]
Column name id referenced for relation from
BLAAT\Bundle\AdminBundle\Entity\Core\Product towards
BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProduct does not exist.
I'm not really sure why I get this error and don't know how to solve it. I'm afraid it has to do with the combined primary key.
Linking-entity with extra override information
BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProduct:
type: entity
table: MerchantProduct
repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProductRepository
id:
merchantId:
type: integer
column: merchant_id
productId:
type: string
length: 255
column: product_id
fields:
inStock:
type: boolean
column: in_stock
nullable: TRUE
productPrice:
type: float
column: product_price
nullable: TRUE
productDescription:
type: text
column: product_description
nullable: TRUE
oneToMany:
products:
targetEntity: Product
mappedBy: product_id
oneToMany:
outlets:
targetEntity: Merchant
mappedBy: merchant_id
lifecycleCallbacks: { }
Product-entity
BLAAT\Bundle\AdminBundle\Entity\Core\Product:
type: entity
table: Product
repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\ProductRepository
id:
id:
type: string
length: 255
id: true
generator:
strategy: NONE
fields:
title:
type: string
length: 255
defaultDescription:
type: text
column: default_description
nullable: TRUE
defaultPrice:
type: float
column: default_price
nullable: TRUE
manyToOne:
sellers:
targetEntity: MerchantProduct
inversedBy: products
joinColumn:
name: product_id
referencedColumnName: id
lifecycleCallbacks: { }
Merchant-entity
BLAAT\Bundle\AdminBundle\Entity\Core\Merchant:
type: entity
table: null
repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\MerchantRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
address:
type: string
length: 255
manyToOne:
stock:
targetEntity: MerchantProduct
inversedBy: outlets
joinColumn:
name: merchant_id
referencedColumnName: id
lifecycleCallbacks: { }
I think the mappedBys in MerchantProduct should be sellers and stock and that the referencedColumnNames should be productId and merchantId (or possibly product_id and merchant_id -- I haven't tested it, but something that exists in MerchantProduct, in any case).
It's the latter that seems to cause the error, because there is no column id: it should be productId.
To deal with a compound primary key, I think you need to use multiple JoinColumns to define the manyToOne relation in Merchant and Product. If I'm reading the documentation correctly, you can combine multiple JoinColumns under JoinColumns.
So:
manyToOne:
stock:
targetEntity: MerchantProduct
inversedBy: outlets
joinColumns:
joinColumn:
name: merchant_id
referencedColumnName: merchantId
joinColumn:
name: product_id
referencedColumnName: productId

Symfony doctrine manyToOne relationship with a composite id

In a Symfony2 app, I have two entites: Club and Member.
I have to use a composite primary keys with four specific fields (edition, distance, district and id). Here is the ORM yml. The Club schema is correctly generated.
MyBundle\Entity\Club:
type: entity
oneToMany:
members:
targetEntity: Member
mappedBy: club
table: null
id:
edition:
type: string
length: 4
distance:
type: string
length: 1
district:
type: integer
id:
type: integer
fields:
name:
type: string
length: 255
location:
type: string
length: 255
lifecycleCallbacks: { }
My problem is now to link the Member entity. Here is my current ORM yml:
MyBundle\Entity\Member:
type: entity
manyToOne:
club:
targetEntity: Club
inversedBy: members
joinColumn:
name: club_id
referencedColumnName: ??? What if composite PK ???
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
lastname:
type: string
length: 255
firstname:
type: string
length: 255
...
What to set as referencedColumnName? How to set up the reference with a composite FK?
EDIT
I found that is possible to set multiple join colums with annotation #joinColumns{#joinColumn...#joinColumn...}. Something similar for yml configuration? I can't find any example.
Try something like this, I think it's the correct YML syntax:
manyToOne:
club:
targetEntity: Club
inversedBy: members
joinColumns:
- joinColumn:
name: club_id
referencedColumnName: id
- joinColumn:
name: club_edition
referencedColumnName: edition
- joinColumn:
name: club_distance
referencedColumnName: distance
- joinColumn:
name: club_district
referencedColumnName: district
(However, for your continued sanity, I'd strongly recommend trying to avoid composite keys where possible!)

How to specify field name

I had next entity with yaml format:
BW\UserBundle\Entity\User:
type: entity
table: users
repositoryClass: BW\UserBundle\Entity\UserRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
username:
type: string
length: 25
unique: true
password:
type: string
length: 64
email:
type: string
length: 60
unique: true
isActive:
type: boolean
lifecycleCallbacks: { }
Now, when I update scheme, I get isActive field name in DB, same as property name.
How can I specify property name isActive like is_active field name in DB?
You need to specify a column name like this:
BW\UserBundle\Entity\User:
type: entity
table: users
repositoryClass: BW\UserBundle\Entity\UserRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
username:
type: string
length: 25
unique: true
password:
type: string
length: 64
email:
type: string
length: 60
unique: true
isActive:
type: boolean
column: is_active
lifecycleCallbacks: { }
More on that - http://docs.doctrine-project.org/en/2.0.x/reference/basic-mapping.html#property-mapping
You can specify the column name for the database
fields:
username:
type: string
length: 25
unique: true
password:
type: string
length: 64
email:
type: string
length: 60
unique: true
isActive:
type: boolean
column: is_active
You dealing with two different kind of conventions here. It is common in Symfony to use camel case for naming variables. Within databases you often find this underscore written names. If you access your isActive property your entity model will map it to the is_active column in the database.

Multiple many to many relationships in symfony2

I have 3 entities:
Song
Artist
Genre
One song can have multiple genres and vice versa. A song can also have multiple artists and vice versa.
That's why I'm trying to create two many-to-many relationships. However, when generating the Entity clases, the first of the two is always 'overridden' by the second one and no variable or accessors are created for that one..
Here's my yaml: (updated)
Foo\BarBundle\Entity\Song:
type: entity
table: song
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string
length: 300
mime_type:
type: string
length: 20
filesize:
type: integer
length: 20
length:
type: string
length: 20
created_at:
type: datetime
gedmo:
timestampable:
on: create
manyToOne:
user:
targetEntity: Spotaset\UserBundle\Entity\User
inversedBy: songs
manyToMany:
genres:
targetEntity: Genre
inversedBy: songs
manyToMany:
artists:
targetEntity: Artist
inversedBy: songs
Foo\BarBundle\Entity\Artist:
type: entity
table: artist
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 100
unique: true
manyToMany:
songs:
targetEntity: Song
mappedBy: artistss
Foo\BarBundle\Entity\Genre:
type: entity
table: genre
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 100
unique: true
manyToMany:
songs:
targetEntity: Song
mappedBy: genres
I don't know a lot about relational databases so I don't know wether this is a bug or that this design is just bad in general..
All help is greatly appreciated!
Finally solved this (stupid) issue. Turned out I had to define both many to many under one manyToMany heading like this:
manyToMany:
genres:
targetEntity: Genre
inversedBy: songs
artists:
targetEntity: Artist
inversedBy: songs

Entity class not found when trying to create entities from yml config files

I'm trying to create entity classes. I am using yml files from Resources/config/doctrine folder.
Category.orm.yml
Marek\JobeetBundle\Entity\Category:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: IDENTITY
name:
type: string
length: '255'
unique: true
manyToMany:
affiliates:
targetEntity: Marek\JobeetBundle\Entity\Affiliate
joinTable:
name: CategoryAffiliate
joinColumns:
category_id:
referencedColumnName: id
inverseJoinColumns:
affiliate_id:
referencedColumnName: id
lifecycleCallbacks: { }
Affilate.orm.yml
Marek\JobeetBundle\Entity\Affiliate:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: IDENTITY
url:
type: string
length: '255'
nullable: true
email:
type: string
length: '255'
nullable: true
unique: true
token:
type: string
length: '255'
isActive:
type: boolean
nullable: false
column: is_active
default: 0
createdAt:
type: datetime
column: created_at
gedmo:
timestampable:
on: create
manyToMany:
categories:
targetEntity: Marek\JobeetBundle\Entity\Category
mappedBy: affiliates
lifecycleCallbacks: { }
Job:
Marek\JobeetBundle\Entity\Job:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: IDENTITY
type:
type: string
length: '255'
company:
type: string
length: '255'
nullable: true
logo:
type: string
length: '255'
url:
type: string
length: '255'
nullable: true
position:
type: string
length: '255'
nullable: true
location:
type: string
length: '255'
description:
type: string
length: '4000'
howToApply:
type: string
length: '4000'
column: how_to_apply
token:
type: string
length: '255'
unique: true
isPublic:
type: boolean
length: null
column: is_public
isActivated:
type: boolean
length: null
column: is_activated
email:
type: string
length: '255'
createdAt:
type: datetime
column: created_at
gedmo:
timestampable:
on: create
updatedAt:
type: datetime
column: updated_at
gedmo:
timestampable:
on: update
expiresAt:
type: datetime
column: expires_at
oneToOne:
category:
targetEntity: Marek\JobeetBundle\Entity\Category
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
category_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
After executing:
php app/console doctrine:generate:entities JobeetBundle --path="src"
I am getting:
Warning: class_parent(): Class Marek\JobeetBundle\Entity\Affiliate
does not exists and could not be loaded in
vendor/gedmo-doctrine-extension\lib\Gedmo\Mapping\ExtensionMetadataFactory.php
on line 80.
I know that I have not any entities I want to create them.
Could somebody help?
This may be caused by an invalid assumption in Gedmo. Try commenting out the entire stof_doctrine_extensions block in your config.yml and then running the generate command. If it works, you should be able to uncomment the config and get back to work.

Resources