Fatal error: Cannot redeclare class "Activity" in PHP - php

// I really dont have any class like that I checked everywhere...even if I give it different name .. I get the same error
// This is my Php file which is an Entity for Doctrine
use Doctrine\ORM\Mapping AS ORM;
/**
* Description of Activity
*
* #author pawanadhikari
*/
/**
* #Entity
* #Table(name="pulse_activity")
*/
class Activity {
/**
* #var integer $id
* #Id
* #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer $patientId
* #Id
* #Column(type="integer")
*/
private $patientId;
/**
* #var integer $steps
* #Column(type="integer")
*/
private $steps;
/**
* #var double $calories
* #Column(type="double")
*/
private $calories;
/**
* #var double $distance
* #Column(type="double")
*/
private $distance;
/**
* #var integer $elevation
* #Column(type="integer")
*/
private $elevation;
/**
* #var integer $secondsActivitySoft
* #Column(type="integer")
*/
private $secondsActivitySoft;
/**
* #var integer $secondsActivityModerate
* #Column(type="integer")
*/
private $secondsActivityModerate;
/**
* #var integer $secondsActivityIntense
* #Column(type="integer")
*/
private $secondsActivityIntense;
/**
* #var string $date
* #Column(type="string")
*/
private $date;
/**
* #var string $timeStamp
* #Column(type="string")
*/
private $timeStamp;
/**
* #ManyToOne(targetEntity = "Person", inversedBy = "personActivities" )
* * #JoinColumn(name="patientId", referencedColumnName="id")
*
*/
protected $patientActivity;
public function getId() {
return $this->id;
}
public function getPatientId() {
return $this->patientId;
}
public function getSteps() {
return $this->steps;
}
public function getCalories() {
return $this->calories;
}
public function getDistance() {
return $this->distance;
}

you've declared your own unique namespace, right? you're just not showing it?
where are you getting the error? are you are calling these from custom runtime pages or is the error occuring when you generate the model.
This kind of error is common when include() and require() are used instead of include_once() or require_once() - particularly if used in an autoload function for the classes.

Related

ongr-io elasticsearch bundle implementation with doctrine symfony2

In our symfony2 project we want to implement elasticsearch with ongr-io bundle, but our whole system is built on doctrine. Is it possible to somehow use ongr-io elasticsearch bundle without totally redoing whole database with documents instead of entities?
Entity that I want to index (fields for search would be: name, ChildCategory and ParentCategory):
/**
* Course
*
* #ORM\Table(name="course")
* #ORM\Entity(repositoryClass="CoreBundle\Repository\CourseRepository")
* #ORM\HasLifecycleCallbacks
*/
class Course
{
/**
* #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, unique=false)
*/
private $name;
/**
* #var ParentCategory
*
* #ORM\ManyToOne(targetEntity="ParentCategory")
* #ORM\JoinColumn(name="parentCategory_id", referencedColumnName="id")
*/
private $parentCategory;
/**
* #var ChildCategory
*
* #ORM\ManyToOne(targetEntity="ChildCategory", inversedBy="courses")
* #ORM\JoinColumn(name="childCategory_id", referencedColumnName="id")
*/
private $childCategory;
/**
* #var City
*
* #ORM\ManyToOne(targetEntity="City")
* #ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
/**
* #var Users
*
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\Users", inversedBy="course")
* #ORM\JoinColumn(name="owner_id", referencedColumnName="id")
*/
private $owner;
/**
* #var text
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var Picture
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Picture", mappedBy="course", cascade={"persist", "remove"})
*/
private $pictures;
/**
* #var Ratings
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Ratings", mappedBy="courseid")
*/
private $ratings;
public $avgRating;
}
it's very interesting.
The first thing is Entity location. The document for ElasticsearchBundle has to be in Document directory. It's not a big deal. All you need to do is to create an empty class and extend entity with #Document annotation. Next are the fields. With name field there is no problem at all, just add #property annotation and you are good. More interesting is with relations. My suggestion would be to create separate property and in the getter return value from the relation for that field. I hope you got the idea.
Update:
Here's an example of documents:
AppBundle/Entity/Post
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use ONGR\ElasticsearchBundle\Annotation as ES;
/**
* Post
*
* #ORM\Table(name="post")
* #ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
*/
class Post
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ES\Property(type="string")
* #ORM\Column(name="title", type="string", length=255, nullable=true)
*/
private $title;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* #var string
*
* #ES\Property(name="user", type="string")
*/
private $esUser;
/**
* Get id
*
* #return int
*/
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 user
*
* #param \AppBundle\Entity\User $user
*
* #return Post
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* #return string
*/
public function getEsUser()
{
return $this->esUser;
}
/**
* #param string $esUser
*/
public function setEsUser($esUser)
{
$this->esUser = $esUser;
}
}
AppBundle/Document/Post
<?php
namespace AppBundle\Document;
use ONGR\ElasticsearchBundle\Annotation as ES;
use AppBundle\Entity\Post as OldPost;
/**
* #ES\Document()
*/
class Post extends OldPost
{
}

