doctrine orm manytoone multi-column join strange error - symfony

I'm "enhancing" a legacy datamodel with referential integrity using doctrine ORM inside Symfony.
I have this schema yml definition
AppBundle\Entity\QualityDataItems:
type: entity
table: quality_data_items
id:
rfi_id:
type: integer
options:
unsigned: false
id: true
rfi_type:
type: string
length: 10
id: true
manyToOne:
RfiPeople:
targetEntity: QualityDataItemsPeople
joinColumn:
rfi_type:
referencedColumnName: rfi_type_id
rfi_id:
referencedColumnName: rfi_id
fields:
issue_text:
type: text
nullable: true
length: 65535
response_text:
type: text
nullable: true
length: 65535
resolution_text:
type: text
nullable: true
length: 65535
add_to_report:
type: integer
nullable: true
options:
unsigned: false
default: '0'
add_to_report_date:
type: datetime
nullable: true
issue_cause:
type: integer
nullable: true
issue_type:
type: integer
nullable: true
issue_sub_type:
type: integer
nullable: true
linked_to_id:
type: integer
nullable: true
status_id:
type: integer
nullable: true
AppBundle\Entity\QualityDataItemsPeople:
type: entity
table: quality_data_items_people
id:
rfi_ppl_idx:
type: integer
generator: { strategy: AUTO }
manyToOne:
RfiJob:
targetEntity: RfiJob
joinColumn:
name: rfi_id
referencedColumnName: rfi_id
RfiType:
targetEntity: RfiType
joinColumn:
name: rfi_type
referencedColumnName: rfi_type_id
fields:
rfi_id:
type: integer
rfi_type:
type: string
length: 10
user_id:
type: integer
when I run
bin/console doctrine:generate:entities AppBundle
bin/console doctrine:schema:update --dump-sql
I get:
[Doctrine\ORM\ORMException] Column name 'id' referenced for relation from AppBundle\Entity\QualityDataItems towards AppBundle\Entity\QualityDataItemsPeople does not exist.
There is no column named id in these tables. I also deleted the Entity classes to be sure there is nothing in there

Either it's not documented or supported, but could you give this a try?
manyToOne:
RfiPeople:
targetEntity: QualityDataItemsPeople
joinColumns:
- joinColumn:
name: rfi_type_id
referencedColumnName: rfi_type_id
- joinColumn:
name: rfi_id
referencedColumnName: rfi_id

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 ManyToMany Relation 'The table with name already exists

I have Entity Party. Shortly it looks like this:
AppBundle\Entity\Main\Party:
type: entity
table: main.party
id:
id:
type: integer
nullable: false
id: true
generator:
strategy: IDENTITY
fields:
legalName:
type: string
nullable: false
length: 255
options:
fixed: false
column: legal_name
manyToMany:
partiesForPartner:
targetEntity: AppBundle\Entity\Main\Party
mappedBy: partnersForParty
fetch: EXTRA_LAZY
cache:
usage: NONSTRICT_READ_WRITE
partnersForParty:
cascade: ["all"]
targetEntity: AppBundle\Entity\Main\Party
inversedBy: partiesForPartner
fetch: LAZY
joinTable:
name: main.party_partnership
joinColumns:
party_id:
referencedColumnName: id
inverseJoinColumns:
partner_id:
referencedColumnName: id
Also there is one more entity PartyPartnership. which includes some additional props.
AppBundle\Entity\Main\PartyPartnership:
type: entity
repositoryClass: AppBundle\Repository\Main\PartyPartnershipRepository
table: main.party_partnership
uniqueConstraints:
party_partnership_ukey:
columns:
- party_id
- partner_id
id:
id:
type: integer
nullable: false
options:
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
codePartyByPartner:
type: string
nullable: true
length: 35
options:
fixed: false
column: code_party_by_partner
codePartnerByParty:
type: string
nullable: true
length: 35
options:
fixed: false
column: code_partner_by_party
manyToOne:
party:
targetEntity: AppBundle\Entity\Main\Party
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
party_id:
referencedColumnName: id
orphanRemoval: false
partner:
targetEntity: AppBundle\Entity\Main\Party
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
partner_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
All this mapping was imported from DB except of many-to-many association in Party. Now when i try to start doctrine commands like 'doctrine:schema:validate' or 'doctrine:schema:update'. I get an error 'The table with name 'main.party_partnership' already exists.'
Symfony ManyToMany creates a table between the two entities linked.
It means it automatically create the table main.party_partnership between party and partnership.
Party ---(ManyToMany main.party_partnership)---> Partnership
You just need to reference as oneToMany, and remove the manyToMany
Party ---(OneToMany)---> PartyPartnership
Partnership ---(OneToMany)---> PartyPartnership
What worked for me after several hours: clear cache.

