Extending models with additional fields in Doctrine - symfony

I'm trying to figure out a way to extend a model in Doctrine. I have the following situation
Card Model (The base model):
Nos\CardBundle\Entity\Card:
type: entity
table: cards
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: card_type
type: string
discriminatorMap:
staff: Nos\StaffBundle\Entity\Staff
supplier: Nos\SupplierBundle\Entity\Supplier
id:
id:
id: true
type: integer
length: 11
generator:
strategy: AUTO
options:
unsigned: true
fields:
user_id:
type: integer
length: 11
nullable: true
options:
unsigned: true
card_display_name:
type: string
length: 255
nullable: false
I discriminate the model using the card_type property. When it's 'staff', it should return a Staff model, when it's 'supplier' it should return a Supplier Model
Staff Model (Extends Card model)
Nos\StaffBundle\Entity\Staff:
type: entity
inheritance:
extends: Nos\CardBundle\Entity\Card
type: simple
staff_initials:
type: string
length: 15
staff_baptismal_names:
type: string
length: 255
and the Supplier model
Nos\StaffBundle\Entity\Supplier:
type: entity
inheritance:
extends: Nos\CardBundle\Entity\Card
type: simple
supplier_name:
type: string
length: 255
When I generate the tables, the additional fields in Supplier and Staff models are not being created. Do I have to define those additional fields in the Card model? And if so, how can I exclude them from the models that they don't belong to?

Related

Symfony doctrine empty relation in behat tests

I have a problem with behat in symfony 2. I have two entities:
//Transaction
DM\MultiStepFormBundle\Entity\Transaction:
type: entity
repositoryClass: DM\MultiStepFormBundle\Repository\Transaction\TransactionRepository
table: rb_multistepform_transaction
indexes:
transaction_id_index:
columns: [ transaction_id ]
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
created_at:
type: datetime
nullable: false
transaction_id:
type: string
length: 128
nullable: false
is_client_new:
type: boolean
options:
default: false
utm_source:
type: string
length: 255
nullable: true
utm_medium:
type: string
length: 255
nullable: true
utm_campaign:
type: string
length: 255
nullable: true
user_agent:
type: text
nullable: true
http_referer:
type: string
length: 255
nullable: true
ip:
type: string
length: 32
nullable: true
status:
type: smallint
nullable: false
options:
default: 0
continue_service_run:
type: boolean
nullable: false
options:
default: false
continue_serivce_run_datetime:
type: datetime
nullable: true
oneToMany:
items:
targetEntity: DM\MultiStepFormBundle\Entity\TransactionItem
mappedBy: transaction
cascade: [persist, merge, remove]
manyToOne:
client:
targetEntity: DM\KlienciBundle\Entity\Client
inversedBy: multistepform_transactions
joinColumn:
name: client_id
referencedColumnName: id
//TransactionItem
DM\MultiStepFormBundle\Entity\TransactionItem:
type: entity
repositoryClass: DM\MultiStepFormBundle\Repository\Transaction\TransactionRepository
table: rb_multistepform_transaction_item
id:
id:
type: integer
generator: { strategy: AUTO }
indexes:
partner_status_index:
columns: [ partner_name, status ]
fields:
type:
type: smallint
nullable: false
partner_name:
type: string
length: 128
nullable: true
status:
type: smallint
nullable: false
options:
default: 0
link_id:
type: integer
nullable: true
product_id:
type: integer
nullable: true
pixelconversion_hash:
type: string
length: 128
nullable: true
partner_api_response:
type: json_array
nullable: true
clicked:
type: boolean
nullable: false
options:
default: 0
clicked_datetime:
type: datetime
nullable: true
order_nr:
type: integer
nullable: true
highlighted:
type: boolean
nullable: false
options:
default: false
manyToOne:
transaction:
targetEntity: DM\MultiStepFormBundle\Entity\Transaction
inversedBy: items
cascade: [persist, merge, remove]
joinColumn:
name: transaction_id
referencedColumnName: id
When i testing a service where I have such loops:
$transactions = $this->transactionRepository->getTransactionsByContinueServiceRunInDateRange(0, $minDateTime, $maxDateTime);
foreach ($transactions as $transactionKey => $singleTransaction) {
//in this place, relationships are not taken
$transactionItems = $singleTransaction->getItems();
//when it performs a test query items are downloaded correctly
$transactionItemsQuery = $this->transactionRepository->getItems($singleTransaction->getId());
}
In which i needs to get items from the reverse relation. With a normal call to the site in symfony, the relationships are correct and everything is ok.
In the case of activating test in behat, relations in the variable $transactionItems are an empty collection. The data surely exists in the database because in the variable $transactionItemsQuery as I get data from query I have correct data.
Has anyone ever encountered something like that in behat?

doctrine create manyToMany over third entity does not work properly

I want to creta a manyToMany relation between two entites not directly but by using a third entity with double manyToOne relations:
AppBundle\Entity\AttributeKey:
type: entity
table: attribute_keys
repositoryClass: AppBundle\Repository\AttributeKeyRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
label:
type: string
length: 255
unique: true
sort:
type: integer
AppBundle\Entity\AttributeValue:
type: entity
table: attribute_values
repositoryClass: AppBundle\Repository\AttributeValueRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
label:
type: string
length: 255
unique: true
AppBundle\Entity\AttributeKeyValue:
type: entity
table: attribute_keys_values
repositoryClass: AppBundle\Repository\AttributeKeyValueRepository
manyToOne:
attributeKey:
targetEntity: AttributeKey
attributeValue:
targetEntity: AttributeValue
manyToMany:
documents:
targetEntity: Document
mappedBy: attributes
id:
id:
type: integer
id: true
generator:
strategy: AUTO
In the database everything looks fine, the foreign keys are properly built. But the AttributeKey Entity that is generated by doctrine does not contain a method getValues() and AttributeValue does not have the method getKeys() or addKey()
What did I do wrong?
You mapped only the party that owns the relationship. You need to set up a two-way relationship.
Everything is explained here :
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional
and
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional
That's all :)

Running function on Sonata Admin List field

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?

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.

symfony2: trying to update the entities using "doctrine:generate:entities"

I have this file:
#src/Jander/JanderBundle/resources/config/doctrine/metadata/orm/
Propuestas.orm.yml
Propuestas:
type: entity
table: propuestas
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
contenido:
type: string
length: 230
fixed: false
nullable: false
lifecycleCallbacks: { }
I have just added a new field like this:
Propuestas:
type: entity
table: propuestas
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
contenido:
type: string
length: 230
fixed: false
nullable: false
contenido2:
type: string
length: 230
fixed: false
nullable: false
lifecycleCallbacks: { }
and then I'm trying to generate the entity with the new field and I
get this:
$ php app/console doctrine:generate:entities "JanderJanderBundle"
backing up Propuestas.php to Propuestas.php~
generating Volumus\VolumusBundle\Entity\Propuestas
but when I open Propuestas.php there isn't any reference about
"contenido2".
Any idea?
Clearing the cache solves the problem. This is strange, make sure your cache permissions are fine.

Resources