Symfony Doctrine2 Doesn't insert an available ID - php

I have a new problem related to Doctrine2 and Oracle...
I migrated an application from Symfony1 to symfony2. When I insert a new entry in the production database with Doctrine2, I get the error "ORA-00001: unique constraint violated". It tries to insert with the ID 1, if I try again it tries to insert with ID 2 etc...
Here is how I setup my entity :
<?php
namespace EspaceApprenti\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ApprenticeMark
*
* #ORM\Table(name="APPRENTICE_MARK", indexes={#ORM\Index(name="IDX_8582BCF7105754FC", columns={"FK_BRANCH"}), #ORM\Index(name="IDX_8582BCF7C9387C17", columns={"FK_YEAR"}), #ORM\Index(name="IDX_8582BCF73B451C64", columns={"FK_MARKTYPE"})})
* #ORM\Entity(repositoryClass="EspaceApprenti\UserBundle\Entity\ApprenticeMarkRepository")
*/
class ApprenticeMark
{
/**
* #var integer
*
* #ORM\Column(name="COEFFICIENT", type="bigint", nullable=false)
*/
private $coefficient;
/**
* #var integer
*
* #ORM\Column(name="ID", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="RESULT", type="decimal", precision=2, scale=1, nullable=false)
*/
private $result;
/**
* #var \ApprenticeBranch
*
* #ORM\ManyToOne(targetEntity="ApprenticeBranch")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="FK_BRANCH", referencedColumnName="ID")
* })
*/
private $fkBranch;
/**
* #var \ApprenticeYear
*
* #ORM\ManyToOne(targetEntity="ApprenticeYear")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="FK_YEAR", referencedColumnName="ID", onDelete="CASCADE")
* })
*/
private $fkYear;
/**
* #var \ApprenticeMarktype
*
* #ORM\ManyToOne(targetEntity="ApprenticeMarktype")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="FK_MARKTYPE", referencedColumnName="ID")
* })
*/
private $fkMarktype;
/**
* Set coefficient
*
* #param integer $coefficient
* #return ApprenticeMark
*/
public function setCoefficient($coefficient)
{
$this->coefficient = $coefficient;
return $this;
}
/**
* Get coefficient
*
* #return integer
*/
public function getCoefficient()
{
return $this->coefficient;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set result
*
* #param integer $result
* #return ApprenticeMark
*/
public function setResult($result)
{
$this->result = $result;
return $this;
}
/**
* Get result
*
* #return integer
*/
public function getResult()
{
return $this->result;
}
/**
* Set fkBranch
*
* #param \EspaceApprenti\UserBundle\Entity\ApprenticeBranch $fkBranch
* #return ApprenticeMark
*/
public function setFkBranch(\EspaceApprenti\UserBundle\Entity\ApprenticeBranch $fkBranch = null)
{
$this->fkBranch = $fkBranch;
return $this;
}
/**
* Get fkBranch
*
* #return \EspaceApprenti\UserBundle\Entity\ApprenticeBranch
*/
public function getFkBranch()
{
return $this->fkBranch;
}
/**
* Set fkYear
*
* #param \EspaceApprenti\UserBundle\Entity\ApprenticeYear $fkYear
* #return ApprenticeMark
*/
public function setFkYear(\EspaceApprenti\UserBundle\Entity\ApprenticeYear $fkYear = null)
{
$this->fkYear = $fkYear;
return $this;
}
/**
* Get fkYear
*
* #return \EspaceApprenti\UserBundle\Entity\ApprenticeYear
*/
public function getFkYear()
{
return $this->fkYear;
}
/**
* Set fkMarktype
*
* #param \EspaceApprenti\UserBundle\Entity\ApprenticeMarktype $fkMarktype
* #return ApprenticeMark
*/
public function setFkMarktype(\EspaceApprenti\UserBundle\Entity\ApprenticeMarktype $fkMarktype = null)
{
$this->fkMarktype = $fkMarktype;
return $this;
}
/**
* Get fkMarktype
*
* #return \EspaceApprenti\UserBundle\Entity\ApprenticeMarktype
*/
public function getFkMarktype()
{
return $this->fkMarktype;
}
}
Here is the code of the controller :
public function newAction($idYear,$idYeartype)
{
$m = $this->getDoctrine()
->getManager();
// Get year
$year = $m->getRepository('EspaceApprentiUserBundle:ApprenticeYear')->find($idYear);
if (!$year) {
throw $this->createNotFoundException('Year not found');
}
// Get yeartype
$yeartype = $m->getRepository('EspaceApprentiUserBundle:ApprenticeYeartype')->find($idYeartype);
if (!$yeartype) {
throw $this->createNotFoundException('Year type not found');
}
// Get apprentice
$apprentice = $m->getRepository('EspaceApprentiUserBundle:ApprenticeApprentice')->find($year->getFkApprentice()->getId());
if (!$apprentice) {
throw $this->createNotFoundException('Apprentice type not found');
}
$mark = new ApprenticeMark();
$mark->setFkYear($year);
$form = $this->createForm(new ApprenticeMarkType($yeartype), $mark);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$m->persist($mark);
$m->flush();
return $this->redirect($this->generateUrl('grids_apprentice_index',array('idApprentice' => $apprentice->getId())));
}
}
return $this->render('EspaceApprentiGridsBundle:Grids_Mark:new.html.twig', array('apprentice' => $apprentice, 'form' => $form->createView()));
}
How do I configure Doctrine2 to get the last ID and not just increment from 1 ?
Or should I get and insert the last ID manually ?
Regards

I found the solution to my problem. Apparently my SEQUENCES in Oracle were not up to date, so I had to manually update them.

Related

The option "filters" does not have a callable "setFilters" ("setfilters") setter method

