Symfony 3 an exception occurred while executing SELECT NEXTVAL - php

I get this error when I try to insert something
An exception occurred while executing 'SELECT
NEXTVAL('oficinas_id_seq')':
SQLSTATE[42P01]: Undefined table: 7 ERROR: no existe la relación
«oficinas_id_seq» LINE 1: SELECT NEXTVAL('oficinas_id_seq')
This is my entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Oficinas
*
* #ORM\Table(name="oficinas")
* #ORM\Entity
*/
class Oficinas
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="public.oficinas_id_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="descripcion", type="string", length=100, nullable=false)
*/
private $descripcion;
/**
* #var \DateTime
*
* #ORM\Column(name="tu", type="datetime", nullable=false)
*/
private $tu;
/**
* #var \DateTime
*
* #ORM\Column(name="ts", type="datetime", nullable=false)
*/
private $ts = 'now()';
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set descripcion
*
* #param string $descripcion
*
* #return Oficinas
*/
public function setDescripcion($descripcion)
{
$this->descripcion = $descripcion;
return $this;
}
/**
* Get descripcion
*
* #return string
*/
public function getDescripcion()
{
return $this->descripcion;
}
}
I´ve test this doctrine commands but didn't work
php bin/console doctrine:mapping:import Bundle annotation php
bin/console generate:doctrine:entities Bundle php bin/console
doctrine:schema:update --force
Any idea?

I just run this command to see the diff between code and DB
php bin/console doctrine:schema:update --dump-sql

Related

Table join with entity (Symfony)

