Symfony 2 entity join or doctrine query join - symfony

I was wondering if it was possible to configure a entity to automatically load data from another entity.
Ex.
/**
* accountsUsers
*
* #ORM\Table()
* #ORM\Entity
*/
class accountsUsers
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* #var string
*
* #ORM\Column(name="userid",type="integer")
*/
public $userid;
/**
* #var string
*
* #ORM\Column(name="roleid",type="integer")
*/
public $roleid;
/**
* #var string
* admin,adviser,etc
* #ORM\Column(name="roleType", type="string", length=255)
*/
public $roleType;
/**
* #var string
*
* #ORM\Column(name="firstname", type="string", length=255)
*/
public $firstname;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=255)
*/
public $lastname;
/**
* #var string
*
* #ORM\Column(name="company", type="string", length=255)
*/
public $company;
/**
* #var string
*
* #ORM\Column(name="url", type="string", length=255)
*/
public $url;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=255)
*/
public $phone;
/**
* #var string
*
* #ORM\Column(name="phone2", type="string", length=255)
*/
public $phone2;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
public $address;
/**
* #var string
*
* #ORM\Column(name="address2", type="string", length=255)
*/
public $address2;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=255)
*/
public $city;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=255)
*/
public $state;
/**
* #var string
*
* #ORM\Column(name="zip", type="string", length=255)
*/
public $zip;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
public $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
public $password;
}
AND
/**
* accounts
*
* #ORM\Table()
* #ORM\Entity
*/
class accounts
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* #var string
*
* #ORM\Column(name="partnerid", type="string", length=100)
*/
public $partnerid;
/**
* #var \DateTime
*
* #ORM\Column(name="subscribedate", type="date")
*/
public $subscribedate;
/**
* #var string
*
* #ORM\Column(name="connectionType", type="string", length=100)
*/
public $connectionType;
/**
* #var string
*
* #ORM\Column(name="recordkeeperTpaid", type="string", length=100)
*/
public $recordkeeperTpaid;
/**
* #var string
*
* #ORM\Column(name="recordkeeperAdviceProviderCd", type="string", length=100)
*/
public $recordkeeperAdviceProviderCd;
/**
* #var string
*
* #ORM\Column(name="recordkeeperUrl", type="string", length=200)
*/
public $recordkeeperUrl;
/**
* #var string
*
* #ORM\Column(name="recordkeeperUser", type="string", length=100)
*/
public $recordkeeperUser;
/**
* #var string
*
* #ORM\Column(name="recordkeeperPass", type="string", length=100)
*/
public $recordkeeperPass;
/**
* #var string
*
* #ORM\Column(name="recordkeeperPortfoliosAvailable", type="smallint")
*/
public $recordkeeperPortfoliosAvailable;
/**
* #var string
*
* #ORM\Column(name="recordkeeperRiskBasedFundsAvailable", type="smallint")
*/
public $recordkeeperRiskBasedFundsAvailable;
/**
* #var string
*
* #ORM\Column(name="investmentsMinScore", type="integer")
*/
public $investmentsMinScore;
/**
* #var string
*
* #ORM\Column(name="investmentsMaxScore", type="integer")
*/
public $investmentsMaxScore;
/**
* #var string
*
* #ORM\Column(name="ACAon", type="smallint")
*/
public $ACAon;
/**
* #var string
*
* #ORM\Column(name="MSTClientID", type="string", length = 100)
*/
public $MSTClientID;
}
Is it possible to set up accountsUsers to automatically load in all the accounts data when accountsUsers is accessed by findby, findbyone, etc. The relationship is accountsUsers.userid = accounts.id.
If not, how would I do this in a join using doctrine. I know how to do it using pure raw sql.
Ex. SELECT * FROM accounts a LEFT JOIN accountsUsers b ON a.id =
b.userid

It is not possible to load separate entities in one query (i.e. with a JOIN) just by using annotations. You'll have to define a custom query in your repository which instructs Doctrine, via DQL or QueryBuilder, to join with the related entities.
It was a big gotcha for me as well, but using fetch="EAGER" in the annotation defining the association doesn't actually use a JOIN to retrieve the result - it just issues 2 separate queries to pre-fill the relationship so that its first access does not trigger the lazy loading process.
See also this question for more information.

Symfony developed relations which give you the ability to define a property in entity files and relate it to other entity.
There are two ways.
First way:
In your case,
first you have to make a column in users table(for example name it account) and relate it to account.id column using foreign key's, then in App\Entity\Accounts you can define a property name $users and relate it to App\Entity\Users like code below:
/**
* #ORM\OneToMany(targetEntity="App\Entity\Users", mappedBy="account")
*/
private $users;
and one in App\Entity\Users
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Accounts", inversedBy="users")
*/
private $Account;
like this whenever you get a object from App\Entity\Accounts
a key with the name users will return that have the information of all of this account users just the result:
SELECT * FROM accounts a LEFT JOIN accountsUsers b ON a.id = b.userid
will give you.
The second way is pretty easy just by using make:entity command
php bin/console make:entity
you can make a new column and make a relation for it, command will ask you about everything step by step and its pretty much so simple working with it that you don't need to read the document but if you need more information this is the link to read about it
https://symfony.com/doc/current/doctrine/associations.html#the-manytoone-onetomany-association