Environment:
"require": {
"php": "5.6",
"zendframework/zendframework": "2.5.0",
"zendframework/zend-servicemanager": "2.5.1",
"zendframework/zend-developer-tools": "1.0.0",
"doctrine/doctrine-orm-module": "0.9.2",
"acelaya/zf2-acmailer": "4.*",
"tasmaniski/zend-params-helper": "^1.0"
}
Fatal error: Uncaught exception 'Zend\Stdlib\Exception\BadMethodCallException' with message 'The option "filters" does not have a callable "setFilters" ("setfilters") setter method which must be defined' in C:\OSPanel\domains\csmCrew_github\vendor\zendframework\zend-servicemanager\src\ServiceManager.php on line 1135
I am trying to add SQL filter using this DareDevels tutorial
my interface: VacanciesFilterInterface.php
namespace Common\DoctrineFilter;
Interface VacanciesFilterInterface
{
public function setFilters($filters);
}
my filter class: VacanciesFilter.php
namespace Common\DoctrineFilter;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;
class VacanciesFilter extends SQLFilter
{
/**
* Gets the SQL query part to add to a query.
*
* #param ClassMetaData $targetEntity
* #param string $targetTableAlias
*
* #return string The constraint SQL if there is available, empty string otherwise.
*/
public function addFilterConstraint (ClassMetadata $targetEntity, $targetTableAlias)
{
// Check if the entity implements the LocalAware interface
if (!$targetEntity->reflClass->implementsInterface('\Common\DoctrineFilter\VacanciesFilterInterface')) {
return '';
}
return $targetTableAlias.'.Vacancies = ' . $this->getParameter('Vacancies'); // getParameter applies quoting automatically
}
my module.config.php:
'doctrine' => array(
'driver' => array(
// defines an annotation driver with two paths, and names it `my_annotation_driver`
'common_entity' => array(
'class' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class,
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Common/Entity',
__DIR__ . '/../src/Common/Entity/Repository',
),
),
// default metadata driver, aggregates all other drivers into a single one.
// Override `orm_default` only if you know what you're doing
'orm_default' => array(
'drivers' => array(
// register `my_annotation_driver` for any entity under namespace `My\Namespace`
'Common\Entity' => 'common_entity',
),
'filters' => array(
'vacancies' => 'Common\DoctrineFilter\VacanciesFilter',
),
),
),
),
I add filter in my controller using these methods:
public function enableVacanciesFilter()
{
$em = $this->getEntityManager();
$filter = $em->getFilters()->enable('vacancies');
$filter->setParameter('vacancies', '41');
}
/**
* Disable locale filter
*/
public function disableVacanciesFilter()
{
$em = $this->getEntityManager();
$filter = $em->getFilters()->disable('vacancies');
}
So the question is: how can I implement that callable setFilters setter method ?
I have tried to implement Interface in my Entity:
<?php
namespace Common\Entity;
use Common\DoctrineFilter\VacanciesFilterInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* Vacancies
*
* #ORM\Table(name="vacancies", indexes={#ORM\Index(name="RankId_fk", columns={"rank_id"}), #ORM\Index(name="id", columns={"id"}), #ORM\Index(name="FK_vacancies_crewing_companies", columns={"company_id"}), #ORM\Index(name="FK_vacancies_vessels", columns={"vessel_id"}), #ORM\Index(name="FK_vacancies_vacancy_positions", columns={"position"}), #ORM\Index(name="status_id", columns={"status_id"})})
* #ORM\Entity
*/
class Vacancies implements VacanciesFilterInterface
{
/**
* #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=128, nullable=false)
*/
private $title = '';
/**
* #var string
*
* #ORM\Column(name="additional_info", type="string", length=255, nullable=false)
*/
private $additionalInfo = '';
/**
* #var integer
*
* #ORM\Column(name="duration_of_contract_in_months", type="smallint", nullable=false)
*/
private $durationOfContractInMonths;
/**
* #var \DateTime
*
* #ORM\Column(name="joining_date", type="date", nullable=false)
*/
private $joiningDate;
/**
* #var integer
*
* #ORM\Column(name="salary_per_month", type="integer", nullable=false)
*/
private $salaryPerMonth;
/**
* #var boolean
*
* #ORM\Column(name="required_age_from", type="boolean", nullable=false)
*/
private $requiredAgeFrom;
/**
* #var boolean
*
* #ORM\Column(name="required_age_to", type="boolean", nullable=false)
*/
private $requiredAgeTo;
/**
* #var \DateTime
*
* #ORM\Column(name="creation_date", type="datetime", nullable=false)
*/
private $creationDate;
/**
* #var integer
*
* #ORM\Column(name="views_count", type="integer", nullable=true)
*/
private $viewsCount = '0';
/**
* #var integer
*
* #ORM\Column(name="job_seekers_applied", type="integer", nullable=true)
*/
private $jobSeekersApplied = '0';
/**
* #var boolean
*
* #ORM\Column(name="is_published", type="boolean", nullable=false)
*/
private $isPublished = '0';
/**
* #var \Common\Entity\CrewingCompanies
*
* #ORM\ManyToOne(targetEntity="Common\Entity\CrewingCompanies")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
* })
*/
private $company;
/**
* #var \Common\Entity\VacancyPositions
*
* #ORM\ManyToOne(targetEntity="Common\Entity\VacancyPositions")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="position", referencedColumnName="id")
* })
*/
private $position;
/**
* #var \Common\Entity\Vessels
*
* #ORM\ManyToOne(targetEntity="Common\Entity\Vessels")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vessel_id", referencedColumnName="id")
* })
*/
private $vessel;
/**
* #var \Common\Entity\Ranks
*
* #ORM\ManyToOne(targetEntity="Common\Entity\Ranks")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="rank_id", referencedColumnName="id")
* })
*/
private $rank;
/**
* #var \Common\Entity\VacancyStatuses
*
* #ORM\ManyToOne(targetEntity="Common\Entity\VacancyStatuses")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="status_id", referencedColumnName="id")
* })
*/
private $status;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Vacancies
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set additionalInfo
*
* #param string $additionalInfo
*
* #return Vacancies
*/
public function setAdditionalInfo($additionalInfo)
{
$this->additionalInfo = $additionalInfo;
return $this;
}
/**
* Get additionalInfo
*
* #return string
*/
public function getAdditionalInfo()
{
return $this->additionalInfo;
}
/**
* Set durationOfContractInMonths
*
* #param integer $durationOfContractInMonths
*
* #return Vacancies
*/
public function setDurationOfContractInMonths($durationOfContractInMonths)
{
$this->durationOfContractInMonths = $durationOfContractInMonths;
return $this;
}
/**
* Get durationOfContractInMonths
*
* #return integer
*/
public function getDurationOfContractInMonths()
{
return $this->durationOfContractInMonths;
}
/**
* Set joiningDate
*
* #param \DateTime $joiningDate
*
* #return Vacancies
*/
public function setJoiningDate($joiningDate)
{
$this->joiningDate = $joiningDate;
return $this;
}
/**
* Get joiningDate
*
* #return \DateTime
*/
public function getJoiningDate()
{
return $this->joiningDate;
}
/**
* Set salaryPerMonth
*
* #param integer $salaryPerMonth
*
* #return Vacancies
*/
public function setSalaryPerMonth($salaryPerMonth)
{
$this->salaryPerMonth = $salaryPerMonth;
return $this;
}
/**
* Get salaryPerMonth
*
* #return integer
*/
public function getSalaryPerMonth()
{
return $this->salaryPerMonth;
}
/**
* Set requiredAgeFrom
*
* #param boolean $requiredAgeFrom
*
* #return Vacancies
*/
public function setRequiredAgeFrom($requiredAgeFrom)
{
$this->requiredAgeFrom = $requiredAgeFrom;
return $this;
}
/**
* Get requiredAgeFrom
*
* #return boolean
*/
public function getRequiredAgeFrom()
{
return $this->requiredAgeFrom;
}
/**
* Set requiredAgeTo
*
* #param boolean $requiredAgeTo
*
* #return Vacancies
*/
public function setRequiredAgeTo($requiredAgeTo)
{
$this->requiredAgeTo = $requiredAgeTo;
return $this;
}
/**
* Get requiredAgeTo
*
* #return boolean
*/
public function getRequiredAgeTo()
{
return $this->requiredAgeTo;
}
/**
* Set creationDate
*
* #param \DateTime $creationDate
*
* #return Vacancies
*/
public function setCreationDate($creationDate)
{
$this->creationDate = $creationDate;
return $this;
}
/**
* Get creationDate
*
* #return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Set viewsCount
*
* #param integer $viewsCount
*
* #return Vacancies
*/
public function setViewsCount($viewsCount)
{
$this->viewsCount = $viewsCount;
return $this;
}
/**
* Get viewsCount
*
* #return integer
*/
public function getViewsCount()
{
return $this->viewsCount;
}
/**
* Set jobSeekersApplied
*
* #param integer $jobSeekersApplied
*
* #return Vacancies
*/
public function setJobSeekersApplied($jobSeekersApplied)
{
$this->jobSeekersApplied = $jobSeekersApplied;
return $this;
}
/**
* Get jobSeekersApplied
*
* #return integer
*/
public function getJobSeekersApplied()
{
return $this->jobSeekersApplied;
}
/**
* Set isPublished
*
* #param boolean $isPublished
*
* #return Vacancies
*/
public function setIsPublished($isPublished)
{
$this->isPublished = $isPublished;
return $this;
}
/**
* Get isPublished
*
* #return boolean
*/
public function getIsPublished()
{
return $this->isPublished;
}
/**
* Set company
*
* #param \Common\Entity\CrewingCompanies $company
*
* #return Vacancies
*/
public function setCompany(\Common\Entity\CrewingCompanies $company = null)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* #return \Common\Entity\CrewingCompanies
*/
public function getCompany()
{
return $this->company;
}
/**
* Set position
*
* #param \Common\Entity\VacancyPositions $position
*
* #return Vacancies
*/
public function setPosition(\Common\Entity\VacancyPositions $position = null)
{
$this->position = $position;
return $this;
}
/**
* Get position
*
* #return \Common\Entity\VacancyPositions
*/
public function getPosition()
{
return $this->position;
}
/**
* Set vessel
*
* #param \Common\Entity\Vessels $vessel
*
* #return Vacancies
*/
public function setVessel(\Common\Entity\Vessels $vessel = null)
{
$this->vessel = $vessel;
return $this;
}
/**
* Get vessel
*
* #return \Common\Entity\Vessels
*/
public function getVessel()
{
return $this->vessel;
}
/**
* Set rank
*
* #param \Common\Entity\Ranks $rank
*
* #return Vacancies
*/
public function setRank(\Common\Entity\Ranks $rank = null)
{
$this->rank = $rank;
return $this;
}
/**
* Get rank
*
* #return \Common\Entity\Ranks
*/
public function getRank()
{
return $this->rank;
}
/**
* Set status
*
* #param \Common\Entity\VacancyStatuses $status
*
* #return Vacancies
*/
public function setStatus(\Common\Entity\VacancyStatuses $status = null)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return \Common\Entity\VacancyStatuses
*/
public function getStatus()
{
return $this->status;
}
public function setFilters ($filters)
{
$this->filters = $filters;
// TODO: Implement setFilters() method.
}
}
But nothing helps to deal with that error.

