Reputation: 13061
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!
Upvotes: 0
Views: 67
Reputation: 8032
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.
Upvotes: 1