Related

how to add a foreign key (one to many) in symfony

i have two entities 'Panier' and 'Reservation' i want to add a one to many foreign key (the Entity 'Panier' can have many 'Reservation' and 'Reservation' has only one 'Panier' id ) , so i have to add a foreign key 'id' of 'Panier' in my Reservation entity class .
this is my Reservation class :
class Reservation
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="dateReservation", type="datetime", nullable=false)
*/
private $datereservation = 'CURRENT_TIMESTAMP';
/**
* #var integer
*
* #ORM\Column(name="quantite", type="integer", nullable=false)
*/
private $quantite;
/**
* #var float
*
* #ORM\Column(name="total", type="float", precision=10, scale=0, nullable=true)
*/
private $total;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=255, nullable=false)
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="seat", type="string", length=255, nullable=false)
*/
private $seat;
/**
* #var integer
*
* #ORM\Column(name="payer", type="integer", nullable=true)
*/
private $payer;
/**
* #var string
*
* #ORM\Column(name="nomReservation", type="string", length=255, nullable=true)
*/
private $nomreservation;
/**
* #var \Event
*
* #ORM\ManyToOne(targetEntity="Event")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="event_id", referencedColumnName="id")
* })
*/
private $event;
/**
* #var \User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;
<?php
/** #Entity */
class Reservation {
/**
* #ManyToOne(targetEntity="Panier", inversedBy="reservations")
* #JoinColumn(name="panier_id", referencedColumnName="id")
*/
private $panier;
}
/** #Entity */
class Panier {
/**
* One Panier has many Reservations. This is the inverse side.
* #OneToMany(targetEntity="Reservation", mappedBy="panier")
*/
private $reservations;
public function __construct() {
$this->features = new ArrayCollection();
}
}

Symfony3 - The association refers to the owning side field which does not exist with ManyToMany and fields table

i'm trying to make a manyToMany relationship with more attributes than the ids, so I need two OneToMany relationships and two ManytoOne relationships having three tables/entities.
I have Product entity, Client entity and ProductClient entity:
class Client
{
/**
* #var integer
*
* #ORM\Column(name="id_client", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idClient;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* #var \ProductClient
*
* #ORM\OneToMany(targetEntity="ProductClient", mappedBy="client")
*/
private $products_clients;
}
class Product
{
/**
* #var integer
*
* #ORM\Column(name="id_product", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idProduct;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* #var \ProductClient
*
* #ORM\OneToMany(targetEntity="ProductClient", mappedBy="product")
*/
private $products_clients;
}
class ProductClient
{
/**
* #ORM\Column(name="product_id", type="integer")
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Product", inversedBy="products_clients")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id_product", nullable=false)
*/
protected $product;
/**
* #ORM\Column(name="client_id", type="integer")
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Client", inversedBy="products_clients")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id_client", nullable=false)
*/
protected $client;
/**
* #var bool
*
* #ORM\Column(name="status", type="boolean")
*/
private $status;
}
That's something like this (with its getters and setters and more attributes). But symfony launches two "invalid entities errors" when I go to the Product crud:
AppBundle\Entity\Product - The association AppBundle\Entity\Product#products_clients refers to the owning side field AppBundle\Entity\ProductClient#product which is not defined as association, but as field.
AppBundle\Entity\Product - The association AppBundle\Entity\Product#products_clients refers to the owning side field AppBundle\Entity\ProductClient#product which does not exist.
And the same result if I go to the Client crud. What's wrong?
As you can see in the error message, AppBundle\Entity\ProductClient#product is not defined as association, but as field.
Just remove this #ORM\Column(name="product_id", type="integer") and this #ORM\Column(name="client_id", type="integer").
class ProductClient
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Product", inversedBy="products_clients")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id_product", nullable=false)
*/
protected $product;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Client", inversedBy="products_clients")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id_client", nullable=false)
*/
protected $client;
/**
* #var bool
*
* #ORM\Column(name="status", type="boolean")
*/
private $status;
}

Use php-imap classes to parse new mails from server, with Symfony 2.7