I'm new to Symfony and am having trouble getting my entities set up. I want to be able to access the tag names from my Acasset entity.
Here are the relevant entities:
class Actag
{
/**
* #var string
*
* #ORM\Column(name="tag_name", type="string", length=200, nullable=true)
*/
private $tagName;
/**
* #var integer
*
* #ORM\Column(name="tag_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $tagId;
/**
* Acassettag
*
* #ORM\Table(name="acAssetTag", indexes={#ORM\Index(name="IDX_7C4A2A745DA1941", columns={"asset_id"}), #ORM\Index(name="IDX_7C4A2A74BAD26311", columns={"tag_id"})})
* #ORM\Entity
*/
class Acassettag
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \AdminBundle\Entity\Acasset
*
* #ORM\ManyToOne(targetEntity="AdminBundle\Entity\Acasset", inversedBy="asset", cascade="PERSIST")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="asset_id", referencedColumnName="asset_id")
* })
*/
private $asset;
/**
* #var \AdminBundle\Entity\Actag
*
* #ORM\ManyToOne(targetEntity="AdminBundle\Entity\Actag")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="tag_id", referencedColumnName="tag_id")
* })
*/
private $tag;
/**
* Acasset
*
* #ORM\Table(name="acAsset", indexes={#ORM\Index(name="IDX_3B81679E68BA92E1", columns={"asset_type"}), #ORM\Index(name="IDX_3B81679E12469DE2", columns={"category_id"})})
* #ORM\Entity(repositoryClass="AdminBundle\Repository\AcAssetRepository")
*/
class Acasset
{
/**
* #var string
*
* #ORM\Column(name="asset_name", type="string", length=100, nullable=false)
*/
private $assetName;
/**
* #var integer
*
* #ORM\Column(name="asset_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $assetId;
/**
* #var \AdminBundle\Entity\Acassettype
*
* #ORM\OneToOne(targetEntity="AdminBundle\Entity\Acassettype", mappedBy="asset", fetch="EAGER", cascade={"persist"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="asset_type_id", referencedColumnName="asset_type_id")
* })
*/
private $assetType;
/**
* #var \AdminBundle\Entity\Actag
*
* #ORM\ManyToOne(targetEntity="AdminBundle\Entity\Actag")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="tag_id", referencedColumnName="tag_id")
* })
*/
private $assetTags;
/**
* Set tag
*
* #param \AdminBundle\Entity\Actag $tag
*
* #return Acassettag
*/
public function setAssetTags(\AdminBundle\Entity\Actag $tag = null)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* #return \AdminBundle\Entity\Actag
*/
public function getAssetTags()
{
return $this->tag;
}
So in my Acasset entity I have created $assetTags but I am getting an error: Invalid column name 'tag_id'.
But $tag in the Acasettag entity works, which is set up the same way. What am I missing, still struggling a little with this part of Symfony.
I didnt't include all the getters and setters in this post, just the one I created for this.
Why does your Actag-Entity hat also a tagId field? You don't need a defined Id-Column on the Owning-Side. You have to define a OneToMany.
Many Acassettag are owned by one Actag (because of the ManyToOne-Definition in Acassettag
So every Acassettag will need the field actag_id with the Id of the owning Actag which will be autogenerated by your ManyToOne-Definition
When you create a new Acassettag Entity and want to "connect" it with a existing Actag you need Acassettag
public setActag(Actag $actag)
{
$this->tag = $actag;
return $this;
}

Symfony2 doctrine2 insert record [Semantical Error] The annotation

When trying to insert a record to doctrine, records is inserting but i am getting an error :
[Semantical Error] The annotation "#Expose" in property
C2Educate\ToolsBundle\Entity\StudentScores::$id was never imported.
Did you maybe forget to add a "use" statement for this
annotation? (500 Internal Server Error)
Entity :
<?php
namespace C2Educate\ToolsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\SerializerBundle\Annotation\Type;
/**
* C2Educate\ToolsBundle\Entity\StudentScores
*
* #ORM\Table(name="tbl_student_scores")
* #ORM\Entity
*/
class StudentScores {
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="tbl_student_scores_id_seq", allocationSize="1", initialValue="1")
* #Expose
* #Type("integer")
*/
protected $id;
/**
* #var integer $studentId
*
* #ORM\Column(name="student_id", type="integer", nullable=true)
* #Assert\NotBlank()
* #Type("integer")
*/
private $studentId;
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set studentId
*
* #param string $studentId
*/
public function setStudentId($studentId) {
$this->studentId = $studentId;
}
/**
* Get studentId
*
* #return string
*/
public function getStudentId() {
return $this->studentId;
}
}
Controller Script :
$em = $this->getDoctrine()->getEntityManager();
$sc = new StudentScores();
$sc->setScore((!empty($test->composite) ? $test->composite : 0));
$sc->setTestDate($test->test_date);
$sc->setcreatedBy($loggedinUser);
$sc->setCreatedAt($date);
$sc->setupdatedBy($loggedinUser);
$sc->setUpdatedAt($date);
$sc->setStudentId($returnID);
$em->persist($sc);
$em->flush();
You forgot to add use statements as it says in error description. Try this
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;

Doctrine Symfony2 column already exists

I am working on project using Symfony2 with Doctrine and PostgreSQL as DB Engine and I have a problem. When I'm trying to do app/console doctrine:schema:update --force after changing entities I get this error:
This is my entity class code:
use Doctrine\ORM\Mapping as ORM;
/**
* LostConversation
*
* #ORM\Table(name="lost_conversation")
* #ORM\Entity(repositoryClass="AppBundle\Repository\LostConversationRepository")
*/
class LostConversation
{
const MAXSREENSHOTS = 50;
const SCREENSHOT_TIMEOUT = 10;
const MESSAGE_TIMEOUT = 30;
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var int
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Room", cascade={"all"})
* #ORM\JoinColumn(name="room_id", referencedColumnName="id", nullable=false)
*/
private $room;
/**
* #var string
* #ORM\Column(name="file_name ", type="string",length=40, nullable=true)
*/
private $fileName;
/**
* #var string
* #ORM\Column(name="message ", type="string",length=140, nullable=true)
*/
private $message;
/**
* #var int
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\User", cascade={"all"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
*/
private $user;
/**
* #var int
* #ORM\Column(name="insd", type="integer")
*/
private $insd;
Thanks for every answer! :)
You've got an extra space in the column name of the $fileName field:
/**
* #ORM\Column(name="file_name ", type="string",length=40, nullable=true)
^extra space here
Remove it and it should work.
The annotation of $message has the same problem.

Symfony and doctrine request

I'm trying to get all Tickets with "Destinataire" equal to a "Compte":
$ret = $repository->findByDestinataires($compte->getId());
My problem is that I get an error:
An exception occurred while executing 'SELECT t0.id AS id_1, t0.Titre AS Titre_2, t0.DateCreation AS DateCreation_3, t0.DateButoire AS DateButoire_4, t0.DateFin AS DateFin_5, t0.Priorite AS Priorite_6, t0.Commentaire AS Commentaire_7, t0.Statut AS Statut_8, t0.emeteur_id AS emeteur_id_9, t0.client_id AS client_id_10 FROM ticket t0 WHERE ticket_compte.compte_id = ?' with params [1]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ticket_compte.compte_id' in 'where clause'
I have two tables like this:
Compte:
namespace CommonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Compte
*
* #ORM\Table(name="compte")
* #ORM\Entity(repositoryClass="CommonBundle\Repository\CompteRepository")
*/
class Compte
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* #var string
*
* #ORM\Column(name="Nom", type="string", length=80)
*/
public $nom;
/**
* #var string
*
* #ORM\Column(name="Prenom", type="string", length=80)
*/
public $prenom;
/**
* #var string
*
* #ORM\Column(name="Fonction", type="string", length=80)
*/
public $fonction;
/**
* #var string
*
* #Assert\NotBlank()
* #ORM\Column(name="Pseudo", type="string", length=80)
*/
public $pseudo;
/**
* #var string
*
* #Assert\NotBlank()
* #ORM\Column(name="MotDePasse", type="string", length=80)
*/
public $motDePasse;
/**
* #ORM\ManyToOne(targetEntity="CommonBundle\Entity\Profil", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
public $profil;
/**
* #ORM\ManyToMany(targetEntity="CommonBundle\Entity\Ticket", cascade={"persist"})
*/
public $tickets;
}
Ticket:
namespace CommonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ticket
*
* #ORM\Table(name="ticket")
* #ORM\Entity(repositoryClass="CommonBundle\Repository\TicketRepository")
*/
class Ticket
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* #var string
*
* #ORM\Column(name="Titre", type="string", length=80)
*/
public $titre;
/**
* #var \DateTime
*
* #ORM\Column(name="DateCreation", type="datetimetz")
*/
public $dateCreation;
/**
* #var \DateTime
*
* #ORM\Column(name="DateButoire", type="datetimetz")
*/
public $dateButoire;
/**
* #var \DateTime
*
* #ORM\Column(name="DateFin", type="datetimetz", nullable=true)
*/
public $dateFin;
/**
* #var int
*
* #ORM\Column(name="Priorite", type="integer")
*/
public $priorite;
/**
* #var string
*
* #ORM\Column(name="Commentaire", type="text")
*/
public $commentaire;
/**
* #var int
*
* #ORM\Column(name="Statut", type="integer")
*/
public $statut;
/**
* #ORM\ManyToOne(targetEntity="CommonBundle\Entity\Compte", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
public $emeteur;
/**
* #ORM\ManyToMany(targetEntity="CommonBundle\Entity\Compte", cascade={"persist"})
* #ORM\JoinColumn(nullable=true)
*/
public $destinataires;
/**
* #ORM\ManyToOne(targetEntity="CommonBundle\Entity\Client")
* #ORM\JoinColumn(nullable=true)
*/
public $client;
}
And there links:
compte_ticket //for "emeteure"
->ticket_id compte_id
ticket_compte //for "destinataires"
->ticket_id compte_id
I've tried the same request directly on the server and got the same error.
Is it the PHP code that is wrong?
Or maybe the entities...
INFO:
I've not put all the get/set from Compte or Ticket on purpose. If it's needed I'll edit.
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html
It seem the that the shortcut method you are using (findByDestinataires) is only designed to get related records from the owning side of a relationship and hence only one 2 many not many 2 many.
You will need to create a custom query for this.
Here is one I knocked up quickly, add the following function to your TicketRepository
public function getTicketsByCompteId($compte_id)
{
return $this->getEntityManager()
->createQuery(
"SELECT t, d FROM AppBundle:Ticket t
LEFT JOIN t.destinataires d
WHERE
d.id = :compte_id"
)
->setParameter('compte_id', $compte_id)
->getResult();
}
And then call it from your controller:
$ret = $repository->getTicketsByCompteId($compte->getId());
Sounds like the foreign key field is not created. Did you run
php app/console doctrine:schema:update --force --dump-sql
After adding your relationships?