Doctrine 2 Issue

I am trying to learn the framework Doctrine 2. I have a Model in MySQL and implement it in Doctrine. After the implementation of the dependency between the two classes Task and Dropdown list, it doesnt operate.
My code is:
<?php
use Doctrine\Common\Collections;
use Doctrine\ORM\Mapping AS ORM;
/**
* Task
*
* #Table(name="task")
* #Entity
*/
class Task
{
/**
* #var integer
*
* #Column(name="ID", type="integer", nullable=false)
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var Dropdownlist
* #ORM\OneToMany(targetEntity="Dropdownlist", mappedBy="tasks")
* #ORM\JoinColumn(name="priority", referencedColumnName="id")
*/
protected $priority;
/**
* #var string
*
* #Column(name="Label", type="string", length=45, nullable=true)
*/
private $label;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set priority
*
* #param integer $priority
*
* #return Task
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
/**
* Get priority
*
* #return integer
*/
public function getPriority()
{
return $this->priority;
}
/**
* Set label
*
* #param string $label
*
* #return Task
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* Get label
*
* #return string
*/
public function getLabel()
{
return $this->label;
}
}
/**
* Dropdownlist
*
* #Table(name="dropdownlist")
* #Entity
*/
class Dropdownlist
{
/**
* #var integer
*
* #Column(name="ID", type="integer")
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #Column(name="PriorityLabel", type="string", length=45, nullable=true)
*/
private $prioritylabel;
/*
* #var ArrayCollection
* #ORM\ManyToOne(targetEntity="Task", inversedBy="priority")
*/
protected $tasks;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set prioritylabel
*
* #param string $prioritylabel
*
* #return Dropdownlist
*/
public function setPrioritylabel($prioritylabel)
{
$this->prioritylabel = $prioritylabel;
return $this;
}
/**
* Get prioritylabel
*
* #return string
*/
public function getPrioritylabel()
{
return $this->prioritylabel;
}
public function getTasts(){
return $this->tasks;
}
public function __construct()
{
}
}
Problem:
The database schema is not in sync with the current mapping file.
What is the reason?
Where is the problem?
Best regards
It means your database is not in sync with your current mapping.
Run doctrine:schema:update --complete --dump-sql to see which changes need to be done. You can also run doctrine:schema:update --complete --force to deploy the changes directly.

Why command generate:doctrine:entities don't generate methods in these classes? (reverse engineering)

