How can I add a property to an Entity Class, which should not be mapped to the database?
I need the property for a temporary value. Therefore the property should not be fetched from or persisted to the database. it neither should be a sql-calculated value, i need to set (and get) this within php code only.
Writing a property without annotation should not be linked to your Database as example of an entity User.
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="array")
*/
protected $username;
protected $notPersistedProperty
}
Hope this helped.
Related
I have an Entity class which already has constrains:
/**
* #ORM\Column(type="string", length=255)
*/
private $X;
/**
* #ORM\Column(type="integer")
*/
private $Y;
In my controller i'm getting post data from form and using setMethods on entity class:
$property = new PropertyEntity();
$property->setX($request->request->get('X'));
$property->setY($request->request->get('Y'));
next step - save to db.
Do I need to do additional validation on post data ? I though I need to use validation library but i'm not sure if it will only add unnecessary overhead since "#ORM" is already form type is already doing some validation.
Any general idea how and where to write validation ?(pseudocode is enough)
Good question!
ORM mapping map the PHP class to the doctrine metadata (Model).
Assert is a mechanism to validate objects received from form (View/Controller).
This means that you can use assert on objects that are not entities or that you cannot use a mapped field in your formType
You can make validation in the annotation of the field. example:
/**
* #Assert\NotBlank
* #ORM\Column(type="string", length=255)
*/
private $X;
/**
* #Assert\NotBlank
* #ORM\Column(type="integer")
*/
private $Y;
Don't forget to add:
use Symfony\Component\Validator\Constraints as Assert;
More validation constraints are in this link:
https://symfony.com/doc/current/validation.html#basic-constraints
I'm just making a new Entity as usual, but something goes wrong and console report this error and I couldn't generate the entity setter/getter:
[Doctrine\ORM\Mapping\MappingException]
Class "AppBundle\Entity\Admin_Actions" is not a valid entity or mapped super class.
Here is my Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="admin_actions")
*/
class Admin_Actions
{
/**
* #ORM\Column(name="id",type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="uid",type="string",length=100)
*/
private $uid;
/**
* #ORM\Column(name="type",type="integer")
*/
private $type;
/**
* #ORM\Column(name="linedate",type="datetime")
*/
private $linedate;
}
If I do doctrine:mapping:info:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have
entities or mapping files you should check your mapping configuration for errors.
I've just waste an hour trying to investigate the problem and I've already tried to rewrite it from new but I'm missing something. What's wrong with this?
May be datetime field has same name as function/implementation in doctrine, I have got same mistake by naming a table "condition" which may be condition function in MySql query
I am creating a simple post system with the Symfony 2 framework. I want the page to display a list of DiscussionPost entities that are fetched from the database. For each post, I want to render a small delete button that will remove the respective post and persist the deletion to the database if the User field matches the currently logged in user.
Here is the entity file for DiscussionPost.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="discussion_post")
*/
class DiscussionPost
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="User")
*/
protected $user;
/**
* #ORM\Column(type="string", length=140, nullable=false)
*/
protected $text;
/**
* #ORM\Column(type="datetime", nullable=false)
*/
protected $postTime;
// getters and setters...
I have already looked at the form collection article in the Symfony Cookbook:
How to Embed a Collection of Forms
However, this example is for multiple Tasks, each with a list of Tags that can be modified. My case is more similar to a list of Tasks that needs to be modified.
I also plan to have a form below the list of posts that is used to add a new post and persist it to the database.
Add a delete form for each record in your template with just the button and id in a hidden field and use jQuery to remove the item from the page and to AJAX so you don't have to reload the page every time.
I'm struggling with this, in my project in symfony. I have a entity content, with doctrine i have a table in my db content, that saves contents. Every content has an id. If anyone wants to see a specific content they have to request it by submitting an email, I need to associate all the emails to the specific id content. I dont want to create a new entity just to save the request emails. Is there a way to create/associate a new table with doctrine annotations. They have to be in a relation many-to-one. Or is there another simple way to save the email requests. This is the code I have, but is not working.
/**
* #ORM\Table(name="content")
* #ORM\Entity(repositoryClass="myBundle\Repository\ContentRepository")
*/
class Content{
/**
* #var integer
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $nome
* #ORM\ManyToOne(targetEntity="Content")
* #ORM\Column(name="email_request", type="string", length=255, unique=false, nullable=false )
*/
private $emailRequest;
Thanks for your time.
Your $emailRequest wouldn't be defined as Column if you want it to map entity. You need to define #ORM\JoinColumn instead.
As I am creating a web application that will be used in research on patients in the health domain, I want all my users to be completely anonymous. Can I in a simple way get rid of the email and email_canonical fields without rewriting stuff in the bundle itself, for instance by doing something to my User Class in my own bundle?
EDIT: I did this:
/**
* #ORM\Column(nullable=true)
**/
protected $email;
/**
* #ORM\Column(nullable=true)
**/
protected $emailCanonical;
In my User entity class. Bu twhen I do php app/console doctrine:schema:update --force i get
[Doctrine\ORM\Mapping\MappingException]
Duplicate definition of column 'email' on entity 'Pan100\MoodLogBundle\Enti
ty\User' in a field or discriminator column mapping.
EDIT 2: Forgot to say this is done in a class extending the FOUserBundle's model class User as BaseUser...
OK!
I did:
...
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #ORM\AttributeOverrides({
* #ORM\AttributeOverride(name="email", column=#ORM\Column(nullable=true)),
* #ORM\AttributeOverride(name="emailCanonical", column=#ORM\Column(nullable=true, unique=false))
*
* })
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...(code emitted)
Later I will find out if more is needed - I will probably have to override some of the FOSUserBundle methods for creating users. At least the "php app/console fos:user:create testuser" command requires an email... But it does not have to be unique anymore, so if I am hindered later I can just add the string "none" or something...