I'm working on Symfony 2.7. I have to create a mail client to retrieve and send mails from the mail server (IMAP protocol); to do this, I used the php-imap classes, with a bundle (included with composer). But I'm not sure about the way I should use them : Do I extend the classes to represent my Mail and Mailbox objects, or should I create new classes from scratch ?
I don't want to manipulate IMAP straight from my controllers, I think it would be too long to process. Is that right?
Is it a good idea to create a "watcher" (periodic command executed by cron) to parse new mails every 2 minutes or so, create new mails entity from them, and send the waiting ones?
Could I do that while extending the php-imap classes? This way I would use one class only? But wouldn't that be too heavy to store for the database ?
What's the correct way to fetch only new mails ? Do I have to use a specific function, like imap_check, or do I do that by a search criteria (like date from the last check) ? I tried with criteria "NEW", but that was unsuccessful.
Also, the mailboxes I have to parse are quite heavy. I tried to make a search in one of them with "ALL" criteria, but it's really long to process ! Am I doing it right ? Do I just have to be patient ?
Here's what I did for the "watcher" function :
use PhpImap\Mailbox as ImapMailbox;
class GetNewMailsCommand extends ContainerAwareCommand
{
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$mailboxes = $em->getRepository('MIPMailBundle:MailBox')->findAllActive();
foreach ($mailboxes as $mailbox){
$imapBox = new ImapMailbox('{'.$mailbox->getServer().':143/notls/norsh/novalidate-cert}INBOX', $mailbox->getAdress(), $mailbox->getPassword());
if ($mailbox->getMails() == null || empty($mailbox->getMails())){
$mailsIds = $imapBox->searchMailbox('ALL');
if(!$mailsIds) {
$output->writeln($mailbox->getAdress() . " is empty");
}
} else {
$mailsIds = $imapBox->searchMailbox('NEW');
if(!$mailsIds) {
$output->writeln("No new mail for " . $mailbox->getAdress());
}
}
foreach ($mailsIds as $mailId){
$imapMail = $imapBox->getMail($mailId);
$mail = new Mail($mailbox, false);
$mail->setSubject($imapMail->subject);
$mail->setSender($imapMail->fromAddress);
$mail->setCc($imapMail->cc);
$mail->setBcc($imapMail->bcc);
$mail->setToString($imapMail->toString);
$mail->setContent($imapMail->textPlain);
$mail->setDate(new \DateTime($imapMail->date));
foreach ($imapMail->to as $toAddress){
$mail->addRecipient($toAddress);
}
$em->persist($mail);
}
}
$em->flush();
And here's my entities :
class MailBox
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var
*
* #ORM\OneToOne(targetEntity="\MIP\CRMBundle\Entity\Agency", inversedBy="mailBox", cascade={"persist"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
*/
private $user;
/**
* #var Agency
*
* #ORM\ManyToMany(targetEntity="\MIP\CRMBundle\Entity\Agency", inversedBy="mailBoxShared")
* #ORM\JoinTable(name="mailbox_shared")
*/
private $sharedTo;
/**
* #var
*
* #ORM\OneToMany(targetEntity="Mail", mappedBy="mailBox")
*/
private $mails;
/**
* #var boolean
*
* #ORM\Column(name="active", type="boolean")
*/
private $active;
/**
* #var string
*
* #ORM\Column(name="adress", type="string", length=255)
*/
private $adress;
/**
* #var string
*
* #ORM\Column(name="server", type="string", length=255)
*/
private $server;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* #var integer
*
* #ORM\Column(name="port", type="integer")
*/
private $port;
/**
* MailBox constructor.
*/
public function __construct()
{
$this->sharedTo = new ArrayCollection();
$this->mails = new ArrayCollection();
}
class Mail
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="sender", type="string", length=255)
*/
private $sender;
/**
* #var array
*
* #ORM\Column(name="recipients", type="json_array", nullable=true)
*/
private $recipients;
/**
* #var string
*
* #ORM\Column(name="toString", type="string", nullable=true)
*/
private $toString;
/**
* #var array
*
* #ORM\Column(name="cc", type="json_array", nullable=true)
*/
private $cc;
/**
* #var array
*
* #ORM\Column(name="bcc", type="json_array", nullable=true)
*/
private $bcc;
/**
* #var string
*
* #ORM\Column(name="subject", type="string", length=255, nullable=true)
*/
private $subject;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=true)
*/
private $content;
/**
* #var
*
* #ORM\OneToMany(targetEntity="MIP\CRMBundle\Entity\File", mappedBy="mail", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="file_id", referencedColumnName="id", nullable=true)
*/
protected $files;
/**
* #var ArrayCollection
*/
private $attached;
/**
* #var MailBox
* #ORM\ManyToOne(targetEntity="MailBox", inversedBy="mails")
* #ORM\JoinColumn(name="mailBox_id", referencedColumnName="id")
*/
private $mailBox;
/**
* #var LabelSticker
*
* #ORM\ManyToMany(targetEntity="\MIP\MailBundle\Entity\LabelSticker", mappedBy="mails")
*/
private $labels;
/**
* #var boolean
*/
private $readed;
/**
* #var boolean
*/
private $sent;
/**
* Constructor
* #param MailBox $mailbox
* #param boolean $readed
*/
public function __construct($mailbox, $readed)
{
$this->files = new ArrayCollection();
$this->date = new \DateTime('now');
$this->mailBox = $mailbox;
$this->readed = $readed;
}
Thanks for your help !

