change default identity column name in inherited (joined ) class in symfony - symfony

I have used a Class Table Inheritance so that my entity "image" inherits from the "media" Entity.
this procedure create an auto generated column id in the image table. How can i change this default name "id" to set it "media_id" so that the image column has a column "media_id" instead of "id" ?
here is my code :
namespace App\Entity\OldApp;
use Doctrine\ORM\Mapping as ORM;
use \App\Entity\OldApp\TemplateContent;
/**
* #ORM\Entity(repositoryClass="App\Repository\OldApp\MediaRepository")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"media" = "Media", "image" = "Image"})
*/
class Media
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer",name="CAT_id")
*/
private $id;
/**
* #ORM\Column(name="filename", type="string", length=100, nullable=false)
*/
private $filename;
/**
* #ORM\Column(name="size", type="string", length=30, nullable=false)
*/
private $size;
/**
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* #ORM\Column(name="high", type="boolean", nullable=false)
*/
private $high = '0';
namespace App\Entity\OldApp;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\OldApp\ImageRepository")
*/
class Image extends Media
{
/**
* #ORM\Column(type="string", length=3)
*/
private $extension;
private static $type='image' ;
/**
* #ORM\Column(type="string", length=50)
*/
private $ratio;
/**
* #ORM\Column(type="smallint")
*/
private $height;
/**
* #ORM\Column(type="smallint")
*/
private $width;
}
i've tryed to add #PrimaryKeyJoinColumn(name="person_id") Bellow #ORM\Entity but i ged the following error
Error
[Semantical Error] The annotation "#Doctrine\ORM\Mapping\PrimaryKeyJoinColumn" in class App\Entity\OldApp\Image doe
s not exist, or could not be auto-loaded.

trait MediaTrait
{
/**
* #ORM\Column(name="filename", type="string", length=100, nullable=false)
*/
private $filename;
// getter //setter
}
/**
* #ORM\Entity(repositoryClass="App\Repository\OldApp\MediaRepository")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"media" = "Media", "image" = "Image"})
*/
class Media
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer",name="CAT_id")
*/
private $id;
use MediaTrait;
}
/**
* #ORM\Entity(repositoryClass="App\Repository\OldApp\ImageRepository")
*/
class Image
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer",name="media_id")
*/
private $id;
use MediaTrait;
// reste of the code
....
}

Related

Symfony / Doctrine materializedPath issues

I have followed the docs:
https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/tree.md#materialized-path
I am getting the following error:
An exception occurred while executing 'UPDATE task SET path = ?, task_id = ?, lvl = ? WHERE id = ?' with params ["-1", "-1", 0, 1]:
SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'task_id' at row 1
Here is my entity:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\MaterializedPathRepository")
* #Gedmo\Tree(type="materializedPath")
*/
class Task
{
/**
* #ORM\Id
* #ORM\Column(type="integer", options={"unsigned":true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=128, nullable=false)
*/
private $title;
/**
* #ORM\Column(type="integer", nullable=true)
* #Gedmo\TreeLevel
*/
private $lvl;
/**
* #ORM\Column(type="integer", nullable=true)
* #Gedmo\TreeLeft
*/
private $lft;
/**
* #ORM\Column(type="integer", nullable=true)
* #Gedmo\TreeRight
*/
private $rgt;
/**
* #ORM\Column(type="string", nullable=true)
* #Gedmo\TreePath(separator=".", startsWithSeparator=false, endsWithSeparator=false)
*/
private $path;
/**
* #ORM\Column(type="string", nullable=true)
* #Gedmo\TreePathSource
*/
private $source;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Task", mappedBy="parent")
* #ORM\OrderBy({"lft":"ASC"})
*/
private $children;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Task")
* #ORM\JoinColumn(name="task_id", referencedColumnName="id", onDelete="CASCADE")
* #Gedmo\TreeRoot
*/
private $root;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Task", inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
* #Gedmo\TreeParent
*/
private $parent;
}
What am I not understanding or what am I missing about the configuration for materialized path?
Why are the node ID's coming up as negative also??? Very odd...

Symfony extends entity

