Symfony Doctrine ManyToOne association migration No reaction - php

I have two tables one is entry.php and user.php
I Want to associate $balance of user with the $balance of entry
but when I try php bin/console doctrine:migrations:diff
I get
No changes detected in your mapping information.
this is my user.php
<?php
namespace BankBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Table(name="user")
*
* #ORM\Entity(repositoryClass="BankBundle\Entity\user")
**/
class user
{
/**
* #ORM\Id
* #ORM\Column(name="id",type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="entry", mappedBy="balance")
* #var entry[] An ArrayCollection of entry objects.
*/
private $balance;
public function getId()
{
return $this->id;
}
public function getBalance()
{
return $this->balance;
}
}
and my entry.php
<?php
namespace BankBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Table(name="entry")
*
* #ORM\Entity(repositoryClass="BankBundle\Repository\BankRepository")
**/
class entry
{
/**
* #ORM\Id
* #ORM\Column(name="id",type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(name="amount",type="integer")
*/
private $amount;
/**
* #ORM\Column(name="balance",type="integer")
* #ORM\ManyToOne(targetEntity="user", inversedBy="balance")
*/
private $balance;
/**
* #ORM\Column(name="created_at",type="datetime")
*/
private $created_at;
}

Related

Doctrine entities reference each other's primary key

I have an Account entity which contains a collection of Group entities. In addition, each Account must have a default Group. How can one persist the entities? When attempting to do so, $manager->flush(); complains with a Not null violation for the entity that was persisted first.
Account:
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\IdGenerator\UuidV4Generator;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
*/
class Account
{
/**
* #ORM\Id
* #ORM\Column(type="uuid", unique=true)
* #ORM\GeneratedValue(strategy="CUSTOM")
* #ORM\CustomIdGenerator(class=UuidV4Generator::class)
*/
private $id;
/**
* #ORM\OneToOne(targetEntity=Group::class, cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=false, unique=true)
*/
private $defaultGroup;
/**
* #ORM\OneToMany(targetEntity=Group::class, mappedBy="account")
*/
private $groups;
public function __construct()
{
$this->groups = new ArrayCollection();
$this->defaultGroup = new Group();
}
// Typical getters and setters
}
Group:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
*/
class Group
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity=Account::class, inversedBy="groups")
* #ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $account;
// Typical getters and setters
}
Your approach is not possible because Group::$account is already mapped to the one-to-many Account::$groups property. Doctrine does not know what to do when you persist a Group via Account::$defaultGroup, as this is not the property Group::$account is inversing.
You will have to enforce this Account default group invariant yourself programmatically. There's multiple approaches to this, but I suggest using the existing Account::$groups mapping in combination with the Symfony Collection validation constraint. Validating that an Account should have at least 1 Group assigned to it, which will be the default one.
Here's an example:
Because the entity name Group was giving me syntax problems in the SQL dialect I was using I renamed it to Team.
The Account implementation:
<?php declare(strict_types = 1);
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
*/
class Account
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* #ORM\OneToMany(
* targetEntity="App\Entity\Team",
* mappedBy="account",
* cascade={"persist", "remove"},
* )
* #Assert\Count(
* min=1
* )
*/
private Collection $teams;
public function __construct()
{
$this->teams = new ArrayCollection();
$this->addTeam(new Team());
}
public function getId(): ?int
{
return $this->id;
}
public function getDefaultTeam(): ?Team
{
$team = $this->teams->first();
return $team instanceof Team ? $team : null;
}
public function getTeams(): Collection
{
return $this->teams;
}
public function setTeams(array $teams): void
{
$this->teams->clear();
foreach ($teams as $team) {
if ($team instanceof Team) {
$this->addTeam($team);
}
}
}
public function addTeam(Team $team): void
{
if (false === $this->teams->contains($team)) {
$this->teams->add($team);
$team->setAccount($this);
}
}
public function removeTeam(Team $team): void
{
if (true === $this->teams->contains($team)) {
$this->teams->removeElement($team);
}
}
}
The Team implementation:
<?php declare(strict_types = 1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Team
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* #ORM\ManyToOne(
* targetEntity="App\Entity\Account",
* inversedBy="teams",
* )
* #ORM\JoinColumn(
* name="account_id",
* referencedColumnName="id",
* nullable=false
* )
*/
private ?Account $account = null;
public function getId(): ?int
{
return $this->id;
}
public function getAccount(): ?Account
{
return $this->account;
}
public function setAccount(Account $account): void
{
$this->account = $account;
}
}