Doctrine Get the result of a DQL Query in a nested form

I have the following problem:
I have the following method that fetches the GPS points of a vessel:
/**
* #param array $mmsids
* #param unknown $longituteMin
* #param unknown $longtitudeMax
* #param unknown $latitudeMin
* #param unknown $latitudeMax
* #param \Datetime $timeInterval
*
* #throws EmptyParamGivenException
* #throws Exception
*
* #return Vesel[] with their routes
*/
public function getRoutes(array $mmsids=[],
$longituteMin=null,
$longtitudeMax=null,
$latitudeMin=null,
$latitudeMax=null,
\DateTime $fromDate=null,
\DateTime $toDate=null
) {
$em=$this->getEntityManager();
$query=$em->createQueryBuilder('v')
->from('AppBundle:Vesel', 'v')
->innerJoin('v.veselMoveStatuses','m')
->select('v.mmsi,m.logtitude,m.latitude,m.timestamp')
->addOrderBy('v.mmsi','ASC')
->addOrderBy('m.timestamp','DESC');
if(!empty($longituteMin)){
$query->andWhere('m.logtitude >= :long_min')->setParameter(':long_min',$longituteMin);
}
if(!empty($longtitudeMax)) {
$query->andWhere('m.logtitude <= :long_max')->setParameter(':long_max',$longituteMax);
}
if(!empty($latitudeMin)){
$query->andWhere('m.latitude >= :lat_min')->setParameter(':lat_min',$latitudeMin);
}
if(!empty($latitudeMax)){
$query->andWhere('m.latitude <= :lat_max')->setParameter(':lat_max',$latitudeMin);
}
if(!empty($mmsids)){
$query->andWhere('v.mmsi IN (:mmsids)')->setParameter('mmsids', $mmsids,\Doctrine\DBAL\Connection::PARAM_STR_ARRAY);
}
$paramsToValidate=[RouteInputParameter::PARAM_DATE_FROM=>$fromDate,RouteInputParameter::PARAM_DATE_TO=>$toDate];
if($fromDate!==null && $toDate!==null){
InputValidator::dateRangeValidation($paramsToValidate,RouteInputParameter::PARAM_DATE_FROM,RouteInputParameter::PARAM_DATE_TO);
$query->andWhere('m.timestamp BETWEEN :date_min AND :date_max')
->setParameters(['date_min'=>$fromDate,'date_max'=>$toDate]);
} else if($fromDate!==null) {
$query->andWhere('m.timestamp <= :date_min')
->setParameters(['date_min'=>$fromDate]);
} else if($toDate!==null) {
$query->where('m.timestamp >= :date_max')
->setParameters(['date_max'=>$toDate]);
}
$query = $query->getQuery();
return $query->getResult();
}
What I want to achieve it that I want to generate the result in a nested way like that:
[
[
"mmsi"=>"^somevalue^"
points=>[
"longtitude":"^longtitude_value^",
"latitude":"^latitude_value^",
"time":"^time_value^"
],
....
],
....
]
do you have any idea if there is an internal Doctrine mechanism that allows me to do this or should I implement my own?
The Vessel Entity is:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="vessel",indexes={
* #ORM\Index(name="mmsi",columns={"mmsi"})
* })
*/
class Vesel
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #var integer
*/
private $id;
/**
* #ORM\Column(type="integer",name="mmsi")
* #var integer
*/
private $mmsi;
/**
* #ORM\OneToMany(targetEntity="VesselMoveStatus",mappedBy="vesel")
* #var \Doctrine\Common\Collections\Collection
*/
private $veselMoveStatuses;
/**
* Constructor
*/
public function __construct($mmsi)
{
$this->veselMoveStatuses = new \Doctrine\Common\Collections\ArrayCollection();
$this->setMmsi($mmsi);
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set mmsi
*
* #param integer $mmsi
*
* #return Vesel
*/
public function setMmsi($mmsi)
{
$this->mmsi = $mmsi;
return $this;
}
/**
* Get mmsi
*
* #return integer
*/
public function getMmsi()
{
return $this->mmsi;
}
/**
* Add veselMoveStatus
*
* #param \AppBundle\Entity\VesselMoveStatus $veselMoveStatus
*
* #return Vesel
*/
public function addVeselMoveStatus(\AppBundle\Entity\VesselMoveStatus $veselMoveStatus)
{
$this->veselMoveStatuses[] = $veselMoveStatus;
return $this;
}
/**
* Remove veselMoveStatus
*
* #param \AppBundle\Entity\VesselMoveStatus $veselMoveStatus
*/
public function removeVeselMoveStatus(\AppBundle\Entity\VesselMoveStatus $veselMoveStatus)
{
$this->veselMoveStatuses->removeElement($veselMoveStatus);
}
/**
* Get veselMoveStatuses
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getVeselMoveStatuses()
{
return $this->veselMoveStatuses;
}
}
And is related into VesselMoveStatus entity:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Vesel;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\VeselRouteRepository")
* #ORM\Table(name="vessel_position_status",indexes={
* #ORM\Index(name="position",columns={"long","lat"})
* })
*/
class VesselMoveStatus
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id=null;
/**
* #ORM\ManyToOne(targetEntity="Vesel",inversedBy="veselMoveStatuses")
* #ORM\JoinColumn(name="vesel_id", referencedColumnName="id")
* #var Vesel
*/
private $vesel=null;
/**
* #ORM\Column(name="status",type="integer")
* #var integer|null
*/
private $status=null;
/**
* #ORM\Column(name="speed",type="integer")
* #var integer|null
*/
private $speed=null;
/**
* #ORM\Column(name="long",type="float")
* #var float|null
*/
private $logtitude=null;
/**
* #ORM\Column(name="lat",type="float")
* #var float|null
*/
private $latitude=null;
/**
* #ORM\Column(name="course",type="integer")
* #var integer|null
*/
private $course=null;
/**
* #ORM\Column(name="heading",type="integer")
* #var integer|null
*/
private $heading=null;
/**
* #ORM\Column(name="rotation",type="integer")
* #var integer|null
*/
private $rotation=null;
/**
* #ORM\Column(name="timestamp",type="datetime")
* #var Datetime|null
*/
private $timestamp=null;
public function __construct(
Vesel $vesel=null,
$status=null,
$speed=null,
$long=null,
$lat=null,
$course=null,
$heading=null,
$rotation=null,
$timestamp=null
){
$this->setVesel($vesel)
->setStatus($status)
->setSpeed($speed)
->setLogtitude($long)
->setLatitude($lat)
->setCourse($course)
->setHeading($heading)
->setRotation($rotation)
->setTimestamp($timestamp);
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set mmsi
*
* #param integer $mmsi
*
* #return VesselMoveStatus
*/
public function setMmsi($mmsi)
{
$this->mmsi = $mmsi;
return $this;
}
/**
* Get mmsi
*
* #return integer
*/
public function getMmsi()
{
return $this->mmsi;
}
/**
*
* #param integer $status
* #return \AppBundle\Entity\VesselMoveStatus
*/
public function setStatus($status)
{
$this->status=$status;
return $this;
}
/**
*
* #return \AppBundle\Entity\integer|NULL
*/
public function getStatus()
{
return $this->status;
}
/**
* Set speed
*
* #param integer $speed
*
* #return VesselMoveStatus
*/
public function setSpeed($speed)
{
$this->speed = $speed;
return $this;
}
/**
* Get speed
*
* #return float
*/
public function getSpeed()
{
return $this->speed/10;
}
/**
* Set logtitude
*
* #param integer $logtitude
*
* #return VesselMoveStatus
*/
public function setLogtitude($logtitude)
{
$this->logtitude = $this->sanitizeGpsCoordinate($logtitude);
return $this;
}
/**
* Get logtitude
*
* #return float
*/
public function getLogtitude()
{
return $this->logtitude;
}
/**
* Set latitude
*
* #param integer $latitude
*
* #return VesselMoveStatus
*/
public function setLatitude($latitude)
{
$this->latitude = $this->sanitizeGpsCoordinate($latitude);
return $this;
}
/**
* Get latitude
*
* #return float
*/
public function getLatitude()
{
$latitude=$this->latitude;
return $latitude;
}
/**
* Set course
*
* #param integer $course
*
* #return VesselMoveStatus
*/
public function setCourse($course)
{
$this->course = $course;
return $this;
}
/**
* Get course
*
* #return integer
*/
public function getCourse()
{
return $this->course;
}
/**
* Set heading
*
* #param integer $heading
*
* #return VesselMoveStatus
*/
public function setHeading($heading)
{
$this->heading = $heading;
return $this;
}
/**
* Get heading
*
* #return integer
*/
public function getHeading()
{
return $this->heading;
}
/**
* Set rotation
*
* #param integer $rotation
*
* #return VesselMoveStatus
*/
public function setRotation($rotation)
{
$this->rotation = $rotation;
return $this;
}
/**
* Get rotation
*
* #return integer
*/
public function getRotation()
{
return $this->rotation;
}
/**
* Set timesptamp
*
* #param string $timesptamp
*
* #return VesselMoveStatus
*/
public function setTimestamp($timesptamp)
{
$this->timestamp = date_create_from_format("Y-m-d H:i:s.u",$timesptamp);
return $this;
}
/**
* Get timesptamp
*
* #return \DateTime
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* Set vesel
*
* #param \AppBundle\Entity\Vesel $vesel
*
* #return VesselMoveStatus
*/
public function setVesel(\AppBundle\Entity\Vesel $vesel = null)
{
$this->vesel = $vesel;
return $this;
}
/**
* Get vesel
*
* #return \AppBundle\Entity\Vesel
*/
public function getVesel()
{
return $this->vesel;
}
/**
* Sometimes a GPS Coordinate may have the following format:
* 1,234532 if inserted as is then itn WONT be retreived correctly.
* Please use this method to sanitize the gps coordinate on setter method.
*
* #param string | float $coordinate
* #return number
*/
private function sanitizeGpsCoordinate($coordinate)
{
if(is_string($coordinate))
{
$coordinate=str_replace(',','.',$coordinate);
}
return (float)$coordinate;
}
}
The way you want to transform the data is not standard thus there is nothing generic in Doctrine to do so. But it's still very simple to achieve the objective you want.
However, IMHO the simplest solution is to use a closure to transform the result:
$result = $query->getResult();
$formatter = function($row) {
return [
"mmsi" => $row['mmsi']
"points" => [
"longtitude" => $row['logtitude'],
"latitude" => $row['latitude'],
"time" => $row['timestamp']
]
];
};
return array_map($formatter, $result);
The final array will have one element per point (veselMoveStatuses). If what you actually want is to have one element per vesel, consider changing the SELECT and adapt the formatter accordingly.
->select('v,m')
The result will be an array of Vesel objects with the veselMoveStatuses association already loaded (see this doc - section "Retrieve a CmsUser and fetch join all the phonenumbers he has").

Issue when trying to use doctrine queryBuilder

I have two entities Categorie and ChampCat with ManyToMany relation and I want to get list of categories with related ChampCat by using doctrine QueryBuilder so I have used this query:
class CategorieRepository extends \Doctrine\ORM\EntityRepository
{
public function myFindCategories(array $tab){
$em = $this->getEntityManager();
$query = $em->select(array('cat', 'ch'))
->from('AnnonceBundle\Entity\Categorie', 'cat')
->join('cat.champsCat', 'ch')
->where('cat.id In (:tabOfIds)')
->setParameter('tabOfIds', array_values($tab))
->getQuery();
return $query->getResult();
}
}
Here u can see the Categorie Entity:
<?php
namespace AnnonceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Categorie
*
* #ORM\Table(name="categorie")
* #ORM\Entity(repositoryClass="AnnonceBundle\Repository\CategorieRepository")
*/
class Categorie implements \JsonSerializable
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="refCat", type="string", length=100)
*/
private $refCat;
/**
* #var string
*
* #ORM\Column(name="libelleCat", type="string", length=50)
*/
private $libelleCat;
/**
* #ORM\ManyToMany(targetEntity="AnnonceBundle\Entity\ChampCat",cascade={"persist"})
*/
private $champsCat;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set refCat
*
* #param string $refCat
*
* #return Categorie
*/
public function setRefCat($refCat)
{
$this->refCat = $refCat;
return $this;
}
/**
* Get refCat
*
* #return string
*/
public function getRefCat()
{
return $this->refCat;
}
/**
* Set libelleCat
*
* #param string $libelleCat
*
* #return Categorie
*/
public function setLibelleCat($libelleCat)
{
$this->libelleCat = $libelleCat;
return $this;
}
/**
* Get libelleCat
*
* #return string
*/
public function getLibelleCat()
{
return $this->libelleCat;
}
/**
* Set champsCat
*
* #param \AnnonceBundle\Entity\ChampCat $champsCat
*
* #return Categorie
*/
public function setChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat = null)
{
$this->champsCat = $champsCat;
return $this;
}
/**
* Get champsCat
*
* #return \AnnonceBundle\Entity\ChampCat
*/
public function getChampsCat()
{
return $this->champsCat;
}
/**
* Constructor
*/
public function __construct()
{
$this->champsCat = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add champsCat
*
* #param \AnnonceBundle\Entity\ChampCat $champsCat
*
* #return Categorie
*/
public function addChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat)
{
$this->champsCat[] = $champsCat;
return $this;
}
/**
* Remove champsCat
*
* #param \AnnonceBundle\Entity\ChampCat $champsCat
*/
public function removeChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat)
{
$this->champsCat->removeElement($champsCat);
}
function jsonSerialize()
{
return get_object_vars($this);
}
}
And this is ChamCat Entity:
<?php
namespace AnnonceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ChampCat
*
* #ORM\Table(name="champ_cat")
* #ORM\Entity(repositoryClass="AnnonceBundle\Repository\ChampCatRepository")
*/
class ChampCat implements \JsonSerializable
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*#var string
*
* #ORM\Column(name="refCh", type="string")
*/
private $refCh;
/**
* #var string
*
* #ORM\Column(name="nom_ch", type="string", length=255)
*/
private $nomCh;
/**
* #var bool
*
* #ORM\Column(name="app_ch", type="boolean")
*/
private $appCh;
/**
* #var string
*
* #ORM\Column(name="format_ch", type="string", length=255)
*/
private $formatCh;
/**
* Get id
*
* #return string
*/
public function getId()
{
return $this->refCh;
}
/**
* Set refCh
*
* #param integer $refCh
* #return ChampCat
*/
public function setRefCh($refCh)
{
$this->refCh = $refCh;
return $this;
}
/**
* Get refCh
*
* #return integer
*/
public function getRefCh()
{
return $this->refCh;
}
/**
* Set nomCh
*
* #param string $nomCh
* #return ChampCat
*/
public function setNomCh($nomCh)
{
$this->nomCh = $nomCh;
return $this;
}
/**
* Get nomCh
*
* #return string
*/
public function getNomCh()
{
return $this->nomCh;
}
/**
* Set appCh
*
* #param boolean $appCh
* #return ChampCat
*/
public function setAppCh($appCh)
{
$this->appCh = $appCh;
return $this;
}
/**
* Get appCh
*
* #return boolean
*/
public function getAppCh()
{
return $this->appCh;
}
/**
* Set formatCh
*
* #param string $formatCh
* #return ChampCat
*/
public function setFormatCh($formatCh)
{
$this->formatCh = $formatCh;
return $this;
}
/**
* Get formatCh
*
* #return string
*/
public function getFormatCh()
{
return $this->formatCh;
}
function jsonSerialize()
{
return get_object_vars($this);
}
}
Unfortunately this query didn't work, so what am I missing ?
Is it working with this syntax?
public function myFindCategories(array $tab){
if(count($tab) == 0) return []; //or IN() will bug with an empty array
$query = $this->createQueryBuilder('cat')
->addSelect('ch')
->innerJoin('cat.champsCat', 'ch')
->where('cat.id in (:tabOfIds)')
->setParameter('tabOfIds', $tab)
->getQuery();
return $query->getResult();
}

