Namespace doesn't contain mapped entities in Symfony 2 - symfony

I've created a new entity in src/Andrei/StatisticsBundle/Entity/Attribute/Value/ButtonVarchar.php. Here is the code for this class:
<?php
namespace Andrei\StatisticsBundle\Entity\Attribute\Value;
class ButtonVarchar
{
protected $value;
}
and in src/Andrei/StatisticsBundle/Resources/config/doctrine/ButtonVarchar.yml I defined the following mapping information:
Andrei\StatisticsBundle\Entity\Attribute\Value\ButtonVarchar:
type: entity
table: button_attribute_value_varchar
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
value:
type: string
length: 255
manyToOne:
button:
targetEntity: Button
inversedBy: attributeValues
joinColumn:
name: button_id
referencedColumnName: id
For some reason when I run php app/console doctrine:generate:entities I get the following error:
[RuntimeException] Namespace "Andrei\StatisticsBundle\Entity\Attribute\Value" does not contain any mapped entities.
I can't understand why is this happening. Can someone point me to the right direction? Thank you.

Did you add your StatisticsBundle to Doctrine config?
eg:
doctrine:
orm:
auto_mapping: true
mappings:
AndreiStatisticsBundle: ~
You can see mapping problem in the following link:
https://github.com/symfony/symfony/pull/675

It seems that your entities are separated into fine grained packages. In that case you need to specify fully qualified namespace in order for it to work.
targetEntity: Fully\Qualified\Namespace\To\Button

It may also help.
This works:
MyUniqBundle:Entity
This doesn't work:
MyUniqBundle/Entity

Related

Simple Derived Identity issue

I'm struggling with an issue based on the use case that is described here :
Use-Case 2: Simple Derived Identity
I have the following Doctrine entities and mapping in my Symfony app:
class User
{
private $entity_id;
private $address;
...
}
class Address
{
private $user;
...
}
AppBundle\Entity\User:
type: entity
id:
entity_id:
type: integer
generator:
strategy: AUTO
oneToOne:
address:
targetEntity: Address
mappedBy: user
cascade: ["persist"]
AppBundle\Entity\Address:
type: entity
id:
user:
associationKey: true
oneToOne:
user:
targetEntity: User
inversedBy: address
joinColumn:
name: entity_id
referencedColumnName: entity_id
Every time I perform a DQL query that involves the User entity, Doctrine performs one additional query per matching User to retrieve the corresponding Address entity. That happen every time, even if the Address data are never used in the code.
I tried to reproduce this issue on a vanilla Symfony installation, and I faced another issue, I'm not able to perform the following code as I get an error (Entity of type AppBundle\Entity\Address is missing an assigned ID for field 'user'):
$user = (new User())->setAddress(new Address());
$entityManager->persist($user);
$entityManager->flush();
Do you have any hint or what is wrong?
Best regards

Doctrine manyToMany Definition not Working Undefined index: joinTable

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.

FOSElasticaBundle NumberFormatException error on populate

I use FOSElasticaBundle in my Symfony 2 project. Since today reindexing is resulting in the below error:
index: /app/hotel/1 caused MapperParsingException[failed to parse
[priceFrom]]; nested: NumberFormatException[For input string:
"410.00"];
In my doctrine orm yml the priceFrom field is defined as followed:
priceFrom:
type: decimal
nullable: true
precision: 7
scale: 2
comment: ''
column: price_from
My fos_elastica config looks like this (config.yml):
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
app:
types:
hotel:
mappings:
id: ~
active: ~
priceFrom: { type: integer }
persistence:
driver: orm
model: XXX\XXXBundle\Entity\Hotel
provider: ~
listener:
immediate: ~
finder: ~
The command I use to reindex: php app/console fos:elastica:populate
The above setup has worked until now. I hope someone can point my to the good direction to solve this problem.
Versions:
ruflin/elastica (2.1.0)
friendsofsymfony/elastica-bundle (v3.1.5)
symfony/symfony (v2.6.11)
PS: No other entities in my project are using a priceFrom field.
In mappings, you define PriceFrom as integer but then you pass a decimal.
I haven't tested it but it definitely seems the major candidate as the culprit.
Francesco Abeni is right with answer. If you are already pushed something in ES as integer (or ES defined it as integer) it will generate exception when you will try to save decimal data here.
I always explicitly specify type in mapping like:
id: {"type" : "integer"}
shop_id: {"type" : "integer"}
source: {"type" : "string", "index" : "not_analyzed"}
There I see two ways to solve problem.
index alias and index merge
specify type in mapping; kill index; populate in again
I used second variant on a dev :)

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.

