Is it possible to map Gedmo with yaml ? I'm using yaml file in my project and I would like to use Translatable from doctrine extensions but I have a problem with it.
I have something like that (one entity / one translation table) :
class CategoryTranslation extends \Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation
{
/**
* All required columns are mapped through inherited superclass
*/
}
but, when I run "doctrine:schema:update --dump-sql" command, I've got an error :
There is no column with name 'locale' on table 'category_translation'.
I have the feeling that yaml mapping "can't see" Gedmo AbstractTranslation class...
Do you have an idea of the problem ?
Thank you !
EDIT :
To avoid discussions in comments, I post an answer.
MyProject\MyBundle\Entity\Category:
type: entity
table: category
repositoryClass: MyProject\MyBundle\Repository\CategoryRepository
gedmo:
tree:
type: nested
translation:
locale: locale
entity: MyProject\MyBundle\Entity\Translation\CategoryTranslation
fields:
label:
type: string
length: 255
gedmo:
- translatable
With this config, I had to create a Translation.CategoryTranslation.orm.yml file because there is no database migration. And, there problem is really here :
MyProject\MyBundle\Entity\Translation\CategoryTranslation:
type: entity
table: category_translation
repositoryClass: Gedmo\Translatable\Entity\Repository\TranslationRepository
indexes:
category_translation_idx:
columns: [ locale, object_class, field, foreign_key ]
id:
id:
type: integer
generator:
strategy: AUTO
fields:
locale:
type: string
length: 8
object_class:
type: string
length: 255
field:
type: string
length: 32
foreign_key:
type: string
length: 64
content:
type: text
By doing this, the database migration works. I have a new table named category_translation. But, when I try to add a new translation, there is an error in my sql request generated by doctrine. Object_class field and foreign_key are null.
Here, the SQL request generated by doctrine :
INSERT INTO category_translation (locale, object_class, field, foreign_key, content) VALUES (?, ?, ?, ?, ?)' with params [\"fr\", null, \"name\", null, \"toto\"]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'object_class' cannot be null"}
Related
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.
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: { }
This just doesn't make sense. I can't seem to get a simple Symfony2 validation working.
$insert = new MyEntity();
$insert->setTest1( 'test' );
$validator = $this->get('validator');
$errors = $validator->validate($insert);
...but $errors is always an object with an empty constraints array. It never fails the validation.
My configuration (Yaml):
MyBundle\Entity\MyEntity:
properties:
test1:
- MinLength: 10
- Email
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
test1:
type: string
length: 255
column: test_1
test2:
type: integer
column: test_2
lifecycleCallbacks: { }
You're mixing doctrine's mapping and symfony's validation in a single yml file.
The validation configuration in yml is loaded from the files:
Acme/YourBundle/Resources/config/validation.yml // YAML
Acme/YourBundle/Resources/config/validation.xml // XML
And the mapping information should be placed in one of:
Acme/YourBundle/Resources/config/doctrine/MyEntity.orm.yml // YAML
Acme/YourBundle/Resources/config/doctrine/MyEntity.orm.xml // XML
Acme/YourBundle/Resources/config/doctrine/orm/MyEntity.orm.yml // YAML
Acme/YourBundle/Resources/config/doctrine/orm/MyEntity.orm.xml // XML
In annotation based mapping, as per the documentation of doctrine, we can do as shown below:
/** #Column(type="string", columnDefinition="ENUM('visible', 'invisible')") */
My question is how do I represent this in yaml meta data file for doctrine?
I want to do something like this:
fields:
status:
type: string
columnDefinition: ....
I am using symfony 2 as framework
Just use:
fields:
status:
type: string
columnDefinition: ENUM('visible', 'invisible')
status:
type: enum
values: ['visible', 'invisible']
https://www.doctrine-project.org/projects/doctrine1/en/latest/manual/yaml-schema-files.html#enums
In my project based on Symfony 2 + Doctrine 2 I implement model with following approach (based on FOSUserBundle source code):
All classes which belong to model are in "Model" folder of my bundle
In "Entity" folder I have classes which extend classes from Model
In "Resources/config/doctrine/" I have files with mappings in YAML format
Important: Classes for persistance (Entities) for which I want STI extend respective classes from Model, not from Entity.
The problem:
#Resources/config/doctrine/ContentElement.orm.yml
My\CoreBundle\Entity\ContentElement:
type: entity
table: content_element
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
length: 255
discriminatorMap:
contentElement: ContentElementList
htmlContentElement: HtmlContentElement
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
anchor_id:
type: string
anchor_text:
type: string
#Resources/config/doctrine/HtmlContentElement.orm.yml
My\CoreBundle\Entity\HtmlContentElement:
type: entity
fields:
html:
type: text
When I try to update database I've got errors from YAML driver until I specify additionally 'id' (which should be inherited as I thought)
After adding mapping for id I have sql queries in which I see 2 separate tables for each entity.
I suspect that this happens because HtmlContentElement extends Model\HtmlContentElement but not Entity\ContentElement.
Am I right and is there known solution to my problem?