StofDoctrineExtensionsBundle checking the uniqueness of slug by the two fields

I use Knp\DoctrineBehaviors for Translation and StofDoctrineExtensionsBundle for Sluggable. How to make checking the uniqueness of slug by the sluggable and locale? I want to get the slug look like this:
for EN: /contacts
for PT: /pt/contacts
for PT (if duplicate): /pt/contacts-1
for ES: /es/contacts
But now, i have this database filter_node_translation
Entity\FilterNode.php:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* #ORM\Table(name="filter_node")
* #ORM\Entity()
*/
class FilterNode
{
use ORMBehaviors\Translatable\Translatable;
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(name="id", type="integer")
*/
protected $id;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
}
FilterNodeTranslation:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Table(name="filter_node_translation")
* #ORM\Entity()
*/
class FilterNodeTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #var string
*
* #ORM\Column(type="string", nullable=true)
*/
protected $sluggable;
/**
* #var string
*
* #Gedmo\Slug(fields={"sluggable"})
* #ORM\Column(type="string", nullable=true)
*/
protected $slug;
/**
* #return string
*/
public function getSluggable()
{
return $this->sluggable;
}
/**
* #param string $sluggable
*/
public function setSluggable($sluggable)
{
$this->sluggable = $sluggable;
}
}
I found solution. Gedmo sluggable have other configuration option "unique_base".
It looks like this:
class FilterNodeTranslation
{
/**
* #var string
*
* #Gedmo\Slug(updatable=true, unique=true, unique_base="locale", fields={"sluggable"})
* #ORM\Column(type="string")
*/
protected $slug;
}

Symfony2 Doctrine OneToMany Error

I have a big understanding problem with OneToMany Relationship - i searched a lot but that didnt help, maybe someone can help me with this.
I have a Event Entity with OneToMany Relation
<?php
namespace #\#\#;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Turmforum\BookingBundle\Entity\Contact;
/**
* Event
*
* #ORM\Table(name="events")
* #ORM\Entity
*/
class Event
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="Contact", mappedBy="event")
*/
protected $event_contacts;
/**
*
*/
public function __construct() {
$this->contacts = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function getContacts() {
return $this->contacts;
}
public function addContact(Contact $contact) {
$this->contacts[] = $contact;
return $this;
}
And have a Contact Entity with ManyToOne Relation
<?php
namespace #\#\#;
use Doctrine\ORM\Mapping as ORM;
use Turmforum\BookingBundle\Entity\Event;
/**
* Contact
*
* #ORM\Table(name="contacts")
* #ORM\Entity
*/
class Contact
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Event", inversedBy="event_contacts")
* #ORM\JoinColumn(name="event_id", referencedColumnName="id")
*/
protected $event;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* #return mixed
*/
public function getEvent() {
return $this->event;
}
When i try to save my event details over a form i get the error:
Neither the property "event_id" nor one of the methods "getEventId()",
"eventId()", "isEventId()", "hasEventId()", "__get()" exist and have
public access in class "Turmforum\BookingBundle\Entity\Event".
What am i missing?

Different behaviour with slightly similar entity

I made a web application with Symfony2. There are 2 kind of user, a UserOperator and a UserGroundStation. Both of them extend User.
USER GROUND STATION
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="user_GroundStation")
* #UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User",
message="Username already_used")
* #UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User",
message="Email already_used")
*/
class UserGroundStation extends User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #var float
*
* #ORM\Column(name="latitude", type="float")
*/
private $latitude;
/**
* #var float
*
* #ORM\Column(name="longitude", type="float")
*/
private $longitude;
/**
* Set latitude
*
* #param string $latitude
* #return UserGroundStation
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param float $longitude
* #return UserGroundStation
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return float
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* #ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->addRole('ROLE_GS');
}
}
USER OPERATOR
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="user_Operator")
* #UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User",
message="Username already_used")
* #UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User",
message="Email already_used")
*/
class UserOperator extends User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->addRole('ROLE_OPERATOR');
}
}
They extend User.
When I call the registration page for UserOperator, it works, when I call registration page for UserGroundStation, Symfony alerts me:
Neither the property "mission" nor one of the methods "getMission()", "isMission()", "hasMission()", "__get()" exist and have public access in class "Acme\ManagementBundle\Entity\UserGroundStation".
How is it possible? Where can I find the error?
USER
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* #ORM\Entity
* #ORM\Table(name="user")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"user_one" = "UserGroundStation", "user_two" = "UserOperator"})
* #ORM\MappedSuperclass()
*
*/
abstract class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", mappedBy="users")
*/
protected $missions;
public function __construct(){
$this -> missions = new ArrayCollection();
}
public function setMissions(Collection $missions){
$this -> missions = $missions;
return $this;
}
public function addMission($mission){
$this -> missions -> add($mission);
return $this;
}
public function getMissions(){
return $this -> missions;
}
It was really a dummy problem.
They have different FormType:
RegistrationUserOperatorFormType is different to RegistrationUserGroundStationFormType and
ProfileUserGroundStationFormType is different to ProfileUserOperatorFormType.