Symfony save date for every change status

how i can to do this: I have OrderWork entity this for order and have relation manyToMany with Status entity. All work good, but i want have date for every saved and updated order.
* Order
*
* #ORM\Table(name="order_work")
* #ORM\Entity(repositoryClass="AppBundle\Repository\OrderWorkRepository")
*/
class OrderWork
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Client", cascade={"persist"})
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
/**
* #var string
*
* #ORM\Column(name="orderNumber", type="string", length=255)
*/
private $orderNumber;
/**
* #var string
*
* #ORM\Column(name="orderCity", type="string", length=255)
*/
private $orderCity;
/**
* #var date
*
* #ORM\Column(name="date", type="string", length=255)
*/
private $date;
/**
* #var \DateTime
*
* #ORM\Column(name="orderDate", type="string", length=255, options={"default": NULL})
*/
private $orderDate;
/**
* #var \DateTime
*
* #ORM\Column(name="returnDate", type="string", length=255, nullable=true)
*/
private $returnDate;
/**
* #var string
*
* #ORM\Column(name="device", type="string", length=255)
*/
private $device;
/**
* #ORM\ManyToOne(targetEntity="SurrogatePhone", cascade={"persist"})
* #ORM\JoinColumn(name="surrogate_id", referencedColumnName="id")
*/
private $surrogatePhone;
/**
* #var int
*
* #ORM\Column(name="orderType", type="integer")
*/
public $orderType;
/**
* #ORM\ManyToMany(targetEntity="Status")
* #ORM\JoinTable(name="order_status",
* joinColumns={#ORM\JoinColumn(name="order_id", referencedColumnName="id", unique=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="status_id", referencedColumnName="id", unique=false)}
* )
*/
private $status;
How better resolve this solution?
Define date in your constructor (for creating):
class OrderWork
{
//...
public function __construct()
{
$this->date = new DateTime();
}
}
And update date field when updating:
$orderWork->setDate(new DateTime());
$em->flush();

How persist one to one entity unidirectionally in Symfony2

I have a problem with two entities in Symfony2 with Doctrine:
This is the first Entity:
/**
* Pedidos
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="RestCarta\Bundle\FrontendBundle\Entity\PedidosRepository")
*/
class Pedidos
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="usuario", type="string", length=100)
*/
private $usuario;
/**
* #var string
*
* #ORM\Column(name="mesa", type="string", length=3)
*/
private $mesa;
/**
* #var integer
*
* #ORM\OneToOne(targetEntity="Articulos")
* #ORM\JoinColumn(name="articulo_id", referencedColumnName="id")
*/
private $articulo;
/**
* #var string
*
* #ORM\Column(name="precio", type="decimal")
*/
private $precio;
This is the second Entity:
/**
* Articulos
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="RestCarta\Bundle\FrontendBundle\Entity\ArticulosRepository")
*/
class Articulos
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="referencia", type="string", length=100)
*/
private $referencia;
/**
* #var string
*
* #ORM\Column(name="nombre", type="string", length=255)
*/
private $nombre;
/**
* #var string
*
* #ORM\Column(name="descripcion", type="string", length=255)
*/
private $descripcion;
/**
* #var string
*
* #ORM\Column(name="precio", type="decimal")
*/
private $precio;
/**
* #var string
*
* #ORM\Column(name="imagen", type="string", length=255)
*/
private $imagen;
/**
* #ORM\ManyToOne(targetEntity="Categorias", inversedBy="articulos")
* #ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
*/
protected $categoria;
And now the problem.
How i can persist one "Pedido" with contain one "Articulo" ??
I can read all "Pedido" and the LEFT JOIN with "Articulo" work perfectly, (data inserted manually via phpMyAdmin) but when I persist more data with this code:
$em = $this->getDoctrine()->getManager();
$pedido = new Pedidos();
$pedido->setUsuario('blablabla');
$pedido->setMesa('blablabla');
$pedido->setArticulo($identi);
$pedido->setPrecio('blablabla');
$em->persist($pedido);
$em->flush();
$identi = corresponds to an id of "Articulos"
The result is:
Warning: spl_object_hash() expects parameter 1 to be object, string
given in
/Applications/MAMP/htdocs/RestCarta/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php
line 1388 500 Internal Server Error - ContextErrorException
Please, anyone can help me?
Thanks in Advance
$identi must be a entity object, not only the ID you can use
$em->getReference('YourNamespace\Articulos', $identi);

Resources