FOSUserBundle using ORM and FOSMessageBundle using MongoDB - mongodb

I need implement FOSUserBundle using ORM database and FOSMessageBundle using MongoDB (ODM database). Is it posible?
I configure FOSUserBundle using ORM and works.
I am triying to configure FOSMessageBundle using the documentation https://github.com/FriendsOfSymfony/FOSMessageBundle/blob/master/Resources/doc/01b-odm-models.md the problem it is here:
/**
* #MongoDB\ReferenceOne(targetDocument="Acme\UserBundle\Document\User")
*/
protected $sender;
I don´t have Acme\UserBundle\Document\User, I have Acme\UserBundle\Entity\User .
If I put Acme\UserBundle\Entity\User don´t work.
I try to use http://doctrine-mongodb-odm.readthedocs.org/en/latest/cookbook/blending-orm-and-mongodb-odm.html but I need help.
Another option it is create a duplicate User table in MongoDB but I don't know how I can do this.
Thanks for your solution Nawdal Serrar.
I read the documentation and try this. Don`t work, can you help me please?
Message.php
/**
* #MongoDB\Document
*/
class Message extends BaseMessage{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\EmbedMany(targetDocument="xxx\MensajeriaBundle\Document\MessageMetadata")
*/
protected $metadata;
/**
* #MongoDB\ReferenceOne(targetDocument="xxx\MensajeriaBundle\Document\Thread")
*/
protected $thread;
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="senderMongo")
*/
protected $sender;
public function __construct()
{
$this->metadata = new \Doctrine\Common\Collections\ArrayCollection();
$this->createdAt = new \DateTime();
}
}
MessageMetadata.php
/**
* #ODM\EmbeddedDocument
*/
class MessageMetadata extends BaseMessageMetadata
{
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="participantMesssageMongo")
*/
protected $participant;
}
Thread.class
/**
* #MongoDB\Document
*/
class Thread extends BaseThread
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\ReferenceMany(targetDocument="xxx\MensajeriaBundle\Document\Message")
*/
protected $messages;
/**
* #MongoDB\EmbedMany(targetDocument="xxx\MensajeriaBundle\Document\ThreadMetadata")
*/
protected $metadata;
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="participantsMongo")
*/
protected $participants;
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="createdByMongo")
*/
protected $createdBy;
}
ThreadMetadata.php
/**
* #ODM\EmbeddedDocument
*/
class ThreadMetadata extends BaseThreadMetadata
{
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="participantThreatMongo")
*/
protected $participant;
}
usuarios.php
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\Message", inversedBy="sender", identifier="senderId")
*/
private $senderMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $senderId;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\MessageMetadata", inversedBy="participant", identifier="participantMessageId")
*/
private $participantMessageMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $participantMessageId;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\Thread", inversedBy="participants", identifier="participantsId")
*/
private $participantsMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $participantsId;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\Thread", inversedBy="createdBy", identifier="createdById")
*/
private $createdByMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $createdById;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\ThreadMetadata", inversedBy="participant", identifier="participantThreadId")
*/
private $participantThreadMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $participantThreadId;

This is what you need, you can reference relationships between ODM and ORM entities https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/references.md

Related

APIPlatform (Symfony/REST) - "Error: Bad Request : Invalid IRI\"1\""

there is the situation :
I would like to send information to my REST API (working on Symfony), but when i tried to send this line, i got some errors that i don't understand, like : This one .
I really tried to resolve it alone, but nothing helped me on forums.
I think about an attribute than can't probably be set to editiion, or the reverse : an attribute which is not in edition and has to be.
This is what i want : be able to modify "dateCommande" (attribute for Purchase Date) , "statut" (used for see the statement of the purchase) and "idUsager" (used for put an id for a user); and finally be able to create automatically an id ("$id").
Thanks beforehand to help me ! ^^
Code of my class "Categorie.php" :
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Commande
*
* #ORM\Table(name="commande", indexes={#ORM\Index(name="IDX_6EEAA67DCF034CDB", columns={"id_usager_id"})})
* #ORM\Entity
* #ApiResource(normalizationContext={"groups"={"commande"},"enable_max_depth"=true},denormalizationContext={"groups"={"commandeecriture"},"enable_max_depth"=true})
*/
class Commande
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #Groups({"commande"})
* #NotNull(message="L'id ne peut être null")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="date_commande", type="date", nullable=false)
* #Groups({"commande","commandeecriture"})
* #NotBlank(message="Date non renseignée")
*/
private $dateCommande;
/**
* #var string
*
* #ORM\Column(name="statut", type="string", length=255, nullable=false)
* #Groups({"commande","commandeecriture"})
* #NotBlank(message="Statut non renseigné")
*/
private $statut;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_usager_id", referencedColumnName="id")
* })
* #Groups({"commande", "commandeecriture"})
* #NotNull(message="L'id ne peut être null")
*/
private $idUsager;
/**
* Constructor
*/
public function __construct()
{
$this->idArticle = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* #return \DateTime
*/
public function getDateCommande(): \DateTime
{
return $this->dateCommande;
}
/**
* #param \DateTime $dateCommande
*/
public function setDateCommande(\DateTime $dateCommande): void
{
$this->dateCommande = $dateCommande;
}
/**
* #return string
*/
public function getStatut(): string
{
return $this->statut;
}
/**
* #param string $statut
*/
public function setStatut(string $statut): void
{
$this->statut = $statut;
}
/**
* #return User
*/
public function getIdUsager(): User
{
return $this->idUsager;
}
/**
* #param User $idUsager
*/
public function setIdUsager(User $idUsager): void
{
$this->idUsager = $idUsager;
}
}
api-platform uses IRI instead of entity ids, try to replace idUsager="1" by idUsager="/usager/1". I'm guessing usager is the name of the related entity but if it's not then you just need to replace the actual name in the snippet in general it must be /related-entity-name/id

ManyToMany odm symfony2

I need to do this schema in symfony2 with odm, I have problems because the relasion is ManyToMany and I dont know How to do this. I need help.
I have the entity "Persona" and entity "Direccion" and these generate the entity "Persona-has-Direccion" with his ids and the attribute "viveAqui".
Persona{
_id: 1000,
nombre: 'Carlos',
apellidos: 'Gracia',
genero: 'M',
direcciones: [{
direccion_id: 1,
viveAqui: true
}.{
direccion_id: 2,
viveAqui: true
}]
}
My code:
/**
* #MongoDB\Document
*/
class Direccion
{
/**
* #MongoDB\Id(strategy="auto")
*/
private $id;
/**
* #MongoDB\String
*/
private $pais;
/**
* #MongoDB\String
*/
private $estado;
/**
* #MongoDB\String
*/
private $ciudad;
/**
* #MongoDB\Boolean
*/
private $viveAqui;
}
/**
* #MongoDB\Document
*/
class Persona
{
/**
* #MongoDB\Id(strategy="auto")
*/
private $id;
/**
* #MongoDB\String
*/
private $nombre;
/**
* #MongoDB\String
*/
private $apellidos;
/**
* #MongoDB\String
*/
private $genero;
}
Not sure what this mean:
these generate the entity "Persona-has-Direccion"
but I think basically simple reference is what you need:
class Persona
{
/**
*
* #MongoDB\ReferenceMany(targetDocument="Direccion", simple=true)
*/
protected $direcciones;
Then generate setter and getter (adder and remover in this case):
php app/console doctrine:mongodb:generate:documents YourBundle
You can save it later:
$persona = new Persona();
$direccion = new Direccion();
$persona->addDireccion($direccion);
$dm->persist($persona);
$dm->flush($persona);
Ok, I have a solution, but I dont know if it is correct.
My code:
/**
* #MongoDB\Document
*/
class Persona
{
/**
* #MongoDB\Id(strategy="auto")
*/
private $id;
/**
* #MongoDB\EmbedMany(targetDocument="PersonaDireccion")
*/
private $direccionPersona;
/**
* #MongoDB\String
*/
private $nombre;
/**
* #MongoDB\String
*/
private $apellidos;
/**
* #MongoDB\String
*/
private $genero;
}
class PersonaDireccion
{
/**
* #MongoDB\Id(strategy="auto")
*/
private $id;
/**
* #MongoDB\ReferenceOne(targetDocument="Direccion", cascade={"persist", "remove"})
*/
private $direccion;
/**
* #MongoDB\Boolean
*/
private $viveAqui;
}
/**
* #MongoDB\Document
*/
class Direccion
{
/**
* #MongoDB\Id(strategy="auto")
*/
private $id;
/**
* #MongoDB\String
*/
private $pais;
/**
* #MongoDB\String
*/
private $estado;
/**
* #MongoDB\String
*/
private $ciudad;
}
Now I have 3 documents. The document "PersonaDireccion" has referenceOne to "Direccion". The document "Persona" has embedMany "PersonaDireccion". The schema is the next:
Persona{
_id: 1000,
nombre: 'Carlos',
apellidos: 'Gracia',
genero: 'M',
direcciones: [{
direccionPersona_id: 1,
direccion_id: 1,
viveAqui: true
}.{
direccionPersona_id: 2,
direccion_id: 2,
viveAqui: true
}]
}

MongoDB ODM Indexing : How to Index multiple Compound index on a documnt that have EmbeddedDocument in itself?

I have this classes:
/**
* #ODM\Document
* #Indexes({
* #Index(keys={"status"="asc", "regDate"="desc", "expDate"="asc", "isFeatured"="asc"}),
* #Index(keys={"status"="asc", "visits.total"="asc", "visists.today"="asc"}),
* #Index(keys={"status"="asc", "price.value"="asc", "regDate"="asc"})
* })
*/
class Product {
/**
* #ODM\Date
*/
protected $regDate;
/**
* #ODM\Date
*/
protected $expire;
/**
* #ODM\EmbedOne(targetDocument="Price")
*/
protected $price;
/**
* #ODM\Boolean
*/
protected $isFeatured;
/**
* #ODM\EmbedMany(targetDocument="Visit")
*/
protected $visits;
}
/**
* #ODM\EmbeddedDocument
*/
class Price {
/**
* #ODM\Int
*/
protected $value;
/**
* #ODM\String
*/
protected $currency;
}
/**
* #ODM\EmbeddedDocument
*/
class Visit {
/**
* #ODM\Int
*/
protected $total;
/**
* #ODM\Int
*/
protected $today;
/**
* #ODM\EmbedMany(targetDocument="VisitPerDate")
*/
protected $perDate = array();
}
/**
* #ODM|EmbeddedDocument
*/
class VisitPerDate {
/**
* #ODM\Date
*/
protected $date;
/**
* #ODM\Int
*/
protected $visit;
}
I want to apply multiple compound indexes on Product Document.The indexes i wanna to add to database are the following:
{ "status"=1, "regDate"=-1, "expDate"=1, "isFeatured"=1 }
{ "status"=1, "visits.total"=1, "visits.today"=1, "regDate"=1 }
{ "status"=1, "price.value"=1, "regDate"=1 }
Was my Indexing annotation correct?
It seems that the first index must be correct,but i think the second and the third index is incorrect!
I think applying indexes is not possible yet by ODM. You may need to apply indexes by a command like this in mongo.exe command line:
use yourDbName
db.ensureIndexes()

Symfony2.1 persist ArrayCollection() collection to MongoDB via EmbedMany

I'm trying to persist some data to MongoDB with Docrtrine (symfony2.1). Here are my entities:
// src/Acme/ReportsBundle/Entity/ReportCore.php
namespace Acme\ReportsBundle\Entity;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #MongoDB\Document(collection="registeredReports")
*/
class ReportCore {
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
*/
protected $title;
/**
* #MongoDB\String
*/
protected $description;
/**
* #MongoDB\String
*/
protected $reference;
/**
* #MongoDB\Date
*/
protected $created;
/**
* #MongoDB\Date
*/
protected $createdBy;
/**
* #MongoDB\EmbedMany(targetDocument="ReportFields")
*/
protected $fields = array();
public function __construct () {
$this->fields = new ArrayCollection();
}
// Setters, getters
}
Here's embedded document:
// src/Acme/ReportsBundle/Entity/ReportFields.php
namespace Acme\ReportsBundle\Entity;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* #MongoDB\EmbeddedDocument
*/
class ReportFields {
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
*/
protected $title;
/**
* #MongoDB\String
*/
protected $description;
/**
* #MongoDB\String
*/
protected $name;
/**
* #MongoDB\String
*/
protected $type;
//Setters-getters...
}
and here is controller:
// src/Acme/ReportsBundle/Controller/ReportsController.php
namespace Acme\ReportsBundle\Controller;
use Acme\ReportsBundle\Entity\ReportCore;
use Acme\ReportsBundle\Entity\ReportFields;
use Acme\ReportsBundle\Form\Type\ReportCoreType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ReportsController extends Controller {
public function newAction (Request $request) {
$report = new ReportCore();
$fields1 = new ReportFields();
$report->getFields()->add($fields1);
$form = $this->createForm(new ReportCoreType(), $report);
if ( $request->isMethod('POST') ) {
$form->bind($request);
if ( $form->isValid() ) {
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($report);
$dm->flush();
}
}
return $this->render('AcmeReportsBundle:Report:new.html.twig', array(
'form' => $form->createView()
));
}
My problem is - when I try to persist data to database, I get
The class 'Acme\ReportsBundle\Entity\ReportCore' was not found in the chain configured namespaces FOS\UserBundle\Document
As I'm quite new to Symfony and Doctrine - I can't figure out what it has to do with FOSUserBundle, and what am I doing wrong.
Your MongoDb-Schemas should be within the Document namespace
namespace Acme\ReportsBundle\Document;

Zend - doctrine 2 querybuilder help

i want to use this:
$query = $this->_doctrine->createQueryBuilder()
->select('u')
->from('\Entities\Users', 'l')
->leftJoin('l.userentities', 'u')
->getQuery();
return $info = $query->getResult();
and my users entity is:
namespace Entities\Users;
/**
* #Entity
* #Table(name="users")
* #HasLifecycleCallbacks
*/
class Users extends \Entities\AbstractEntity
{
/**
* #Id #Column(name="userid", type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $userid;
/** #Column(name="itemid", type="integer") */
protected $itemid;
}
and my user entities entity class contains:
namespace Entities\Users;
/**
* #Entity
* #Table(name="userentities")
* #HasLifecycleCallbacks
*/
class Userentities extends \Entities\AbstractEntity
{
/**
* #Id #Column(name="entityid", type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $entityid;
/** #Column(name="userid", type="integer") */
protected $userid;
/** #Column(name="crb", type="string") */
protected $crb;
}
i tried put this into the entity but no joy
* #OneToMany(targetEntity="Users", inversedBy="userid")
* #JoinColumn(name="userid", referencedColumnName="userid")
1 user has many user entities..
and i get this error:
Error: Class Entities\Users has no association named Users
i just want to do a left join with users to usersentities..
how can i doa left join?
Try putting this annotation in your Userentities class for the $userid field:
/**
* #var Entities\Userentities
* #ManyToOne(targetEntity="Users", inversedBy="userentities", cascade={"persist"})
*/
private $userid;
and this annotation in your Users class with a new field called $userentities:
/**
* #var \Doctrine\Common\Collections\ArrayCollection
* #OneToMany(targetEntity="Userentities", mappedBy="userid", cascade={"persist", "remove"})
*/
private $userentities;