Doctrine 2: Update Entity Using Table Schema

I am using doctrine 2 within zend framework 2. To generate methods from existing entities using database table, the console command used is:
php doctrine-module orm:generate-entities --generate-annotations="true" --generate-methods="true" module
I have two namespaces Blog and Location
My question is:
1. When I run above code, only blog entities get updated. I want to know why it is behaving like this?
2. If I want to update only a specific entity, how can i do it?
The blog has two entities: Post and cateogry
Category.php
<?php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* Category
*
* #ORM\Table(name="Categories")
* #ORM\Entity
*/
class Category implements InputFilterAwareInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
protected $inputFilter;
/**
* Get Id
*
* #param integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
Post.php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table(name="posts")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", precision=0, scale=0, nullable=false, unique=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="content", type="text", precision=0, scale=0, nullable=false, unique=false)
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
private $createdDate;
/**
* #var \Blog\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Blog\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=true)
* })
*/
private $category;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
* #return Post
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set category
*
* #param \Blog\Entity\Category $category
* #return Post
*/
public function setCategory(\Blog\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Blog\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
The Location has Country.php
<?php
namespace Country\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Country
*
* #ORM\Table(name="countries")
* #ORM\Entity
*/
class Country
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=55, nullable=false)
*/
private $name;
}
OF Course everyone need a function on ZF2 that's turns to possible you generate a single entity. but those times you can't do it just by doctrine-orm-module. When you try to run doctrine in Symphony it's acceptable. I'm using doctrine2 with zf2 having the same issue.
I create a PHP Script that I call
Script to Create a Single Entity:
Choose Entity Name.
Generate All Entities on a Temp Folder.
Exclude All non-needed entities from folder.
Copy Single Entity to "src/$module/Entity" Folder.
Is a hack that i got to make it work property as you need.
Use the --filter option to do this.
php doctrine-module orm:generate-entities --filter="Post" ...

Categories