Doctrine\DBAL\Schema\SchemaException: How to include one or more FKs in a declared table index

I am using Symfony/YaML/Doctrine to store data in an MySQL backend. I want to create an index (to speed queries), using some of the FKs I have declared. However, when I try to generate the schema, I get the following error:
[Doctrine\DBAL\Schema\SchemaException] There is no column with name
'to_user_id' on table 'pms'
When I comment out the index declaration, I am able to generate the table without any problems. This is what my YaML file looks like:
AppBundle\Entity\PrivateMessage:
type: entity
table: pms
repositoryClass: PMSRepository
id:
id:
type: integer
generator: { strategy: AUTO }
manyToOne:
from_user:
targetEntity: User
joinColumn:
name: from_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
manyToOne:
to_user:
targetEntity: User
joinColumn:
name: to_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
manyToOne:
message_type:
targetEntity: PrivateMessageType
joinColumn:
name: pms_type_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
manyToOne:
message_status:
targetEntity: PrivateMessageStatus
joinColumn:
name: pms_status_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
indexes:
idx_pms:
columns: [from_user_id, to_user_id, created_at]
fields:
subject:
type: text
nullable: false
body:
type: text
nullable: false
attach_1:
type: string
length: 128
nullable: true
attach_2:
type: string
length: 128
nullable: true
attach_3:
type: string
length: 128
nullable: true
created_at:
type: datetime
The problem is simple: do not split all the assotiations in several manyToOne. Correct variant should be like
AppBundle\Entity\PrivateMessage:
type: entity
table: pms
repositoryClass: PMSRepository
id:
id:
type: integer
generator: { strategy: AUTO }
manyToOne:
from_user:
targetEntity: User
joinColumn:
name: from_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
to_user:
targetEntity: User
joinColumn:
name: to_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
message_type:
targetEntity: PrivateMessageType
joinColumn:
name: pms_type_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
message_status:
targetEntity: PrivateMessageStatus
joinColumn:
name: pms_status_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
indexes:
idx_pms:
columns: [from_user_id, to_user_id, created_at]
fields:
subject:
type: text
nullable: false
body:
type: text
nullable: false
attach_1:
type: string
length: 128
nullable: true
attach_2:
type: string
length: 128
nullable: true
attach_3:
type: string
length: 128
nullable: true
created_at:
type: datetime

Gedmo Tree Extension: Unable to find ancestor/parent child relation

I'm trying to use tree extension of Gedmo with YAML format but when I try to update the schema it returns the following error:
[Gedmo\Exception\InvalidMappingException]
Unable to find ancestor/parent child relation through ancestor field - [parent] in class -
The following code is the MyEntity.orm.yml file:
Project\MyBundle\Entity\MyEntity:
type: entity
repositoryClass: Gedmo\Tree\Entity\Repository\NestedTreeRepository
gedmo:
tree:
type: nested
id:
id:
type: string
nullable: false
generator:
strategy: UUID
fields:
name:
type: string
length: 200
locale:
type: string
length: 6
lft:
type: integer
gedmo:
- treeLeft
lvl:
type: integer
gedmo:
- treeLevel
rgt:
type: integer
gedmo:
- treeRight
root:
type: string
nullable: true
gedmo:
- treeRoot
manyToOne:
parent:
targetEntity: Project\MyBundle\Model\MyEntityInterface
inversedBy: children
joinColumn:
name: parent_id
referencedColumnName: id
onDelete: CASCADE
gedmo:
- treeParent
oneToMany:
children:
targetEntity: Project\MyBundle\Model\MyEntityInterface
mappedBy: parent
orderBy:
lft: ASC
It cant be interface, it has to be a entity
find
targetEntity: Project\MyBundle\Model\MyEntityInterface
change to:
targetEntity: Project\MyBundle\Entity\MyEntity

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