Symfony 2 - Updating a table based on newly inserted record in another table

I'm trying to create a small forum application using Symfony 2 and Doctrine 2. My ForumTopic entity has a last_post field (oneToOne mapping). Now when I persist my new post with
$em->persist($post);
I want to update my ForumTopic entity so its last_post field would reference this new post. I have just realised that it cannot be done with a Doctrine postPersist Listener, so I decided to use a small hack, and tried:
$em->persist($post);
$em->flush();
$topic->setLastPost($post);
$em->persist($post);
$em->flush();
but it doesn't seem to update my topics table.
I also took a look at http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/working-with-associations.html#transitive-persistence-cascade-operations hoping it will solve the problem by adding cascade: [ 'persist' ] to my Topic.orm.yml file, but it didn't help, either.
Could anyone point me to a solution or an example class?
My ForumTopic is:
FrontBundle\Entity\ForumTopic:
type: entity
table: forum_topics
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string(100)
nullable: false
slug:
type: string(100)
nullable: false
created_at:
type: datetime
nullable: false
updated_at:
type: datetime
nullable: true
update_reason:
type: text
nullable: true
oneToMany:
posts:
targetEntity: ForumPost
mappedBy: topic
manyToOne:
created_by:
targetEntity: User
inversedBy: articles
nullable: false
updated_by:
targetEntity: User
nullable: true
default: null
topic_group:
targetEntity: ForumTopicGroup
inversedBy: topics
nullable: false
oneToOne:
last_post:
targetEntity: ForumPost
nullable: true
default: null
cascade: [ persist ]
uniqueConstraint:
uniqueSlugByGroup:
columns: [ topic_group, slug ]
And my ForumPost is:
FrontBundle\Entity\ForumPost:
type: entity
table: forum_posts
id:
id:
type: integer
generator:
strategy: AUTO
fields:
created_at:
type: datetime
nullable: false
updated_at:
type: datetime
nullable: true
update_reason:
type: string
nullable: true
text:
type: text
nullable: false
manyToOne:
created_by:
targetEntity: User
inversedBy: forum_posts
nullable: false
updated_by:
targetEntity: User
nullable: true
default: null
topic:
targetEntity: ForumTopic
inversedBy: posts
I believe that you're making this more difficult for yourself, because you think that you have to flush your post before you can set it to an association on your topic.
Doctrine is smart enough that if you persist an entity and set it to an association without first calling flush, it'll make sure that when you do call flush, it persists that entity first, so that it has an ID which can be used with the association.
What this means is that all you really need to do is this:
// Assume that topic has been fetched from the DB, and post is completely new
$topic = $em->find('TopicModel', $topicId);
$post = new ForumPost();
// Fill in the post info
// Set up the association
$topic->setLastPost($post);
// And finally, persist and flush - no more effort needed
$em->persist($post);
$em->flush();
A nice side effect of this is that you can simply use a prePersist event listener to update the thread's last post - Doctrine will take care of everything else for you.
Alternately, if you want an approach that can be a bit easier to follow logically, you can have your Post model call setLastPost on the topic yourself - for instance, if you set the post's topic in your constructor or in a setTopic() method, add the setLastPost call in there.
It's relatively common practice to have one side of an association take care of both sides like this, to help keep things nicely synchronised - see Working with Associations.

Resources