Error while putting element into DB with Doctrine2 Symfony

I'm trying to put into a table called question tag an element so:
//the question with Id 1 exist already
$question = new Question();
$question->setId(1);
//the tag with name PYTHON exist already
$tag = new Tag();
$tag->setName("PYTHON");
$questionTag = new QuestionTag();
$questionTag->setQuestion($question);
$questionTag->setTag($tag);
//now I call a service to put the item into DB
$questionTagPF = $this->get('facade.QuestionTagFacade');
$res = $questionTagPF->create($questionTag);
This is the method to save the entity:
public function create( $entity ) {
$this->entityManager->getConnection()->beginTransaction();
try {
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->entityManager->getConnection()->commit();
return true;
} catch ( Exception $e ) {
$this->entityManager->getConnection()->rollBack();
return false;
}
}
And these are the entity classes:
relation (between question and tag):
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* QuestionTag
*
* #ORM\Table(name="question_tag", indexes={#ORM\Index(name="question", columns={"question"}), #ORM\Index(name="index_question_tag", columns={"tag"})})
* #ORM\Entity
*/
class QuestionTag
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \AppBundle\Entity\Question
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Question", cascade={ "persist" })
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="question", referencedColumnName="id")
* })
*/
private $question;
/**
* #var \AppBundle\Entity\Tag
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Tag", cascade={ "persist" })
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="tag", referencedColumnName="name")
* })
*/
private $tag;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set question
*
* #param \AppBundle\Entity\Question $question
*
* #return QuestionTag
*/
public function setQuestion(\AppBundle\Entity\Question $question = null)
{
$this->question = $question;
return $this;
}
/**
* Get question
*
* #return \AppBundle\Entity\Question
*/
public function getQuestion()
{
return $this->question;
}
/**
* Set tag
*
* #param \AppBundle\Entity\Tag $tag
*
* #return QuestionTag
*/
public function setTag(\AppBundle\Entity\Tag $tag = null)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* #return \AppBundle\Entity\Tag
*/
public function getTag()
{
return $this->tag;
}
}
question entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Question
*
* #ORM\Table(name="question", indexes={#ORM\Index(name="index_question_title", columns={"title"}), #ORM\Index(name="index_question_creation", columns={"creation_date"}), #ORM\Index(name="index_question_completed", columns={"completed"}), #ORM\Index(name="index_question_subject", columns={"subject"}), #ORM\Index(name="index_question_user", columns={"user_platform"})})
* #ORM\Entity
*/
class Question
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=50, nullable=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="text", type="string", length=1000, nullable=true)
*/
private $text;
/**
* #var \DateTime
*
* #ORM\Column(name="creation_date", type="date", nullable=false)
*/
private $creationDate;
/**
* #var boolean
*
* #ORM\Column(name="completed", type="boolean", nullable=true)
*/
private $completed = '0';
/**
* #var integer
*
* #ORM\Column(name="level", type="integer", nullable=true)
*/
private $level = '1';
/**
* #var \AppBundle\Entity\Subject
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Subject", cascade={ "persist" })
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="subject", referencedColumnName="name")
* })
*/
private $subject;
/**
* #var \AppBundle\Entity\UserPlatform
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\UserPlatform", cascade={ "persist" })
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_platform", referencedColumnName="username")
* })
*/
private $userPlatform;
/**
* Set id
*
* #param string $id
*
* #return Question
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Question
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set text
*
* #param string $text
*
* #return Question
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* #return string
*/
public function getText()
{
return $this->text;
}
/**
* Set creationDate
*
* #param \DateTime $creationDate
*
* #return Question
*/
public function setCreationDate($creationDate)
{
$this->creationDate = $creationDate;
return $this;
}
/**
* Get creationDate
*
* #return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Set completed
*
* #param boolean $completed
*
* #return Question
*/
public function setCompleted($completed)
{
$this->completed = $completed;
return $this;
}
/**
* Get completed
*
* #return boolean
*/
public function getCompleted()
{
return $this->completed;
}
/**
* Set level
*
* #param integer $level
*
* #return Question
*/
public function setLevel($level)
{
$this->level = $level;
return $this;
}
/**
* Get level
*
* #return integer
*/
public function getLevel()
{
return $this->level;
}
/**
* Set subject
*
* #param \AppBundle\Entity\Subject $subject
*
* #return Question
*/
public function setSubject(\AppBundle\Entity\Subject $subject = null)
{
$this->subject = $subject;
return $this;
}
/**
* Get subject
*
* #return \AppBundle\Entity\Subject
*/
public function getSubject()
{
return $this->subject;
}
/**
* Set userPlatform
*
* #param \AppBundle\Entity\UserPlatform $userPlatform
*
* #return Question
*/
public function setUserPlatform(\AppBundle\Entity\UserPlatform $userPlatform = null)
{
$this->userPlatform = $userPlatform;
return $this;
}
/**
* Get userPlatform
*
* #return \AppBundle\Entity\UserPlatform
*/
public function getUserPlatform()
{
return $this->userPlatform;
}
}
tag entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tag
*
* #ORM\Table(name="tag")
* #ORM\Entity
*/
class Tag
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=20)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $name = '';
/**
* Set name
*
* #param string $name
*
* #return tag
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
when I try to put a value into table question_tag I have this error:
An exception occurred while executing 'INSERT INTO tag (name) VALUES (?)' with params ["PYTHON"]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'PYTHON' for key 'PRIMARY'
Why?
It should not avoid entering a value if it already exists in the database? if this should be made explicit, how should I do?
thank you
In Symfony2 you have this things called Validators their job is to validate an object based on constraints so in your case you could use the following constraint in your tag entity
http://symfony.com/doc/current/reference/constraints/UniqueEntity.html
And then call to the validator service in your controller. You could avoid this step if you use FormTypes for your classes they automatically validate the object with the validator service.
http://symfony.com/doc/current/book/forms.html
Actually you could use the forms to validate data structure.. but that is offtopic
.....
The other way you could do this is by selecting the tag you want to insert and check if there is a result if not well then you can insert the tag if it exists you just recover it from the database and use the existing one
the problem was with the parameters that I give to the method to put data into db, so this code:
//the question with Id 1 exist already
$question = new Question();
$question->setId(1);
//the tag with name PYTHON exist already
$tag = new Tag();
$tag->setName("PYTHON");
$questionTag = new QuestionTag();
$questionTag->setQuestion($question);
$questionTag->setTag($tag);
//now I call a service to put the item into DB
$questionTagPF = $this->get('facade.QuestionTagFacade');
$res = $questionTagPF->create($questionTag);
have to be written so:
//the question with Id 1 exist already
$question = $this->getDoctrine()->getManager()->getReference('AppBundle:Question',"1");
//the tag with name PYTHON exist already
$tag = $this->getDoctrine()->getManager()->getReference('AppBundle:Tag',"PYTHON");
$questionTag = new QuestionTag();
$questionTag->setQuestion($question);
$questionTag->setTag($tag);
//now I call a service to put the item into DB
$questionTagPF = $this->get('facade.QuestionTagFacade');
$res = $questionTagPF->create($questionTag);
The problem was that to insert a value into a relation you have to take values through the manager!

Saving in database entity with 2 oneToMany relationship

i explain my problem, asking sorry for my english!
I have 2 entity: Projects and Operations. Each projects can have more operations, each operation can have different price.
So, i have created a new entity ProgettoOperazionePrezzo
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var float
*
* #ORM\Column(name="prezzo", type="decimal")
*/
private $prezzo;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set prezzo
*
* #param float $prezzo
* #return ProgettoOperazionePrezzo
*/
public function setPrezzo($prezzo)
{
$this->prezzo = $prezzo;
return $this;
}
/**
* Get prezzo
*
* #return float
*/
public function getPrezzo()
{
return $this->prezzo;
}
/**
* #ORM\ManyToOne(targetEntity="Progetto")
* #ORM\JoinColumn(name="progetto_id", referencedColumnName="id")
**/
private $progetto;
/**
* Set progetto
*
* #param \Management\ProgettiBundle\Entity\Progetto $progetto
* #return ProgettoOperazionePrezzo
*/
public function setProgetto(\Management\ProgettiBundle\Entity\Progetto $progetto = null)
{
$this->progetto = $progetto;
return $this;
}
/**
* Get progetto
*
* #return \Management\ProgettiBundle\Entity\Progetto
*/
public function getProgetto()
{
return $this->progetto;
}
/**
* #ORM\ManyToOne(targetEntity="Operazione")
* #ORM\JoinColumn(name="operazione_id", referencedColumnName="id")
**/
private $operazione;
/**
* Set operazione
*
* #param \Management\ProgettiBundle\Entity\Operazione $operazione
* #return ProgettoOperazionePrezzo
*/
public function setOperazione(\Management\ProgettiBundle\Entity\Operazione $operazione = null)
{
$this->operazione = $operazione;
return $this;
}
/**
* Get operazione
*
* #return \Management\ProgettiBundle\Entity\Operazione
*/
public function getOperazione()
{
return $this->operazione;
}
Now i want create a form to save in database all the operations that i chose with checkbutton.
$operazione = new ProgettoOperazionePrezzo();
$progetto = $this->getDoctrine()->getRepository('ManagementProgettiBundle:ProgettoOperazionePrezzo')->find($id_progetto);
$form = $this->createFormBuilder($operazione)
->add('operazione','entity',array(
'label'=>'Operazioni da effettuare:',
'multiple'=>true,
'expanded'=>true,
'class'=>'ManagementProgettiBundle:Operazione',
'property'=>'nome',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->orderBy('u.nome', 'ASC');
}))
->add('Passo successivo','submit');
$form->handleRequest($request);
if($form->isValid()){
$em = $this->getDoctrine()->getManager();
$operazione->setProgetto($progetto);
$em->persist($operazione);
$em->flush();
//return $this->redirect($this->generateUrl('management_progetti_nuovo_progetto_p3',array('id_progetto'=>$id_progetto)));
}
I have no problem to show the form...but it dwsnt works when i try to save in database my choices cause it give me this error:
Catchable Fatal Error: Argument 1 passed to Management\ProgettiBundle\Entity\ProgettoOperazionePrezzo::setOperazione() must be an instance of Management\ProgettiBundle\Entity\Operazione, instance of Doctrine\Common\Collections\ArrayCollection given, called in D:\xampp\htdocs\alemanno_management\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 345 and defined in D:\xampp\htdocs\alemanno_management\src\Management\ProgettiBundle\Entity\ProgettoOperazionePrezzo.php line 108
What am i doing of wrong?!!?maybe all??please...can u tell me how can i resolve this?!
Because you fetch multiple results from database, your $progetto variable will contain an instance of ArrayCollection, over which you should iterate. Because your query contains an id which should be unique, I assume that you want to fetch only one results:
$progetto = $this->getDoctrine()->getRepository('ManagementProgettiBundle:ProgettoOperazionePrezzo')->findOneBy(array('id' => $id_progetto));
Now your $progetto variable should point to an instance of Management\ProgettiBundle\Entity\Operazione.
Try to use next entity, please:
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var float
*
* #ORM\Column(name="prezzo", type="decimal")
*/
private $prezzo;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set prezzo
*
* #param float $prezzo
* #return ProgettoOperazionePrezzo
*/
public function setPrezzo($prezzo)
{
$this->prezzo = $prezzo;
return $this;
}
/**
* Get prezzo
*
* #return float
*/
public function getPrezzo()
{
return $this->prezzo;
}
/**
* #ORM\ManyToOne(targetEntity="Progetto")
* #ORM\JoinColumn(name="progetto_id", referencedColumnName="id")
**/
private $progetto;
/**
* Set progetto
*
* #param \Management\ProgettiBundle\Entity\Progetto $progetto
* #return ProgettoOperazionePrezzo
*/
public function setProgetto(\Management\ProgettiBundle\Entity\Progetto $progetto = null)
{
$this->progetto = $progetto;
return $this;
}
/**
* Get progetto
*
* #return \Management\ProgettiBundle\Entity\Progetto
*/
public function getProgetto()
{
return $this->progetto;
}
public function __construct() {
$this->operazione = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #ORM\ManyToOne(targetEntity="Operazione")
* #ORM\JoinColumn(name="operazione_id", referencedColumnName="id")
**/
private $operazione;
/**
* Set operazione
*
* #param \Doctrine\Common\Collections\ArrayCollection $operazione
* #return ProgettoOperazionePrezzo
*/
public function setOperazione(\Doctrine\Common\Collections\ArrayCollection $operazione = null)
{
$this->operazione = $operazione;
return $this;
}
/**
* Get operazione
*
* #return \Doctrine\Common\Collections\ArrayCollection
*/
public function getOperazione()
{
return $this->operazione;
}

Categories