Main entity Conferimenti.php
/**
* Conferimenti
*
* #ORM\Table(name="conferimenti")
* #ORM\Entity(repositoryClass="App\Repository\ConferimentiRepository")
*/
class Conferimenti
{
/**
* #var int
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="conferimenti_id_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Infocomune")
* #ORM\JoinColumn(name="idcomune", referencedColumnName="id")
*/
private $idcomune;
/**
* #ORM\ManyToOne(targetEntity="Inforifiuti")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="tiporifiuto", referencedColumnName="codicerifiuto"),
* #ORM\JoinColumn(name="idcomune", referencedColumnName="idcomune")
* })
*/
private $tiporifiuto;
Inforifiuti.php
/**
* Inforifiuti
*
* #ORM\Table(name="inforifiuti")
* #ORM\Entity
*/
class Inforifiuti
{
/**
* #var int
*
* #ORM\Column(name="codicerifiuto", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $codicerifiuto;
/**
* #var int
*
* #ORM\Column(name="idcomune", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $idcomune;
/**
* #var string
*
* #ORM\Column(name="descrizione", type="string", length=80, nullable=false)
*/
private $descrizione;
Infocomune.php
/**
* Infocomune
*
* #ORM\Table(name="infocomune")
* #ORM\Entity
*/
class Infocomune
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="infocomune_id_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nome", type="string", length=80, nullable=false)
*/
private $nome;
I am tryng to execute this query (working on Postgres) but can't get it working with doctrine ORM :(
SELECT r.descrizione, SUM(c.peso) as totale
INNER JOIN inforifiuti AS r ON r.codicerifiuto = c.tiporifiuto AND r.idcomune = c.idcomune
WHERE c.idcomune = :id_comune AND c.dataora >= '2019-01-01' AND c.dataora <= '2019-10-30'
GROUP BY c.tiporifiuto, r.descrizione
Querybuilder
$query = $qb
->select(array('r.descrizione','SUM(c.peso) as totale'))
->innerJoin('c.tiporifiuto','r', Expr\Join::WITH, $qb->expr()->andX(
$qb->expr()->eq('c.tiporifiuto', 'r.codicerifiuto'),
$qb->expr()->eq('c.idcomune', 'r.idcomune')
))
->where('c.idcomune = :id_comune')
->andWhere($qb->expr()->between('c.dataora', ':date_from', ':date_to'))
->setParameter('id_comune', $id_comune)
->setParameter('date_from', $date_from, \Doctrine\DBAL\Types\Type::DATETIME)
->setParameter('date_to', $date_to, \Doctrine\DBAL\Types\Type::DATETIME)
->groupBy('c.tiporifiuto')
->addGroupBy('r.descrizione')
->getQuery();
I get this error
A single-valued association path expression to an entity with a composite primary key is not supported. Explicitly name the components of the composite primary key in the query.
tryed to replace ->GroupBy with but same error
->add('groupBy', new Expr\GroupBy(['c.tiporifiuto', 'r.descrizione']))
UPDATE
Tryed using direct DQL but same identical error
$query = $this->getEntityManager()->createQuery(
'SELECT r.descrizione, SUM(c.peso) as totale
FROM App\Entity\conferimenti AS c
INNER JOIN App\Entity\inforifiuti AS r
WHERE c.idcomune = :id_comune AND c.dataora >= :date_from AND c.dataora <= :date_to
GROUP BY c.tiporifiuto, r.descrizione')
->setParameter('id_comune', $id_comune)
->setParameter('date_from', $date_from, \Doctrine\DBAL\Types\Type::DATETIME)
->setParameter('date_to', $date_to, \Doctrine\DBAL\Types\Type::DATETIME);
A single-valued association path expression to an entity with a composite primary key is not supported. Explicitly name the components of the composite primary key in the query.
try this:
$this->manager->createQuery('SELECT .........');
like this example :
$query = $entityManager->createQuery(
'SELECT p
FROM AppBundle:Product p
WHERE p.price > :price
ORDER BY p.price ASC'
)->setParameter('price', 19.99);
the official documentation: https://symfony.com/doc/3.4/doctrine.html
Related
I have a Category entity, this entity allow me to create a tree of my data :
I would like to create a DiscountGrid entity. A DiscountGrid is composed by a textfield name, and an array containing the discount percentage by category ($discount[idCategory] = $percentage).
In fact, I would like a form like this :
The output would be $discount[ 1 ] = 25, $discount[ 2 ] = 30,.....
I have no idea how handle this behavior with Symfony framework. Here is the declaration of my entities
class Category{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="slug", type="string", length=255, nullable=true)
*/
private $slug;
/**
* #var int
* #Gedmo\TreeLeft
* #ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* #var int
* #Gedmo\TreeLevel
* #ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* #var int
* #Gedmo\TreeRight
* #ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* #Gedmo\TreeRoot
* #ORM\ManyToOne(targetEntity="Category")
* #ORM\JoinColumn(name="root", referencedColumnName="id", onDelete="CASCADE")
*/
private $root;
/**
* #Gedmo\TreeParent
* #ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* #ORM\JoinColumn(name="parent", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* #ORM\OneToMany(targetEntity="Category", mappedBy="parent")
* #ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
class DiscountGrid{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
/**
* #var array
*
* #ORM\Column(name="grid", type="array")
*/
private $grid;
First of all, what you are trying to do will be a nightmare if you keep storing DiscountGrid::$grid as an array. Not only this will make your form creation extremely hard, but what will happen if someday you have to add a Category? Will you drag all the indexes to match the new Categories list? This was not your question, and I may be downvoted for suggesting that, but I definitely recommend you build your model a bit cleaner, since having a clean model is mandatory to use FormTypes properly.
What I would suggest is the following model:
DiscountGrid::$grid (rename it to $discounts) is a ManyToOne of a new entity called Discount.
Discount has an attribute $category which is a OneToMany towards Category and an attribute $reduction which is a float.
Once you have this, create a DiscountFormType with a single field reduction which is a PercentType.
Then, create another form DiscountGridFormType with a single field discounts as a CollectionType. This CollectionType should have the option entry_type set as DiscountFormType.
Finally, when you create your form in Controller, bind it a DiscountGrid entity with some Discounts in your discounts attribute. You should see a series of text boxes with percents at the end. That's the list of discounts where you can change values of the reductions.
After that, some form theming will help you display the category name next to the textbox. But I guess you already have some way to go before being at this point.
i'm havin a little problem at the moment.
There's an parent Entity Comment with CommentType and CommentId and some other fields.
Also there are some differently CommentEntites (like CommentText or CommentPhoto) which extends the parent Entity.
The expected behaviour should be, that Doctrine loads Data with ContentId from Entity CommentText or CommentImage (depending on CommentType).
But at the moment it's loaded with the ID from the Parent Entity.
The Parent Entity:
use Doctrine\ORM\Mapping as ORM;
/**
* Comment
*
* #ORM\Table(name="comments")
* #ORM\Entity
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="comment_type", type="string")
* #ORM\DiscriminatorMap( {"txt" = "CommentTxt", "img" = "CommentImg"} )
*/
abstract class Comment
{
/**
* #var integer
*
* #ORM\Column(name="content_id", type="integer", nullable=true)
*/
private $contentId;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
//and so on
One of the Children Entities:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* CommentTxt
*
* #ORM\Table(name="comment_text")
* #ORM\Entity
*/
class CommentText extends Comment
{
/**
* #var string
*
* #ORM\Column(name="content", type="text", length=65535, nullable=false)
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
//and some more
How can i change this behaviour?
Greets
I'm converting a project to Doctrine 2.4 and the used database there is PostgreSQL.
I was wondering if it's possible to tell Doctrine to create the columns of the table(entity) in a particular order?
The table I'm currently converting has a lot of columns and some of them are relations to other tables. When the table is created, the first columns would be the primary keys, followed by foreign keys and at the end - everything else.
Here is an example code:
/**
* #Entity
* #Table(name="receipts")
*/
class Receipt {
/**
* #var
* #Id
* #GeneratedValue(strategy="IDENTITY")
* #Column(name="receipt_id", type="integer")
*/
private $receipt_id;
/**
* #var
* #Column(name="created_date", type="date", nullable=false)
*/
private $created_date;
/**
* #var
* #Column(name="updated_date", type="date", nullable=true)
*/
private $updated_date;
/**
* #var
* #Column(name="invoice_number", type="string", nullable=true)
*/
private $invoice_number;
/**
* #var
* #Column(name="invoice_date", type="date", nullable=true)
*/
private $invoice_date;
/**
* #var
* #Column(name="invoice_link", type="text", nullable=true)
*/
private $invoice_link;
/**
* #var
* #ManyToOne(targetEntity="models\Receipts\Invoice\Type")
* #JoinColumn(name="invoice_type_id", referencedColumnName="invoice_type_id")
*/
private $invoiceType;
/**
* #var
* #ManyToOne(targetEntity="models\Payments\Method")
* #JoinColumn(name="invoice_payment_method_id", referencedColumnName="payment_method_id")
*/
private $invoicePaymentMethod;
/**
* #var
* #ManyToOne(targetEntity="models\Payments\Type")
* #JoinColumn(name="invoice_payment_type_id", referencedColumnName="payment_type_id")
*/
private $invoicePaymentType;
public function __construct(){
$this->created_date = new \Date_Time();
$this->updated_date = new \Date_Time();
}
?>
The code above will look like this in PHPPgAdmin:
And this is how I would like the columns to appear:
Of course, everything is working fine. It's just the people working on the project with me (including I) are used to seeing the tables & columns ordered in a concrete way so that we could find things fast and easy. Hopefully there is a Doctrine ninja here that could help me with this. I couldn't find anything in the documentation.
I have 3 tables Chat(id + info) User(id + some info) and relation table chat_invited(chatId+userId)
I am using ZF2 and Doctrine2
I have Entity chat:
<?php
namespace Chat\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Chat
*
* #ORM\Table(name="chat")
* #ORM\Entity
*/
class Chat
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=256, nullable=true)
*/
private $title;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime", nullable=false)
*/
private $created;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Chat\Entity\User", inversedBy="chat")
* #ORM\JoinTable(name="chat_invited",
* joinColumns={
* #ORM\JoinColumn(name="chat", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="user", referencedColumnName="id")
* }
* )
*/
private $user;
/**
* #var \Chat\Entity\ChatCategory
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\ChatCategory")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category", referencedColumnName="id")
* })
*/
private $category;
/**
* #var \Chat\Entity\User
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="owner", referencedColumnName="id")
* })
*/
private $owner;
/**
* #var \Chat\Entity\Building
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\Building")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="building", referencedColumnName="id")
* })
*/
private $building;
/**
* #var \Chat\Entity\Company
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\Company")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="company", referencedColumnName="id")
* })
*/
private $company;
/**
* Constructor
*/
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add user
*
* #param \Chat\Entity\User $user
* #return Chat
*/
public function addUser(\Chat\Entity\User $user)
{
$this->user[] = $user;
return $this;
}
/**
* Remove user
*
* #param \Chat\Entity\User $user
*/
public function removeUser(\Chat\Entity\User $user)
{
$this->user->removeElement($user);
}
/**
* Get user
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
........................................
}
How find chat which have invited User1 and User2 to him?
Its worked for me
$qb = $em->createQueryBuilder();
$query = $qb->select('chats, user')
->from('Chat\Entity\Chat', 'chats')
->join('chats.user', 'user')
->join('chats.user', 'user2')
->where('user.id = :user AND user2.id = :inv ')
->setParameter('user', <User1.id>)
->setParameter('inv',<User2.id>)
->setMaxResults(1)
->getQuery();
Here are my entities :
/**
* Message_User
*
* #ORM\Table(name="message_user")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
*/
class Message_User
{
/**
* #ORM\ManyToOne(targetEntity="Learnpack\UserBundle\Entity\User", inversedBy="mu")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* #ORM\Id
*/
protected $user;
/**
* #ORM\ManyToOne(targetEntity="Learnpack\MessageBundle\Entity\Message", inversedBy="mu")
* #ORM\JoinColumn(name="message_id", referencedColumnName="id")
* #ORM\Id
*/
protected $message;
/**
* #var string $status
*
* #ORM\Column(name="status", type="string", length=45)
*/
private $status;
}
/**
* Message
*
* #ORM\Table(name="message")
* #ORM\Entity
*/
class Message
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="subject", type="string", length=100)
*/
private $subject;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="status", type="string", length=45)
*/
private $status;
/**
* #var string
*
* #ORM\Column(name="file", type="string", length=100)
*/
private $file;
/**
* #var \DateTime
*
* #ORM\Column(name="send", type="datetime")
*/
private $send;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* #var \DateTime
*
* #ORM\Column(name="modified", type="datetime")
*/
private $modified;
/**
* #ORM\ManyToOne(targetEntity="Learnpack\UserBundle\Entity\User", inversedBy="user")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $sender;
Now I would like to make a form with basic message sending. There will be three fields:
Recipient, subject and content.
My problem is that I do not know how to add a field "input" with ajax for the recipient.