Symfony 4 use Entity from different namespace in annotation - php

I am trying to use inside an entity another entity from another namespce but all I got is an error:
The class 'App\Entity\Gui\User' was not found in the chain configured namespaces App\Entity\Upv6
and the code:
namespace App\Entity\Upv6;
use App\Entity\Gui\User as User;
use Doctrine\ORM\Mapping as ORM;
/**
* ConfigSet
*
* #ORM\Table(name="config_set")
* #ORM\Entity(repositoryClass="App\Repository\ConfigSetRepository")
*/
class ConfigSet
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="App\Entity\Gui\User")
* #ORM\JoinColumn(name="id", referencedColumnName="id",
nullable=false)
*/
private $user;
Looks like it is still searching in same namespace.

Related

Doctrine said I should split parameter in explicit fields but then my ID is empty

I'm using a third party software which neither using Symfony nor Doctrine nor something else. Only PHP & MySQL.
And now I tried to generate entities from this old MySQL structure and using them into my project.
But I don't understand this double primary key situation.
I should split the parameter in explicit field and bind them separately... But in the doctrine documentation it seems possible, too. So what they want? http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity
/** #Id #OneToOne(targetEntity="User") */
This is the error message I get.
[Doctrine\ORM\ORMInvalidArgumentException]
Binding an entity with a composite primary key to a query is not supported.
You should split the parameter into the explicit fields and bind them separately.
And this is my first entity:
src/ShMaBundle/Entity/Passage.php
<?php
// src/ShMaBundle/Entity/Passage.php
namespace ShMaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Passage
*
* #ORM\Table(name="passage", #ORM\Index(name="IDX_98AF07F7EC91F2AA", columns={"DcplID"})})
* #ORM\Entity
*/
class Passage
{
/**
* #var boolean
*
* #ORM\Column(name="PositionsIdx", type="boolean", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $positionsidx;
/**
* #var Discipline
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Discipline")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="DcplID", referencedColumnName="DcplID")
* })
*/
private $dcplid;
}
And this is my second entity.
src/ShMaBundle/Entity/Discipline.php
<?php
// src/ShMaBundle/Entity/Discipline.php
namespace ShMaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Discipline
*
* #ORM\Table(name="discipline")
* #ORM\Entity
*/
class Discipline
{
/**
* #var integer
*
* #ORM\Column(name="DcplID", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $dcplid;
}
And year I don't know why doctrine can't load the entity and ignoring everything else. I can access the id by $passage->dcplid->dcplid. Or they want that I do it more better. Having something like this.
<?php
// src/ShMaBundle/Entity/Passage.php
namespace ShMaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Passage
*
* #ORM\Table(name="passage", #ORM\Index(name="IDX_98AF07F7EC91F2AA", columns={"DcplID"})})
* #ORM\Entity
*/
class Passage
{
/**
* #var boolean
*
* #ORM\Column(name="PositionsIdx", type="boolean", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $positionsidx;
/**
* #var integer
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $dcplid;
/**
* #var Discipline
*
* #ORM\OneToOne(targetEntity="Discipline")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="DcplID", referencedColumnName="DcplID")
* })
*/
private $dcpl;
}
Then I can access the discipline and the id separately.
But if I test this then I have an empty dcplid. But the $dcpl works and is filled. Hmm...
If it's possible, merge the oneToOne tables into one single table.
You should also use better attribute names like :
/**
* Passage
*
* #ORM\Table(name="passage", #ORM\Index(name="IDX_98AF07F7EC91F2AA", columns={"DcplID"})})
* #ORM\Entity
*/
class Passage
{
/**
* #var boolean
*
* #ORM\Column(name="PositionsIdx", type="boolean", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $positionsidx;
/**
* #var Discipline
*
* #ORM\OneToOne(targetEntity="Discipline")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="DcplID", referencedColumnName="DcplID")
* })
*/
private $discipline;
public function getDiscipline(){
return $this->discipline;
}
}
When do you have this error, on a query ?
Why don't you have any getter and setter methods ?
You should use a getter : $passage->getDiscipline()
Then, you can access the Id of the discipline
EDIT :
In the class Passage :
You have to remove the following :
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
that is over the attribute :
private $dcplid

set FOSUSERBUNDLE with existing table user (in symfony2)

I am supposed to built a project with symfony2 for a back end, and angular2 for front end.
For handling user action(authentification,remember me ...) I'am trying to used FOSUSERBUNDLE.
Let me specify that I'am using and existing database which contains a table with many attributes like (id, category, title, name, email, number, adress, ...). Some of attributes are not the same User in FOSUSERBUNDLE.
My question is how can I set FOSUSERBUNDLE to make it work with my existing table without brook it?
When I extend my class customer to BaserUser of FOSUserbundle it broke and changed my table structure
below
<?php
// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class customer extends BaseUser
{
/**
* #var float
*
* #ORM\Column(name="scoring", type="float", precision=10, scale=0, nullable=false)
*/
private $scoring = '0';
/**
* #var integer
*
* #ORM\Column(name="id_type", type="integer", nullable=false)
*/
private $idType = '4';
/**
* #var integer
*
* #ORM\Column(name="id_categorie", type="integer", nullable=false)
*/
private $idCategorie = '0';
/**
* #var string
*
* #ORM\Column(name="autorisation_prelevement", type="string", nullable=false)
*/
private $autorisationPrelevement = 'N';
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $addresse;
public function __construct()
{
parent::__construct();
}
}

Symfony 2.3 Join tables using composite keys

have a element class with composite keys.
When I run php app/console doctrine:schema:validate
I get the following error
The join columns of the association 'parentElement' have to match to
ALL identifier columns of the target entity
'AgRecord\AppBundle\Entity\Element', however 'id, parent_uuid' are
missing.
What am I missing or how do I correctly describe the relationship?
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Elements
*
* #ORM\Table(name="elements",uniqueConstraints={#ORM\UniqueConstraint(name="search_idx", columns={"uuid", "id", "parent_uuid"})})
* #ORM\Entity
*/
class Element
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", columnDefinition="INT AUTO_INCREMENT UNIQUE")
* #ORM\Id
*/
private $id;
/**
* #var guid
* #ORM\Id
* #ORM\Column(name="uuid", type="string", unique=true, nullable=false)
*/
private $uuid;
/**
* #var guid
* #ORM\Id
* #ORM\Column(name="parent_uuid", type="string")
*/
private $parentUUID;
/**
* #ORM\ManyToOne(targetEntity="Element", inversedBy="childElements")
* #ORM\JoinColumn(name="uuid", referencedColumnName="parent_uuid")
*/
private $parentElement;
/**
* #ORM\Id
* #ORM\OneToMany(targetEntity="Element", mappedBy="parentElement")
* #ORM\JoinColumn(name="uuid", referencedColumnName="element_uuid")
*/
private $childElements;
}
I was stupid I had all the mappings mixed up...
I solved my issue kind of.
First I decided to just remove id and have uuid, which meant i didnt need a composite key.
Then needed to remove stupidly placed #Id off of all non primary fields
Then removed the $parentUUID.
I was doing it the wrong way and didn't understand the mapping, and using an extra reference when it wasn't needed.
Then removed the joined annotation from the child elements and made sure to have the inversedby correctly set on the parent.
The name on the parent join annotation needs to be the name of the class member associated.
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Elements
*
* #ORM\Table(name="elements")
* #ORM\Entity
*/
class Element
{
private $id;
/**
* #var guid
* #ORM\Id
* #ORM\Column(name="uuid", type="string", unique=true, nullable=false)
*/
private $uuid;
/**
* #ORM\ManyToOne(targetEntity="Element", inversedBy="childElements")
* #ORM\JoinColumn(name="parentElement", referencedColumnName="uuid")
*/
private $parentElement;
/**
* #ORM\OneToMany(targetEntity="Element", mappedBy="parentElement")
*/
private $childElements;
}