(I'm a beginner)
I would like to automatically generate methods for related models (OneToMany and ManyToOne). Im doing it following docs http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
I added following properties and annotations to the models (I give here only two classes related by ManyToOne as a example) and run generate:doctrine:entities command with no errors and no results. I expected methods like __construct and addXXX() in Kategoria class with OnoToMany relation.
This is fragment I added to Kategoria class.
/**
* #ORM\OneToMany(targetEntity="Ksiazka", mappedBy="kategoria")
*/
protected $ksiazki;
This is entire Kategoria class with OneToMany relation.
#Kategoria.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Kategoria
*
* #ORM\Table(name="kategoria")
* #ORM\Entity
*/
class Kategoria
{
/**
* #var string
*
* #ORM\Column(name="nazwa", type="string", length=45, nullable=true)
*/
private $nazwa;
/**
* #var integer
*
* #ORM\Column(name="idKategoria", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idkategoria;
#THESE NEXT 4 LINES I ADDED
/**
* #ORM\OneToMany(targetEntity="Ksiazka", mappedBy="kategoria")
*/
protected $ksiazki;
/**
* Set nazwa
*
* #param string $nazwa
* #return Kategoria
*/
public function setNazwa($nazwa)
{
$this->nazwa = $nazwa;
return $this;
}
/**
* Get nazwa
*
* #return string
*/
public function getNazwa()
{
return $this->nazwa;
}
/**
* Get idkategoria
*
* #return integer
*/
public function getIdkategoria()
{
return $this->idkategoria;
}
}
This is a fragment I modified in Ksiazka class. I added , inversedBy="ksiazki"
/**
* #var \AppBundle\Entity\Kategoria
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria", inversedBy="ksiazki")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
* })
*/
private $idkategoria;
This is entire Ksiazka class with ManyToOne relation.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ksiazka
*
* #ORM\Table(name="ksiazka", indexes={#ORM\Index(name="idKategoria_idx", columns={"idKategoria"})})
* #ORM\Entity
*/
class Ksiazka
{
/**
* #var string
*
* #ORM\Column(name="autor", type="string", length=45, nullable=true)
*/
private $autor;
/**
* #var string
*
* #ORM\Column(name="opis", type="text", length=65535, nullable=true)
*/
private $opis;
/**
* #var string
*
* #ORM\Column(name="cena", type="decimal", precision=10, scale=2, nullable=true)
*/
private $cena;
/**
* #var string
*
* #ORM\Column(name="obrazek", type="string", length=45, nullable=true)
*/
private $obrazek;
/**
* #var string
*
* #ORM\Column(name="wydawnictwo", type="string", length=45, nullable=true)
*/
private $wydawnictwo;
/**
* #var string
*
* #ORM\Column(name="rokWydania", type="string", length=45, nullable=true)
*/
private $rokwydania;
/**
* #var string
*
* #ORM\Column(name="isbn", type="string", length=45)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $isbn;
// Było. Automatycznie wygenerowane bez inversedBy.
// /**
// * #var \AppBundle\Entity\Kategoria
// *
// * #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria")
// * #ORM\JoinColumns({
// * #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
// * })
// */
// private $idkategoria;
/**
* #var \AppBundle\Entity\Kategoria
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria", inversedBy="ksiazki")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
* })
*/
private $idkategoria;
/**
* #ORM\OneToMany(targetEntity="Zamowienie_Produkt", mappedBy="ksiazka")
*/
protected $zamowienie_produkty;
/**
* Set autor
*
* #param string $autor
* #return Ksiazka
*/
public function setAutor($autor)
{
$this->autor = $autor;
return $this;
}
/**
* Get autor
*
* #return string
*/
public function getAutor()
{
return $this->autor;
}
/**
* Set opis
*
* #param string $opis
* #return Ksiazka
*/
public function setOpis($opis)
{
$this->opis = $opis;
return $this;
}
/**
* Get opis
*
* #return string
*/
public function getOpis()
{
return $this->opis;
}
/**
* Set cena
*
* #param string $cena
* #return Ksiazka
*/
public function setCena($cena)
{
$this->cena = $cena;
return $this;
}
/**
* Get cena
*
* #return string
*/
public function getCena()
{
return $this->cena;
}
/**
* Set obrazek
*
* #param string $obrazek
* #return Ksiazka
*/
public function setObrazek($obrazek)
{
$this->obrazek = $obrazek;
return $this;
}
/**
* Get obrazek
*
* #return string
*/
public function getObrazek()
{
return $this->obrazek;
}
/**
* Set wydawnictwo
*
* #param string $wydawnictwo
* #return Ksiazka
*/
public function setWydawnictwo($wydawnictwo)
{
$this->wydawnictwo = $wydawnictwo;
return $this;
}
/**
* Get wydawnictwo
*
* #return string
*/
public function getWydawnictwo()
{
return $this->wydawnictwo;
}
/**
* Set rokwydania
*
* #param string $rokwydania
* #return Ksiazka
*/
public function setRokwydania($rokwydania)
{
$this->rokwydania = $rokwydania;
return $this;
}
/**
* Get rokwydania
*
* #return string
*/
public function getRokwydania()
{
return $this->rokwydania;
}
/**
* Get isbn
*
* #return string
*/
public function getIsbn()
{
return $this->isbn;
}
/**
* Set idkategoria
*
* #param \AppBundle\Entity\Kategoria $idkategoria
* #return Ksiazka
*/
public function setIdkategoria(\AppBundle\Entity\Kategoria $idkategoria = null)
{
$this->idkategoria = $idkategoria;
return $this;
}
/**
* Get idkategoria
*
* #return \AppBundle\Entity\Kategoria
*/
public function getIdkategoria()
{
return $this->idkategoria;
}
}
Try to delete all methods in Entity class. Then run doctrine:generate:entities command. If not help, try to find mapping errors in profiler. Hope it help.

ManyToOne relation Symfony2

I have a many to one relation between my entities.
An application can have activities
<?php
namespace AppAcademic\ApplicationBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Activity
*
* #ORM\Table()
* #ORM\Entity
*/
class Activity
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="AppAcademic\ApplicationBundle\Entity\Application", inversedBy="activity")
* #ORM\JoinColumn(name="application_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $application;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function setApplication($application)
{
$this->application = $application;
return $this;
}
public function getApplication()
{
return $this->application;
}
/**
* Set title
*
* #param string $title
* #return Activity
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
}
And the application
/**
* Application
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppAcademic\ApplicationBundle\Entity\ApplicationRepository")
*/
class Application
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="AppAcademic\ApplicationBundle\Entity\Activity", mappedBy="application")
*/
protected $activities;
...
}
I have this in the profiler:
AppAcademic\ApplicationBundle\Entity\Application
The mappings AppAcademic\ApplicationBundle\Entity\Application#activities and AppAcademic\ApplicationBundle\Entity\Activity#application are inconsistent with each other.
AppAcademic\ApplicationBundle\Entity\Application
The mappings AppAcademic\ApplicationBundle\Entity\Application#activities and AppAcademic\ApplicationBundle\Entity\Activity#application are inconsistent with each other.
AppAcademic\ApplicationBundle\Entity\Activity
The association AppAcademic\ApplicationBundle\Entity\Activity#application refers to the inverse side field AppAcademic\ApplicationBundle\Entity\Application#activity which does not exist.
For the mapping annotation ManyToOne on the Activity::$application property, the attribute
inversedBy="activity"
This should be
inversedBy="activities"

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