Doctrine manyToMany Definition not Working Undefined index: joinTable - symfony

I am trying to get a manyToMay bi-directional mapping working on Symfony 3.2.6 / PHP 7.1. I am unable to get the
php bin/console doctrine:schema:update --dump-sql
command to run without an error
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Undefined index: joinTable
Definition is as follows:
Busybee\StudentBundle\Entity\Student:
type: entity
table: student
repositoryClass: Busybee\StudentBundle\Repository\StudentRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
startAtSchool:
type: date
manyToMany:
enrolments:
targetEntity: Busybee\StudentBundle\Entity\Enrolment
mappedBy: students
and
Busybee\StudentBundle\Entity\Enrolment:
type: entity
table: enrolment
repositoryClass: Busybee\StudentBundle\Repository\EnrolmentRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
status:
type: string
length: '32'
lastModified:
type: datetime
createdOn:
type: datetime
manyToMany:
students:
targetEntity: Busybee\StudentBundle\Entity\Student
inversedBy: enrolments
If I remove the mappedBy in the Student Entity the SQL will generate using the doctrine:schema:update command. The example at http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association shows the joinTable index on the inversedBy entity, and adding the joinTable to this or the mappedBy entity still generates the error Undefined index: joinTable
So, what if anything am I doing wrong? Is this a bug? Any help much appreciated.
Craig

I found the issue here. Not a Doctrine problem, but a subscriber I had written to add a table prefix. I have updated the code for the Table Prefix subscriber to correctly capture when the definition is a manyToMany association and to ignore the non-owning side. I am now using the Table Prefix code from the Doctrine Site at http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/sql-table-prefixes.html
Craig.

Related

symfony3 entity generator yml bug

I don't have a lot of experience with symfony or doctrine but I'm in a weird situation.
I got a simple User and Post entity. The thing is that Users can post things when they are logged.
The problem is that I wanted to link my table "post" and "user" with post.user_id and user.id
So with doctrine I decided to make a simple manyToOne relation in Post :
(Post.orm.yml)
Whatever\PostBundle\Entity\Post:
type: entity
table: post
repositoryClass: Whatever\PostBundle\Repository\PostRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
content:
type: text
userId:
type: integer
column: user_id
createdAt:
type: datetime
column: created_at
manyToOne:
user:
targetEntity: Whatever\UserBundle\Entity\User
lifecycleCallbacks: { }
Entities generated and doctrine:schema:update done
Now I can get User informations with the posts as I wanted BUT ALSO can't post anything anymore because of user_id null
An exception occurred while executing 'INSERT INTO post (content,
user_id, created_at) VALUES (?, ?, ?)' with params ["content of the
post", null, "2017-04-21 11:27:04"]:
As you can see the user_id is null (I tried to set it manually in the controller, in the entity construct or with a form field, same result)
It's not over. Because if I comment/remove the part of Post.orm.yml :
manyToOne:
user:
targetEntity: Whatever\UserBundle\Entity\User
Then I can post again ... But I don't understand why.
(and btw I can't get users informations with the posts anymore. So I still need it)
Can someone give me an explanation ? I've been on this problem for 2 days. You're my last hope.
In your fields definitions, remove userId. Symfony will take care of that by your ManyToOne definition. See the docs for reference.
Whatever\PostBundle\Entity\Post:
type: entity
table: post
repositoryClass: Whatever\PostBundle\Repository\PostRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
content:
type: text
// Remove the next few lines
// userId:
// type: integer
// column: user_id
createdAt:
type: datetime
column: created_at
manyToOne:
user:
targetEntity: Whatever\UserBundle\Entity\User
lifecycleCallbacks: { }

"Invalid field override named 'id' for class ..." error while generating entities

How to create mapping.yaml with inheritance that is suitable for 'generate:entities' command? I tried like this:
Acme\EdgarMainBundle\Entity\Address:
type: entity
table: address
repositoryClass: AddressRepository
id:
id:
type: integer
autoincrement: true
generator:
strategy: AUTO
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
length: 255
discriminatorMap:
customeraddress: Acme\MyBundle\Entity\Customeraddress
corporationaddress: Acme\MyBundle\Entity\Corporationaddress
fields:
...
Acme\MyBundle\Entity\Customeraddress:
type: entity
repositoryClass: CustomeraddressRepository
fields:
...
Acme\MyBundle\Entity\Corporationaddress:
type: entity
repositoryClass: CorporationaddressRepository
fields:
...
but I have an error:
Entity class 'Acme\MyBundle\Entity\Customeraddress' used in the discriminator map of class 'Acme\EdgarMainBundle\Entity\Address' does not exist.
Yes, I can create it by myself, but is there way to do it with
doctrine:generate:entities MyBundle
command?
doctrine:generate:entities MyBundle
should be changed to
doctrine:generate:entities AcmeMyBundle
or
doctrine:generate:entities Acme/MyBundle
documentation: http://symfony.com/doc/current/book/doctrine.html#generating-getters-and-setters

Doctrine2 + symfony2 Keep history table on delete with FK

I have 2 tables. One let's say organizations and a second one is organization_history. Then I have a table actions_history with concrete action,... But it isn't so important in this case.
In Organization history I keep revision and organizationId. Everything works well until DELETE a table organization. My idea is to keep every action in history table. On INSERT, UPDATE and DELETE action. But problem is when I try to delete organization table. I got this output:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`database`.`base_organizationsHistory`, CONSTRAINT `FK_EAF457A532C8A3DE` FOREIGN KEY (`organization_id`) REFERENCES `base_organizations` (`id`))
1) Is it possible to just delete organization table and keep the history table and ignore foreign key. Or does exist a different and even clear solution?
2) I am thinking also about lifecycle callbacks. In INSERT and UPDATE action I connected tables easily, but when I can create history table on delete action it's not possible to use postRemove callback, because then I don't have the old data for copy to the history. And if I use preRemove callback it's not so clear. Does exist some better idea to do it?
Organization.orm.yml:
BaseBundle\Entity\Organization:
type: entity
table: base_organizations
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 128
type:
type: string
length: 64
oneToMany:
organizationHistory:
targetEntity: OrganizationHistory
mappedBy: organization
nullable: true
lifecycleCallbacks:
postPersist: [saveInsertHistory]
postUpdate: [saveUpdateHistory]
preRemove: [saveDeleteHistory]
and OrganizationHistory.orm.yml
BaseBundle\Entity\OrganizationHistory:
type: entity
table: base_organizationsHistory
uniqueConstraints:
organization_history_idx:
columns: [ organizationId, revision ]
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 128
type:
type: string
length: 64
revision:
type: integer
nullable: false
organizationId:
type: integer
nullable: false
createdAt:
type: datetime
nullable: false
updatedAt:
type: datetime
nullable: false
manyToOne:
organization:
targetEntity: Organization
inversedBy: organizationHistory
nullable: true
lifecycleCallbacks:
preUpdate: [ setUpdateTimestamp ]
prePersist: [ setCreationTimestamp, setUpdateTimestamp ]
you cannot delete data from one table and also want to store data related to organization in other table. Thats why relations are made for... But, you are using symfony, do "soft delete" which will just mark entity as deleted, but data still will be in your database.
maybe you can start here https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md

