We had to refactor many entities in our application because we share database and tables with another app and there was an update that changed the data structure and we must follow up to the new structure.
This meant that some entities were split in 2 or 3 tables so we followed the tutorial for setting up OneToOne relations but ended up with an issue on every attempt to persist both main and related entities.
Whenever we try to flush changes through the entity manager (after persisting both main and related entity either with or without cascade persist) we get 500 error status response, no response data (even using app_dev.php) and we get this message in the log:
request.CRITICAL:
Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException:
"Notice: Undefined index: 000000003440ddf300000000391d8640" at
vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 2905
{"exception":"[object]
(Symfony\\Component\\Debug\\Exception\\ContextErrorException(code: 0):
Notice: Undefined index: 000000003440ddf300000000391d8640 at
vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\UnitOfWork.php:2905)"} []
We are creating new entities so there is not an issue with cached entities.
Here is some sample code on how we defined the entities and the relations. (we tried both with and without id property but we ended up with the same issue)
/**
* #ORM\Table(name="contacts_accounts_1_c")
* #ORM\Entity
*/
class ContactsAccounts1C
{
/**
* #var string
* #ORM\Column(name="id", type="string", length=36, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var ContactsAccounts1CExtradata
* #ORM\OneToOne(targetEntity="ContactsAccounts1CExtradata", mappedBy="principal", cascade={"ALL"})
*/
private $extradata;
...
}
/**
* #ORM\Table(name="contacts_accounts_1_c_extradata")
* #ORM\Entity()
*/
class ContactsAccounts1CExtradata {
/**
* #var string
* #ORM\Column(name="id", type="string", length=36, nullable=false)
*/
private $id;
/**
* #var ContactsAccounts1C
* #ORM\Id
* #ORM\OneToOne(targetEntity="ContactsAccounts1C", inversedBy="extradata")
* #ORM\JoinColumn(name="id")
*/
private $principal;
...
}
If we avoid setting/creating the extradata entity the principal entity ends up being successfully persisted.
I found this question that seems to be related but still not solved.
Thanks in advance for your help.
At a quick glance, this annotation looks out of place on principal:
* #ORM\JoinColumn(name="id")
Given that your relation is principal -> extradata it seems to have no place there.
Found the issue: GeneratedValue was being wrongly defined and overrided for the main entity.
And the solution: Use #GeneratedValue(strategy="UUID") for principal entity (owning side) and #GeneratedValue(strategy="NONE") for related entities (owned side).
Related
I have issue with updating entities with console command bin/console doctrine:mapping:import. Project requirements forces different names for class names and properties in code and in database, for example:
/**
*
* #ORM\Table(name="user_db")
*/
class User
{
/**
* #var int
*
* #ORM\Column(name="id_user", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #ORM\SequenceGenerator(sequenceName="user_id_user_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="mail", type="string", length=128, nullable=false)
*/
private $email;
}
Database schema will be updated many times during project development. After running doctrine:mapping:import again instead of mapping user_db table to User entity (because annotation points to this table) this command creates new entity UserDB. After every database change I'm forced to manually compare changes and add them to code. Is there any way to update mapping for those entities (change column details, add new columns, remove removed)?
I'm using Symfony 5.4 as it is LTS version currently.
I am using Gedmo extension in addition with Symfony 3.2 and Doctrine 2.5.6 and I'm encountering an issue. I can't make Gedmo\Blameable and UniqueEntity constraint work together. Indeed, the blamed field is still null at validation time. Is there any way to make it work or a possible work-around ?
Here is my entity
/**
* #UniqueEntity(
* fields={"author", "question"},
* errorPath="question",
* message="This author already has an answer for that Question"
* )
* #ORM\Entity
*/
class TextAnswer
{
/**
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* #Gedmo\Blameable(on="create")
*/
private $author;
/**
* #Assert\NotNull()
* #ORM\ManyToOne(targetEntity="Question", inversedBy="textAnswers")
* #ORM\JoinColumn(name="question_id", referencedColumnName="id")
*/
private $question;
}
Thanks
EDIT : SOLUTION
Rather than manually setting the user (which removes Gedmo\Blameable interests), I created my own entity validator.
I give it doctrine and token storage as arguments so it can make a query on db to validate my criteria with the currently connected user (that will be later used by Gedmo\Blameable).
The BlameableListener is invoked during the Doctrine's flush operation, which normally happens after the entity has been validated. That's why $author is null at validation time.
The most straightforward workaround is to set $author yourself beforehand.
I'm working to fix some bugs and add new features to a project already in production.
What I need to do I think is very simple for who knows Symfony2 and Doctrine but I'm newbie and I don't know how to achive what i need:
I've got an existing entity on PHP side that is associated with a table in the database.
What I need is to create another entity that has some foreign key with other table.
I've tried to create the table into database first, but I don't know how to create the associated entity in PHP ( with correct annotation pointing to the foreign keys) and how to edit the other entities that need new attribute in class.
What I've also tried is to create an annotated PHP class as this:
<?php
namespace MyProject\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\VirtualProperty;
use MyProject\MyBundle\Model\ItemThumb;
/**
* #ORM\Entity
* #ORM\Table(name="wall_message_comment_answer")
*/
class WallMessageCommentAnswer {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #Groups({"user_details", "search_around"})
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="wall_message_comments")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
* #Groups({})
*/
public $user;
/**
* #var WallMessage
* #ORM\ManyToOne(targetEntity="WallMessage", inversedBy="users_comments")
* #ORM\JoinColumn(name="wall_message_id", referencedColumnName="id", onDelete="CASCADE")
*/
public $wall_message;
/**
* #var WallMessage
* #ORM\OneToMany(targetEntity="WallMessageComment", mappedBy="comment_answers")
* #ORM\JoinColumn(name="wall_message_comment_id", referencedColumnName="id", onDelete="CASCADE")
*/
public $wall_message_comment;
/**
* #var string
* #ORM\Column(type="string")
* #Groups({"user_details", "search_around"})
*/
public $content;
/**
* #var int
* #ORM\Column(type="integer")
* #Groups({"user_details", "search_around"})
*/
public $timestamp;
}
and then, trying to create getter and setter, launch the command:
php app/console doctrine:generate:entities MyProjectMyBundle/Entity/WallMessageCommentAnswer
But it gives me that error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Compile Error: Cannot redeclare MyProject\MyBundle\Entity\User::setDocumentNumber()
as it tries to create again other entities.
Could anyone help me?
Thanks!
Why don't you try creating Entity using php app/console doctrine:generate:entity command. This will ask you for Bundle name, Entity name and columns.
After this you'll have .php file created in specified bundle. Following this URL to manually add relationship between your current and new entity.
http://symfony.com/doc/current/book/doctrine.html#relationship-mapping-metadata
This is how you can give manyToOne relationship in Symfony usng annotations, you can switch your way to assigning this relationship. (YML or any other supported by Symfony)
/**
* #ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
And specify oneToMany in target entity like this
/**
* #ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
After you're done with this run the following command to get the SQL queries of the changes.
php pap/console doctrine:schema:update --dump-sql
You'll have SQL queries output which you need to copy and run on the production environment. If your production and testing environment are same run following command.
php pap/console doctrine:schema:update --force
For above procedure you don't have the table to be created in database. Doctrine does that for you.
If you already have table created you can remove that as it's going to be created automatically when you force the schema.
i have a problem with doctrine and i getting this error from auto generated entity file "Class "Users" is not a valid entity or mapped super class.". File and comments inside looks like fine i dont understund why or i something miss?
Some piece of code
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #ORM\Table(name="users", uniqueConstraints={#ORM\UniqueConstraint(name="username", columns={"username"})})
* #ORM\Entity
*/
class Users
{
/**
* #var integer
*
* #ORM\Column(name="userid", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userid;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=100, nullable=false)
*/
private $username;
Doctrine 2 annotation mapping might have been configured to negate the need for the #ORM prefix.
I would try replacing #ORM\ with #. For example #Entity
As far as i recall, these errors happen when doctrine cant find the entity, double check the namespace, by default the entity folder in symfony is "Entity" (Uppercase!). Also check the config files if auto_mapping is set to true.
for me this problem was solved after adding following namespace
use Doctrine\Common\Annotations\AnnotationReader;
in my doctrine.php
I'm having trouble using entity inheritance in Symfony2. Here are my two classes:
use Doctrine\ORM\Mapping as ORM;
/**
* #Orm\MappedSuperclass
*/
class Object
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
/**
* #Orm\MappedSuperclass
*/
class Book extends Object
{
}
When I run php app/console doctrine:schema:create I get the following error:
[Doctrine\ORM\Mapping\MappingException]
Duplicate definition of column 'id' on entity 'Name\SiteBundle\Entity\Book' in a field or discriminator column mapping.
What may be causing this?
Thanks :)
Update:
You are right I missed this. Now I'm using single table inheritance with both classes being entities:
/**
* #Entity
* #InheritanceType("SINGLE_TABLE")
* #DiscriminatorColumn(name="discr", type="string")
* #DiscriminatorMap({"object" = "Object", "book" = "Book"})
*/
But I still get the same error message.
Actually I found yml files in Resources/config/doctrine/, which were defining my entities, instead of just using annotations.
I removed these files and it's working now.
Thanks for your help !
I had same issue even after adding definitions to yml file. I was trying to add weight & max weight to a class and was getting:
Duplicate definition of column 'weight_value' on entity 'Model\ClientSuppliedProduct' in a field or discriminator column mapping.
Then I realized it requires columnPrefix to be different for similar types of fields and adding following in yml solved it for me:
`maxWeight:`
`class: Model\Weight`
`columnPrefix: max_weight_`
I had the same problem and error message but for me it was the other way around as #user2090861 said.
I had to remove the (unused)
use Doctrine\ORM\Mapping as ORM;
from my entity files, cause my real mapping comes from the orm.xml files.
I hope I can help with my answer many other people, cause this exception drove me crazy the last two days!
I ran into this in a different context - in my case, I had set up an entity for single-table inheritence using #ORM\DiscriminatorColumn, but had included the column in my class definition as well:
/**
* #ORM\Entity(repositoryClass="App\Repository\DirectoryObjectRepository")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="kind", type="string")
*/
class DirectoryObject {
// ...
/**
* #ORM\Column(type="string", length=255)
*/
private $kind;
}
Removing the #ORM\Column definition of kind fixed this issue, as Doctrine defines it for me.
Sometimes it's impossible to remove extra config files, because theay are located in third party bundle and auto_mapping is enabled.
In this case you should disable undesirable mappings in app/config.yml
doctrine:
orm:
entity_managers:
default:
mappings:
SonataMediaBundle: { mapping: false }
Any entity must contain at least one field.
You must add at least one field in Book Entity
Example
/**
* #Orm\MappedSuperclass
*/
class Book extends Object
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
I had the same error message but I had made a different mistake:
Class B had an ID and extended Class A which also had an ID (protected, not private). So I had to remove the ID from Class B.