its posible to extend entity group by other entity gtoup also changin OneToOne/OneToMany relations.
For example i tried to exted UserTemp by User but on schema validation i receive error of mapping invalid.
class User
/**
* #ORM\Table(name="d1_user")
* #ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string|null
*
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* #var UserAddress
*
* #ORM\OneToMany(targetEntity="UserAdress", mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $address;
}
class UserTemp
/**
* #ORM\Table(name="d1_user")
* #ORM\Entity(repositoryClass="App\Repository\UserTempRepository")
*/
class UserTemp extends User
{
/**
* #var UserTempAddress
*
* #ORM\OneToMany(targetEntity="UserTempAdress", mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $address;
}
You try
/**
* #EntityExtension("User") //path Class User
*/
trait UserTempTrait
{
/**
* #var UserTempAddress
*
* #ORM\OneToMany(targetEntity="UserTempAdress", mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $address;
}

Symfony4 extending entity does not add Entity column to database schema

I am developing symfony 4.2 application. I have entity Meal and OrderItem. OrderItem should have all Meal entity properties + few of it's own. The problem is with ManyToOne relationship column. It is not added to order_item table.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html
I tried following "Class Table Inheritance" and "Mapped Superclasses". Example from "Mapped Superclasses" does not add $mappedRelated1 to EntitySubClass . And example from "Class Table Inheritance" removes every other extended field from Meal class and adds some kind of "dtype" column to Meal table.
/**
* #ORM\Entity(repositoryClass="App\Repository\OrderItemRepository")
*/
class OrderItem extends Meal
{
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Order", inversedBy="orderItems")
*/
private $order;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank
* #Assert\Type("string")
*/
private $amount;
}
/**
* #ORM\Entity(repositoryClass="App\Repository\MealRepository")
*/
class Meal
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank
* #Assert\Type("string")
*/
protected $name;
/**
* #ORM\Column(type="float")
* #Assert\NotBlank
* #Assert\Type("float")
*/
protected $price;
/**
* #ORM\Column(type="string", length=255)
* #Assert\Image(
* mimeTypes={"image/jpeg", "image/png"}
* )
*/
protected $image;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Menu", inversedBy="meals")
*/
protected $menu;
}
I expect to have "menu_id" column in "order_item" table, which would have relation to Menu entity.
I know that I could copy all the properties from Meal to OrderItem, but that does not sound right.
EDIT:
Both Meal and OrderItem should be able to have it's instance.
menu_id column is already exists in meal
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Menu", inversedBy="meals")
*/
protected $menu;
OrderItem class doesnt need to have just because it is already subclass of meal.
Also you dont need to copy all propertites from Meal to OrderItem.
u can check php extends here
I had the same issue. I used Trait instead of inheritance and the problem was solved.
Your class will be like this
trait MealInfoTrait
{
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank
* #Assert\Type("string")
*/
protected $name;
/**
* #ORM\Column(type="float")
* #Assert\NotBlank
* #Assert\Type("float")
*/
protected $price;
/**
* #ORM\Column(type="string", length=255)
* #Assert\Image(
* mimeTypes={"image/jpeg", "image/png"}
* )
*/
protected $image;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Menu", inversedBy="meals")
*/
protected $menu;
}
Then, change you Meal class
/**
* #ORM\Entity(repositoryClass="App\Repository\MealRepository")
*/
class Meal
{
use MealInfoTrait;
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
}
And do the same with your OrderItem class. Don't forget to add the id for the database.
/**
* #ORM\Entity(repositoryClass="App\Repository\OrderItemRepository")
*/
class OrderItem
{
use MealInfoTrait;
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Order", inversedBy="orderItems")
*/
private $order;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank
* #Assert\Type("string")
*/
private $amount;
}

Error creating relationship with FOSUserBundle and custom entity with easy-extends

I followed this documentation in sonata, step by step and it worked.
Then I added a new entity and tried to generate a relation many to many to user entity, and when I validate it return this error
$ bin/console doctrine:schema:validate
Mapping
-------
[FAIL] The entity-class AppBundle\Entity\Business mapping is invalid:
* The association AppBundle\Entity\Business#user refers to the owning side field Application\Sonata\UserBundle\Entity\User#business which does not exist.
Database
--------
[OK] The database schema is in sync with the mapping files.
This are my two entities
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Business
*
* #ORM\Table(name="business")
* #ORM\Entity(repositoryClass="AppBundle\Repository\BusinessRepository")
*/
class Business
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="BusinessName", type="string", length=255)
*/
private $businessName;
/**
* #var string
*
* #ORM\Column(name="fantasyName", type="string", length=255)
*/
private $fantasyName;
/**
* #var string
*
* #ORM\Column(name="cuit", type="string", length=13)
*/
private $cuit;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\BankAccountType", inversedBy="business")
*/
private $bankAccountType;
/**
* #var \DateTime $created
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #var \DateTime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updated;
/**
* #ORM\ManyToMany(targetEntity="\Application\Sonata\UserBundle\Entity\User", mappedBy="business")
*/
private $user;
/**
* #var bool
*
* #ORM\Column(name="isActive", type="boolean")
*/
private $isActive = true;
And this
namespace Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* #link https://sonata-project.org/easy-extends
*
* References:
* #link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
*/
class User extends BaseUser
{
/**
* #var int $id
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="\AppBundle\Entity\Business", inversedBy="user")
* #ORM\JoinTable(name="business_user")
*/
private $business;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->business = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
*
* #return int $id
*/
public function getId()
{
return $this->id;
}
}
Any idea?
Here is my code for the same thing and it works.
class Role
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="wizai\WMC\UserBundle\Entity\User", mappedBy="customRoles", fetch="EAGER")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
}
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="wizai\WMC\UserBundle\Entity\Role", inversedBy="users", fetch="EAGER")
* #ORM\JoinTable(name="user_role",
* joinColumns={#ORM\JoinColumn(name="user", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="role", referencedColumnName="id")}
* )
*/
protected $customRoles;
/**
* User constructor.
*/
public function __construct()
{
$this->customRoles = new ArrayCollection();
}
}
if its still not possible, can you first run a migration or a force update ?
Commands
bin/console doctrine:migrations:diff
bin/console doctrine:migrations:migrate
if not, then try.
bin/console doctrine:schema:update --force --complete

Entity inheritance in Doctrine doesn't include OneToOne-relationships

Trying to utilize inheritance, I've created the following entities:
/**
* #ORM\Table(name="persons")
* #ORM\Entity()
*/
class Person
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* #ORM\OneToOne(targetEntity="Image", cascade={"persist"})
* #ORM\JoinColumn(name="image_id", referencedColumnName="id")
*/
protected $image;
}
/**
* #ORM\Table(name="actors")
* #ORM\Entity()
*/
class Actor extends Person
{
/**
* #ORM\Column(name="character", type="string", length=255)
*/
private $character;
}
/**
* #ORM\Table(name="images")
* #ORM\Entity()
*/
class Image
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="path", type="string", length=255)
*/
private $path;
}
Which almost works perfectly. The generated actors-table contains all the persons-fields, except for the image-relation. I've tried to change the relation to a ManyToOne, which didn't help.
How to make the Actor-entity also inherit all joined fields? I'm open to other solutions, if the above isn't ideal.
You need a parent construct in your Actor class:
public function __construct()
{
parent::__construct();
// your own logic
}
It is advised that you add an ID aswell.

Resources