I have the problem when I want to select for example all the categories in my table that Doctrine only selects the first row data.
For example:
I have 3 categories (id: 1, 2 & 3). I want to select all the
categories by doing this: $categories =
$entityManager->getRepository('ReuzzeReuzzeBundle:Categories')
->findAll();
What I get now is: 3 times the first entity!
I have 3 categories (id: 1, 2 & 3). I want to select the category
with id = 2 by doing this: $category =
$entityManager->getRepository('ReuzzeReuzzeBundle:Categories')->findOneByCategoryId(2);
then I get again the first category with id = 1 ...
Is this is a settings problem or other ... ? Can somebody help me with this? It's very annoying when you can't select all the data :/
UPDATE:
My Entity Categories Class:
<?php
namespace Reuzze\ReuzzeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Categories
*
* #ORM\Table(name="categories", indexes={#ORM\Index(name="fk_categories_categories1_idx", columns={"category_parentid"})})
* #ORM\Entity
*/
class Categories
{
/**
* #var boolean
*
* #ORM\Column(name="category_id", type="boolean")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
/**
* #var string
*
* #ORM\Column(name="category_name", type="string", length=45, nullable=false)
*/
private $categoryName;
/**
* #var string
*
* #ORM\Column(name="category_description", type="string", length=255, nullable=false)
*/
private $categoryDescription;
/**
* #var \DateTime
*
* #ORM\Column(name="category_created", type="datetime", nullable=false)
*/
private $categoryCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="category_modified", type="datetime", nullable=true)
*/
private $categoryModified;
/**
* #var \DateTime
*
* #ORM\Column(name="category_deleted", type="datetime", nullable=true)
*/
private $categoryDeleted;
/**
* #var \Reuzze\ReuzzeBundle\Entity\Categories
*
* #ORM\ManyToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Categories")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_parentid", referencedColumnName="category_id")
* })
*/
private $categoryParentid;
/**
* Get categoryId
*
* #return boolean
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Set categoryName
*
* #param string $categoryName
* #return Categories
*/
public function setCategoryName($categoryName)
{
$this->categoryName = $categoryName;
return $this;
}
/**
* Get categoryName
*
* #return string
*/
public function getCategoryName()
{
return $this->categoryName;
}
/**
* Set categoryDescription
*
* #param string $categoryDescription
* #return Categories
*/
public function setCategoryDescription($categoryDescription)
{
$this->categoryDescription = $categoryDescription;
return $this;
}
/**
* Get categoryDescription
*
* #return string
*/
public function getCategoryDescription()
{
return $this->categoryDescription;
}
/**
* Set categoryCreated
*
* #param \DateTime $categoryCreated
* #return Categories
*/
public function setCategoryCreated($categoryCreated)
{
$this->categoryCreated = $categoryCreated;
return $this;
}
/**
* Get categoryCreated
*
* #return \DateTime
*/
public function getCategoryCreated()
{
return $this->categoryCreated;
}
/**
* Set categoryModified
*
* #param \DateTime $categoryModified
* #return Categories
*/
public function setCategoryModified($categoryModified)
{
$this->categoryModified = $categoryModified;
return $this;
}
/**
* Get categoryModified
*
* #return \DateTime
*/
public function getCategoryModified()
{
return $this->categoryModified;
}
/**
* Set categoryDeleted
*
* #param \DateTime $categoryDeleted
* #return Categories
*/
public function setCategoryDeleted($categoryDeleted)
{
$this->categoryDeleted = $categoryDeleted;
return $this;
}
/**
* Get categoryDeleted
*
* #return \DateTime
*/
public function getCategoryDeleted()
{
return $this->categoryDeleted;
}
/**
* Set categoryParentid
*
* #param \Reuzze\ReuzzeBundle\Entity\Categories $categoryParentid
* #return Categories
*/
public function setCategoryParentid(\Reuzze\ReuzzeBundle\Entity\Categories $categoryParentid = null)
{
$this->categoryParentid = $categoryParentid;
return $this;
}
/**
* Get categoryParentid
*
* #return \Reuzze\ReuzzeBundle\Entity\Categories
*/
public function getCategoryParentid()
{
return $this->categoryParentid;
}
}
It looks like your problem is with you annotations definition of your categoryId. You have defined it as a boolean. This should be an integer. That would probably be why you are only getting the category with id 1.
Related
I've been running into a problem when I'm trying to create a list of Movies with specific variable Genre. For example, all Movies with Genre "romance" and so on.
The error is:
ContextErrorException: Notice: Undefined index: genre in /(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
Entity Genre looks like this:
/**
* Genre entity.
*
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Genre.
*
* #package Model
* #ORM\Table(name="genre")
* #ORM\Entity(repositoryClass="AppBundle\Repository\Genre")
*/
class Genre
{
/**
* Id.
*
* #ORM\Id
* #ORM\Column(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #var integer $id
*/
private $id;
/**
* Name.
*
* #ORM\Column(
* name="name",
* type="string",
* length=128,
* nullable=false
* )
*
* #var string $name
*/
private $name;
/**
* Movies array
*
* #ORM\OneToMany(
* targetEntity="AppBundle\Entity\Movie",
* mappedBy="genre"
* )
*/
protected $movies;
/**
* Add movies.
*
* #param \AppBundle\Entity\Movie $movies Movies
*
* #return mixed
*/
public function addMovie(\AppBundle\Entity\Movie $movies)
{
$this->movies[] = $movies;
}
/**
* Remove movies
*
* #param \AppBundle\Entity\Movie $movies Movies
*
* #return mixed
*/
public function removeAnswer(\AppBundle\Entity\Movie $movies)
{
$this->movies->removeElement($movies);
}
/**
* Get movies.
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMovies()
{
return $this->movies;
}
/**
* Constructor
*/
public function __construct()
{
$this->movies = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Remove movies
*
* #param \AppBundle\Entity\Movie $movies Movies
*
* #return mixed
*/
public function removeMovie(\AppBundle\Entity\Movie $movies)
{
$this->movies->removeElement($movies);
}
/**
* Get Id.
*
* #return integer Result
*/
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
/**
* Set name.
*
* #param string $name Name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name.
*
* #return string Name
*/
public function getName()
{
return $this->name;
}
}
While movie looks like this:
/**
* Movie entity.
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Movie.
*
* #package Model
* #ORM\Table(name="movies")
* #ORM\Entity(repositoryClass="AppBundle\Repository\Movie")
*/
class Movie
{
/**
* Id.
*
* #ORM\Id
* #ORM\Column(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #var integer $id
*/
private $id;
/**
* Title.
*
* #ORM\Column(
* name="title",
* type="string",
* length=255,
* nullable=false
* )
*
* #var string $title
*/
private $title;
/**
* Notes.
*
* #ORM\Column(
* name="notes",
* type="text",
* nullable=true
* )
*
* #var string $notes
*/
private $notes;
/**
* Genre array
*
* #ORM\ManyToOne(targetEntity="Genre")
* #ORM\JoinColumn(name="genre_id", referencedColumnName="id")
* )
*
* #var \Doctrine\Common\Collections\ArrayCollection $genres
*/
protected $genres;
/**
* Constructor.
*/
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param integer $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Set title.
*
* #param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title.
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set notes.
*
* #param string $notes
*/
public function setNotes($notes)
{
$this->notes = $notes;
}
/**
* Get notes.
*
* #return string
*/
public function getNotes()
{
return $this->notes;
}
/**
* Add genre
*
* #param \AppBundle\Entity\Genre $genres
* #return Movie
*/
public function addGenre(\AppBundle\Entity\Genre $genres)
{
$this->genres[] = $genres;
return $this;
}
/**
* Remove genre
*
* #param \AppBundle\Entity\Genre $genres
*/
public function removeGenre(\AppBundle\Entity\Genre $genres)
{
$this->generes->removeElement($generes);
}
/**
* Get genre
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getGenre()
{
return $this->genres;
}
/**
* Set genre
*
* #param \AppBundle\Entity\Genre $genres
* #return Movie
*/
public function setGenre(\AppBundle\Entity\Genre $genres = null)
{
$this->genres = $genres;
return $this;
}
And while trying to do this, I use this in GenreController:
public function viewAction(Genre $genre)
{
if (!$genre) {
throw new NotFoundHttpException('Genre not found!');
}
$movies= $genre->getMovies();
return $this->templating->renderResponse(
'AppBundle:Genre:view.html.twig',
array('genre' => $genre)
);
}
Now every time I try to generate the page, I get a error message:
ContextErrorException: Notice: Undefined index: genre in /home/(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1758
It's a first time I'm doing something like this, so I'm fully aware it's probably full of errors, but I have no idea where this one is coming from, so it's really hard for me to do anything about it. I would appreciate, if someone would show me my mistakes.
The entity name is Genre, but you reference it as Genere in some parts of the code
i have two entities that i would like to map them so that when i generate crud for each entity when i want to make a new insert using Map entity to be able to select based on id from Board entity.
I've been trying to make the mapping yet .. i didn't manage not sure if i did it right or not because in mysql i don't see a foreign key after sql generation.
Board.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Board
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\BoardRepository")
*/
class Board
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\ManyToOne(targetEntity="Board")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="BoardId")
* })
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="PropertyName", type="string", length=255)
*/
private $propertyName;
/**
* #var string
*
* #ORM\Column(name="PropertyDescription", type="string", length=255)
*/
private $propertyDescription;
/**
* #var string
*
* #ORM\Column(name="PropertyPicture", type="string", length=255)
*/
private $propertyPicture;
/**
* #var string
*
* #ORM\Column(name="PropertyGlyphonic", type="string", length=255)
*/
private $propertyGlyphonic;
/**
* #var string
*
* #ORM\Column(name="PropertyPrice", type="string", length=255)
*/
private $propertyPrice;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set propertyName
*
* #param string $propertyName
* #return Board
*/
public function setPropertyName($propertyName)
{
$this->propertyName = $propertyName;
return $this;
}
/**
* Get propertyName
*
* #return string
*/
public function getPropertyName()
{
return $this->propertyName;
}
/**
* Set propertyDescription
*
* #param string $propertyDescription
* #return Board
*/
public function setPropertyDescription($propertyDescription)
{
$this->propertyDescription = $propertyDescription;
return $this;
}
/**
* Get propertyDescription
*
* #return string
*/
public function getPropertyDescription()
{
return $this->propertyDescription;
}
/**
* Set propertyPicture
*
* #param string $propertyPicture
* #return Board
*/
public function setPropertyPicture($propertyPicture)
{
$this->propertyPicture = $propertyPicture;
return $this;
}
/**
* Get propertyPicture
*
* #return string
*/
public function getPropertyPicture()
{
return $this->propertyPicture;
}
/**
* Set propertyGlyphonic
*
* #param string $propertyGlyphonic
* #return Board
*/
public function setPropertyGlyphonic($propertyGlyphonic)
{
$this->propertyGlyphonic = $propertyGlyphonic;
return $this;
}
/**
* Get propertyGlyphonic
*
* #return string
*/
public function getPropertyGlyphonic()
{
return $this->propertyGlyphonic;
}
/**
* Set propertyPrice
*
* #param string $propertyPrice
* #return Board
*/
public function setPropertyPrice($propertyPrice)
{
$this->propertyPrice = $propertyPrice;
return $this;
}
/**
* Get propertyPrice
*
* #return string
*/
public function getPropertyPrice()
{
return $this->propertyPrice;
}
}
Map.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Map
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\MapRepository")
*/
class Map
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
* #ORM\Column(name="BoardId", type="integer")
*/
private $boardId;
/**
* #var string
*
* #ORM\Column(name="X", type="string", length=255)
*/
private $x;
/**
* #var string
*
* #ORM\Column(name="Y", type="string", length=255)
*/
private $y;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set boardId
*
* #param integer $boardId
* #return Map
*/
public function setBoardId($boardId)
{
$this->boardId = $boardId;
return $this;
}
/**
* Get boardId
*
* #return integer
*/
public function getBoardId()
{
return $this->boardId;
}
/**
* Set x
*
* #param string $x
* #return Map
*/
public function setX($x)
{
$this->x = $x;
return $this;
}
/**
* Get x
*
* #return string
*/
public function getX()
{
return $this->x;
}
/**
* Set y
*
* #param string $y
* #return Map
*/
public function setY($y)
{
$this->y = $y;
return $this;
}
/**
* Get y
*
* #return string
*/
public function getY()
{
return $this->y;
}
}
It seams like you haven't read the manual. It is great documentation on both Symfony.com and Doctrine-orm.readthedocs.org.
Read this article (http://symfony.com/doc/current/book/doctrine.html) and use this one as reference (http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html) and you will find the answer to this and many future questions.
What you basically need to do is:
class Board {
/**
* #ORM\ManyToOne(targetEntity="Map")
*/
private $map;
}
I'm trying to putting up my first join with Symfony2 createNativeQuery.
I made the first one without join and there's no problem.
I think I lost some steps, to follow my code:
$rsm = new ResultSetMapping;
$rsm
->addEntityResult('Art\ArticleBundle\Entity\ArticleData', 'ad')
// ->addEntityResult('Art\ArticleBundle\Entity\Article', 'a')
// ->addFieldResult('ad', 'id', 'article_data_id')
->addJoinedEntityResult('Art\ArticleBundle\Entity\Article', 'a', 'ad', 'article')
->addFieldResult('a', 'date_article', 'dateArticle');
$query = $this->_em->createNativeQuery(
'SELECT ad.id AS article_data_id, ad.title, ad.abstract '
. 'FROM art_article_data ad '
. 'INNER JOIN art_article ON (ad.article_id = a.id) '
,
$rsm
);
return $query->getResult();
It returns the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'a.id' in 'on clause'").
Why a.id is unknown? It is within the entity and the table.
What step have I lost ?
As #Javad required, below there's the Article entity
<?php
namespace Art\ArticleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Art\ArticleBundle\Entity\ArticleType;
use Art\ArticleBundle\Entity\ArticleCategory;
use Art\ArticleBundle\Entity\ArticleData;
use Art\ArticleBundle\Entity\ArticleHasBrand;
/**
* Article
*
* #ORM\Entity(repositoryClass="Art\ArticleBundle\Entity\Repository\ArticleRepository")
* #ORM\Table(name="art_article", options={"collate"="utf8_general_ci", "charset"="utf8"})
* #ORM\HasLifecycleCallbacks()
*/
class Article
{
function __construct()
{
$this->articles__article_data = new ArrayCollection();
$this->articles_article_has_brand = new ArrayCollection();
$this->setDateInsert(new \DateTime);
$this->setDateEdit(new \DateTime);
$this->setIsVisible(1);
$this->setIsMain(0);
$this->setOrd(0);
}
function __toString()
{
// $this->dateArticle;
}
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="id", type="integer", options={"unsigned"=true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \stdClass
*
* #ORM\ManyToOne(targetEntity="ArticleType", inversedBy="article_types__article")
* #ORM\JoinColumn(name="article_type_id", referencedColumnName="id", nullable=false)
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $articleType;
/**
* #var \stdClass
*
* #ORM\ManyToOne(targetEntity="ArticleCategory", inversedBy="article_categories__article")
* #ORM\JoinColumn(name="article_category_id", referencedColumnName="id", nullable=true)
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $articleCategory;
/**
* #var \DateTime
*
* #ORM\Column(name="date_article", type="date", nullable=true)
*/
private $dateArticle;
/**
* #var string
*
* #ORM\Column(name="url", type="string", length=127, nullable=true)
*/
private $url;
/**
* #var \DateTime
*
* #ORM\Column(name="date_insert", type="datetime", nullable=false)
*/
private $dateInsert;
/**
* #var \DateTime
*
* #ORM\Column(name="date_edit", type="datetime", nullable=true)
*/
private $dateEdit;
/**
* #var boolean
*
* #ORM\Column(name="is_visible", type="boolean", options={"unsigned"=true, "default"=1}, nullable=false)
*/
private $isVisible;
/**
* #var boolean
*
* #ORM\Column(name="is_main", type="boolean", options={"unsigned"=true, "default"=0}, nullable=false)
*/
private $isMain;
/**
* #var integer
*
* #ORM\Column(name="ord", type="integer", options={"unsigned"=true, "default"=0}, nullable=true)
*/
private $ord;
/**
* #var object
*
* #ORM\OneToMany(targetEntity="ArticleData", mappedBy="article")
*/
private $articles__article_data;
/**
* #var object
*
* #ORM\OneToMany(targetEntity="ArticleHasBrand", mappedBy="article")
*/
private $articles_article_has_brand;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set dateArticle
*
* #param \DateTime $dateArticle
* #return Article
*/
public function setDateArticle($dateArticle)
{
$this->dateArticle = $dateArticle;
return $this;
}
/**
* Get dateArticle
*
* #return \DateTime
*/
public function getDateArticle()
{
return $this->dateArticle;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set dateInsert
*
* #param string $url
* #return Article
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* Set dateInsert
*
* #param \DateTime $dateInsert
* #return Article
*/
public function setDateInsert($dateInsert)
{
$this->dateInsert = $dateInsert;
return $this;
}
/**
* Get dateInsert
*
* #return \DateTime
*/
public function getDateInsert()
{
return $this->dateInsert;
}
/**
* Set dateEdit
*
* #param \DateTime $dateEdit
* #return Article
*/
public function setDateEdit($dateEdit)
{
$this->dateEdit = $dateEdit;
return $this;
}
/**
* Get dateEdit
*
* #return \DateTime
*/
public function getDateEdit()
{
return $this->dateEdit;
}
/**
* Set isVisible
*
* #param boolean $isVisible
* #return Article
*/
public function setIsVisible($isVisible)
{
$this->isVisible = $isVisible;
return $this;
}
/**
* Get isVisible
*
* #return boolean
*/
public function getIsVisible()
{
return $this->isVisible;
}
/**
* Set isMain
*
* #param boolean $isMain
* #return Article
*/
public function setIsMain($isMain)
{
$this->isMain = $isMain;
return $this;
}
/**
* Get isMain
*
* #return boolean
*/
public function getIsMain()
{
return $this->isMain;
}
/**
* Set ord
*
* #param integer $ord
* #return Article
*/
public function setOrd($ord)
{
$this->ord = $ord;
return $this;
}
/**
* Get ord
*
* #return integer
*/
public function getOrd()
{
return $this->ord;
}
/**
* Set articleType
*
* #param \Art\ArticleBundle\Entity\ArticleType $articleType
* #return Article
*/
public function setArticleType(ArticleType $articleType = null)
{
$this->articleType = $articleType;
return $this;
}
/**
* Get articleType
*
* #return \Art\ArticleBundle\Entity\ArticleType
*/
public function getArticleType()
{
return $this->articleType;
}
/**
* Set articleCategory
*
* #param \Art\ArticleBundle\Entity\ArticleCategory $articleCategory
* #return Article
*/
public function setArticleCategory(ArticleCategory $articleCategory = null)
{
$this->articleCategory = $articleCategory;
return $this;
}
/**
* Get articleCategory
*
* #return \Art\ArticleBundle\Entity\ArticleCategory
*/
public function getArticleCategory()
{
return $this->articleCategory;
}
/**
* Add articles__article_data
*
* #param \Art\ArticleBundle\Entity\ArticleData $articlesArticleData
* #return Article
*/
public function addArticlesArticleData(ArticleData $articlesArticleData)
{
$this->articles__article_data[] = $articlesArticleData;
return $this;
}
/**
* Remove articles__article_data
*
* #param \Art\ArticleBundle\Entity\ArticleData $articlesArticleData
*/
public function removeArticlesArticleData(ArticleData $articlesArticleData)
{
$this->articles__article_data->removeElement($articlesArticleData);
}
/**
* Get articles__article_data
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getArticlesArticleData()
{
return $this->articles__article_data;
}
/**
* Add articles_article_has_brand
*
* #param \Art\ArticleBundle\Entity\ArticleHasBrand $articlesArticleHasBrand
* #return Article
*/
public function addArticlesArticleHasBrand(ArticleHasBrand $articlesArticleHasBrand)
{
$this->articles_article_has_brand[] = $articlesArticleHasBrand;
return $this;
}
/**
* Remove articles_article_has_brand
*
* #param \Art\ArticleBundle\Entity\ArticleHasBrand $articlesArticleHasBrand
*/
public function removeArticlesArticleHasBrand(ArticleHasBrand $articlesArticleHasBrand)
{
$this->articles_article_has_brand->removeElement($articlesArticleHasBrand);
}
/**
* Get articles_article_has_brand
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getArticlesArticleHasBrand()
{
return $this->articles_article_has_brand;
}
}
Your query should be:
$query = $this->_em->createNativeQuery(
'SELECT ad.id AS article_data_id, ad.title, ad.abstract '
. 'FROM art_article_data ad '
. 'INNER JOIN art_article a ON (ad.article_id = a.id) '
,
$rsm
);
You missed the alias for art_article
I have a database with a table users with fields: user_id, username, password, ... . I also have a table players with player_name, player_position, ... AND a FK user_id.
Image relationship:
I genered the entities from the database but now I would like to get the player from user like this $user->getPlayer(). And doctrine didn't generate player in Users entity, only user in Players Entity.
This is what I have now:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #JoinColumn(name="user_id", referencedColumnName="userId")
*/
private $player;
/**
* Get player
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Players
*/
public function getPlayer() {
return $this->player;
}
/**
* Set player
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Players $player
* #return Users
*/
public function setPlayer(\VolleyScout\VolleyScoutBundle\Entity\Players $player = null){
$this->player = $player;
return $this;
}
But when I try $user->getPlayer() I always get null. (And the user_id is in players table)
UPDATE:
My Players Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Players
*
* #ORM\Table(name="players", indexes={#ORM\Index(name="fk_players_users1_idx", columns={"user_id"}), #ORM\Index(name="fk_players_teams1_idx", columns={"team_id"}), #ORM\Index(name="fk_players_myteam1_idx", columns={"myteam_id"})})
* #ORM\Entity
*/
class Players
{
/**
* #var string
*
* #ORM\Column(name="player_name", type="string", length=255, nullable=false)
*/
private $playerName;
/**
* #var string
*
* #ORM\Column(name="player_licensenumber", type="string", length=45, nullable=false)
*/
private $playerLicensenumber;
/**
* #var string
*
* #ORM\Column(name="player_position", type="string", nullable=false)
*/
private $playerPosition;
/**
* #var integer
*
* #ORM\Column(name="player_birthyear", type="integer", nullable=true)
*/
private $playerBirthyear;
/**
* #var integer
*
* #ORM\Column(name="player_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $playerId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Myteam
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Teams
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
* })
*/
private $team;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set playerName
*
* #param string $playerName
* #return Players
*/
public function setPlayerName($playerName)
{
$this->playerName = $playerName;
return $this;
}
/**
* Get playerName
*
* #return string
*/
public function getPlayerName()
{
return $this->playerName;
}
/**
* Set playerLicensenumber
*
* #param string $playerLicensenumber
* #return Players
*/
public function setPlayerLicensenumber($playerLicensenumber)
{
$this->playerLicensenumber = $playerLicensenumber;
return $this;
}
/**
* Get playerLicensenumber
*
* #return string
*/
public function getPlayerLicensenumber()
{
return $this->playerLicensenumber;
}
/**
* Set playerPosition
*
* #param string $playerPosition
* #return Players
*/
public function setPlayerPosition($playerPosition)
{
$this->playerPosition = $playerPosition;
return $this;
}
/**
* Get playerPosition
*
* #return string
*/
public function getPlayerPosition()
{
return $this->playerPosition;
}
/**
* Set playerBirthyear
*
* #param integer $playerBirthyear
* #return Players
*/
public function setPlayerBirthyear($playerBirthyear)
{
$this->playerBirthyear = $playerBirthyear;
return $this;
}
/**
* Get playerBirthyear
*
* #return integer
*/
public function getPlayerBirthyear()
{
return $this->playerBirthyear;
}
/**
* Get playerId
*
* #return integer
*/
public function getPlayerId()
{
return $this->playerId;
}
/**
* Set myteam
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam
* #return Players
*/
public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Myteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
* #return Players
*/
public function setTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team = null)
{
$this->team = $team;
return $this;
}
/**
* Get team
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Teams
*/
public function getTeam()
{
return $this->team;
}
/**
* Set user
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Users $user
* #return Players
*/
public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
UPDATE 2: In my Users Entity Class I have now:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $player;
But it still doesn't work ..
Your annotations are not set correctly (specifically, the JoinColumn). It is interesting that you aren't getting an error, but anyways... replace this:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #JoinColumn(name="user_id", referencedColumnName="userId")
*/
private $player;
with this:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
private $player;
notice that your referencedColumnName should be the name as it is shown in the database, not the Doxtrine entity. Start by fixing those errors, and it should solve the problem.
UPDATE
This is a very different setup it you want a manyToOne relationship.
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToMany(targetEntity="Players", mappedBy="user_id")
*/
private $players;
public function __construct()
{
$this->players = new ArrayCollection();
}
Because $players can now have many values, it should be set as an ArrayCollection. I also made the variable $players instead of $player, for ease of readability.
You should really familiarize yourself with the Symfony Docs, this is a very simple problem that is described very clear in the Symfony docs
I have table item with item_id, item_title, item_description, item_created, item_approved. I also have a table article with PK item_id (from item table) and article_body.
Now I would like to select all the articles where item.item_approved is NOT equal to NULL. But I'm stuck with creating the query. This is what I have now:
$entityManager = $this->getDoctrine()->getManager();
$repository = $entityManager->getRepository('VolleyScoutBundle:Article');
$query = $repository->createQueryBuilder('a')
->where('a.item.ItemApproved != NULL')
->getQuery();
$articles = $query->getResult();
This gave me the error: [Syntax Error] line 0, col 73: Error: Expected =, <, <=, <>, >, >=, !=, got '.'
This is my Article Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Article
*
* #ORM\Table(name="article")
* #ORM\Entity
*/
class Article
{
/**
* #var string
*
* #ORM\Column(name="article_body", type="text", nullable=false)
*/
private $articleBody;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Item
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Item")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="item_id", referencedColumnName="item_id")
* })
*/
private $item;
/**
* Set articleBody
*
* #param string $articleBody
* #return Article
*/
public function setArticleBody($articleBody)
{
$this->articleBody = $articleBody;
return $this;
}
/**
* Get articleBody
*
* #return string
*/
public function getArticleBody()
{
return $this->articleBody;
}
/**
* Set item
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Item $item
* #return Article
*/
public function setItem(\VolleyScout\VolleyScoutBundle\Entity\Item $item)
{
$this->item = $item;
return $this;
}
/**
* Get item
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Item
*/
public function getItem()
{
return $this->item;
}
}
This is my Item Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Item
*
* #ORM\Table(name="item", indexes={#ORM\Index(name="fk_item_users1_idx", columns={"user_id"}), #ORM\Index(name="fk_item_myteam1_idx", columns={"myteam_id"})})
* #ORM\Entity
*/
class Item
{
/**
* #var string
*
* #ORM\Column(name="item_title", type="string", length=255, nullable=false)
*/
private $itemTitle;
/**
* #var string
*
* #ORM\Column(name="item_description", type="text", nullable=false)
*/
private $itemDescription;
/**
* #var \DateTime
*
* #ORM\Column(name="item_created", type="datetime", nullable=false)
*/
private $itemCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="item_approved", type="datetime", nullable=true)
*/
private $itemApproved;
/**
* #var \DateTime
*
* #ORM\Column(name="item_deleted", type="datetime", nullable=true)
*/
private $itemDeleted;
/**
* #var integer
*
* #ORM\Column(name="item_id", type="bigint")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $itemId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Myteam
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set itemTitle
*
* #param string $itemTitle
* #return Item
*/
public function setItemTitle($itemTitle)
{
$this->itemTitle = $itemTitle;
return $this;
}
/**
* Get itemTitle
*
* #return string
*/
public function getItemTitle()
{
return $this->itemTitle;
}
/**
* Set itemDescription
*
* #param string $itemDescription
* #return Item
*/
public function setItemDescription($itemDescription)
{
$this->itemDescription = $itemDescription;
return $this;
}
/**
* Get itemDescription
*
* #return string
*/
public function getItemDescription()
{
return $this->itemDescription;
}
/**
* Set itemCreated
*
* #param \DateTime $itemCreated
* #return Item
*/
public function setItemCreated($itemCreated)
{
$this->itemCreated = $itemCreated;
return $this;
}
/**
* Get itemCreated
*
* #return \DateTime
*/
public function getItemCreated()
{
return $this->itemCreated;
}
/**
* Set itemApproved
*
* #param \DateTime $itemApproved
* #return Item
*/
public function setItemApproved($itemApproved)
{
$this->itemApproved = $itemApproved;
return $this;
}
/**
* Get itemApproved
*
* #return \DateTime
*/
public function getItemApproved()
{
return $this->itemApproved;
}
/**
* Set itemDeleted
*
* #param \DateTime $itemDeleted
* #return Item
*/
public function setItemDeleted($itemDeleted)
{
$this->itemDeleted = $itemDeleted;
return $this;
}
/**
* Get itemDeleted
*
* #return \DateTime
*/
public function getItemDeleted()
{
return $this->itemDeleted;
}
/**
* Get itemId
*
* #return integer
*/
public function getItemId()
{
return $this->itemId;
}
/**
* Set myteam
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam
* #return Item
*/
public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Myteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set user
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Users $user
* #return Item
*/
public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
try:
$query = $repository->createQueryBuilder('a')
->join('a.item', 'i')
->where('i.ItemApproved is not NULL')
->getQuery();