Class Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class

I have Three entities:
FeatureValue.php
<?php
namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class FeatureValue
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length="100")
*/
protected $name;
/**
* #ORM\OneToMany(targetEntity="FeatureType", mappedBy="name")
*/
private $featuretype;
public function __construct()
{
$this->featuretype = new \Doctrine\Common\Collections\ArrayCollection();
}
FeatureType.php
<?php
namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webmuch\ProductBundle\Entity\FeatureValue;
/**
* #ORM\Entity
*/
class FeatureType
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $title;
/**
* #ORM\ManyToOne(targetEntity="FeatureValue", inversedBy="featuretype",cascade={"persist"})
* #ORM\JoinColumn(name="feature_value_id", referencedColumnName="id")
*/
protected $name;
public function __construct()
{
$this->name = new \Doctrine\Common\Collections\ArrayCollection();
}
Product.php
<?php
namespace Webmuch\ProductBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table()
* #ORM\HasLifecycleCallbacks
*/
class Product
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Store")
*/
protected $store;
/**
* #ORM\ManyToMany(targetEntity="Webmuch\CategoryBundle\Entity\Category")
*/
protected $category;
/**
* #Gedmo\Sluggable
* #ORM\Column(type="string", length=255)
*/
protected $title;
/**
* #Gedmo\Slug(updatable=false, unique=true)
* #ORM\Column(type="string", length=255)
*/
protected $slug;
/**
* #ORM\Column(type="integer")
*/
protected $quantity;
/**
* #ORM\Column(type="boolean")
*/
protected $active;
/**
* #ORM\Column(type="text", length="4000", nullable="true")
*/
protected $preview;
/**
* #ORM\ManyToMany(targetEntity="FeatureType")
* #ORM\JoinColumn(name="feature_type_id", referencedColumnName="id")
*/
protected $features;
public function __toString()
{
return $this->getTitle();
}
}
My problem is that when i add FeatureValue in FeatureType then show me error Class "Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class."
In my project i want FeatureType along with FeatureValue in my Product entity.
suppose in FeatureType two property Size and Colour.Now in FeatureType Size- Three FeatueValue Small,Medium,Large have been given and also suppose in FeatureType Colour- Three FeatureValue Red,Green,Yello have been given.
Now i want to show both the FeatureType along with their FeatureValue in my Product Entity.
So any one tell me how it can be done.I have tried a lot of but not succeed.
In Webmuch\ProductBundle\Entity\FeatureType::__construct you assign an ArrayCollection to $this->name, this is wrong, as this is a ToOne and not a ToMany.
Just remove this line.

Categories