Tracking when an entity or its relations have been updated

I am using Symfony 2.4 and doctrine ORM. I have a parent entity, Property, which has many child relations including:
propertyVideos (OneToMany)
propertyPhotos (OneToMany)
propertyLocation (OneToOne)
In the lastUpdated field of the Property entity, I need to store the date and time of the last update of the Property entity or any of its related entities.
Is there an easy way to do this in Symfony/Doctrine?
Easiest bet would be to use the gedmo/doctrine-extensions or (for Symfony) stof/doctrine-extensions-bundle which you can then use the do the following...
Entity\Article:
type: entity
table: articles
fields:
created:
type: date
gedmo:
timestampable:
on: create // $this create
updated:
type: datetime
gedmo:
timestampable:
on: update // $this update
published:
type: datetime
gedmo:
timestmpable:
on: change
field: type.title
value: Published
// $this->type->title changed to "Published"
blah:
type: datetime
gedmo:
timestampable:
on: change
field: type.somethingelse
// $this->type.somethingelse is changed
manyToOne:
type:
targetEntity: Entity\Type
inversedBy: articles

Doctrine2 YML Mapping on Symfony2

I am starting with Symfony2, and so do I with Doctrine2.
I want to make an entity bidirectional, but I don't know what I am doing wrong. Here is my two .orm.yml files :
Categorie.orm.yml :
MY\SUPERBundle\Entity\Categorie:
type: entity
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
nomCategorie:
type: text
nullable: true
column: NomCategorie
oneToMany:
SousCategories:
targetEntity: MY\SUPERBundle\Entity\SousCategorie
mappedBy: Categorie
SousCategorie.orm.yml :
MY\SUPERBundle\Entity\SousCategorie:
type: entity
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
nomSousCategorie:
type: text
nullable: true
column: NomSousCategorie
manyToOne:
Categorie:
targetEntity: MY\SUPERBundle\Entity\Categorie
inversedBy: SousCategories
joinColumns:
categorie_id:
referencedColumnName: id
nullable: false
When I want to run the command :
doctrine:schema:update --dump-sql
I am getting this error :
[ReflectionException]
Property MY\SUPERBundle\Entity\Categorie::$SousCategories does not exist
If you guys have any hint on what I am doing wrong, I would be very grateful.
Thanks !
Are you sure you really have a property named "SousCategories" under your "Categorie" class?
Would be nice if you could add your entity class in your first post.
BTW, if it's a property, it shouldn't begin with an uppercase letter.

Resources