Entity doctrine format

In doctrine I created entities name Products. It is showing like this.
use Doctrine\ORM\Mapping as ORM;
/**
* Products
*/
class Products
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
and so on...I don't find the above way normal as generally it is like below format.
use Doctrine\ORM\Mapping as ORM;
/**
* Products
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Acme\AcmeBundle\Repository\ProductsRepository")
*/
class Products
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=100)
...
My question is, what I have done wrong, that the I am not able to get the Entity files in the proper format.
thanks
My guess is that you didn't generate your Products entity using annotation but you used yml or xml.

Doctrine 2.1 - Removing from collection

I am trying to remove comments from a parent entity, I remember doing this on my last website but now it's not working..
My entity - users
namespace Application\Entities;
use Doctrine\ORM\Mapping AS ORM,
Doctrine\Common\Collections\ArrayCollection;
/**
* Loan
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class Users{
/**
* #var integer $id
*
* #ORM\Column(type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $username
*
* #ORM\Column(type="string", length=45, nullable=false)
*/
private $username;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="Comments", mappedBy="author", cascade={"persist", "remove"})
*/
private $comments;
public function getComments(){
return $this->comments;
}
and my comments table:
namespace Application\Entities;
use Doctrine\ORM\Mapping AS ORM,
Doctrine\Common\Collections\ArrayCollection;
/**
* Loan
*
* #ORM\Table(name="comments")
* #ORM\Entity
*/
class Comments{
/**
* #var integer $id
*
* #ORM\Column(type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer $user_id
*
* #ORM\Column(type="integer", length=15, nullable=false)
*/
private $user_id
/**
* #var Loan
*
* #ORM\ManyToOne(targetEntity="Users", inversedBy="comments",cascade={"persist"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $author;
This is fine, it works and I get all collections called comments in the users repository..
Now, I usually do this when I need to delete:
$commentToDelete = $this->em->getRepository('Entities\Comments')->findOneById(375);
$userResults = $this->em->getRepository('Entities\Users')->findOneById(23);
$userResults->getComments()->removeElement($commentToDelete);
$this->em->flush();
Nothing deletes, neither it throws an exception to tell me it hasn't.
I doctrine flushed it too, checked the db, and it's still there..
UPDATE:
Straight after I removeElement, I looped through the user id = 23 dataset, and the comment data for id375 is not there... so it removed it from the collection but not from the DB, and I thought $em->flush() is supposed to do this?
Please advise
Thanks
You need to use
$em->remove($commentToDelete);
$em->flush();
Because the mapping is held in the comment you need to remove this entity to remove the reference before you flush which will save the state to the db.

Categories