I'm trying to workout a basic join in a query for MySQL-tables. After reading the documentation and some questions on Stackoverflow, I still can't get it to work ...
The error:
Message: [Semantical Error] line 0, col 74 near 'o': Error: Class Client_Model_Client has no association named Contact_Model_Contact
The code here, are my models and the query I'm working on:
Model client
<?php
/**
* #Table(name="client_clients")
* #Entity(repositoryClass="Client_Model_ClientRepository")
*/
class Client_Model_Client {
public function __construct()
{
$this->contacts = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #var integer $id
* #Id #Column(type="integer")
* #GeneratedValue
*/
private $id;
/**
* #var string $name
* #Column(type="string")
*/
private $name;
/**
* #var string $address
* #Column(type="string")
*/
private $address;
/**
* #var string $comment
* #Column(type="string")
*/
private $comment;
/**
* #var integer $account_id
* #Column(type="integer")
*/
private $account_id;
/**
* #var integer $contact_id
* #Column(type="integer")
*/
private $contact_id;
/**
* #var string $created_at
* #Column(type="string")
*/
private $created_at;
/**
* #var contact_data $contact_data
* #Column(name="contact_data", type="integer", nullable=false)
* #OneToMany(targetEntity="Contact_Model_Contact", mappedBy="contact_data")
* #JoinColumn(name="contact_id", referencedColumnName="id")
*/
private $contact_data;
... (getters and setters)
Model Contact
<?php
/**
* #Table(name="contact_contacts")
* #Entity(repositoryClass="Contact_Model_ContactRepository")
*/
class Contact_Model_Contact {
/**
* #var integer $id
* #Id #Column(type="integer")
* #GeneratedValue
* #ManyToOne(targetEntity="Client_Model_Client", mappedBy="contact_data")
*/
private $id;
/**
* #var string $name
* #Column(type="string")
*/
private $name;
/**
* #var string $email
* #Column(type="string")
*/
private $email;
/**
* #var string $phone
* #Column(type="string")
*/
private $phone;
/**
* #var integer $address
* #Column(type="string")
*/
private $address;
/**
* #var string $comment
* #Column(type="string")
*/
private $comment;
/**
* #var string $created_at
* #Column(type="string")
*/
private $created_at;
/**
* #var integer $contact_id
* #Column(type="string")
*/
private $contact_id;
The query
$qb = $this->_em->createQueryBuilder()
->select('i, o')
->from('Client_Model_Client', 'i')
->join('i.Contact_Model_Contact', 'o');
$query = $qb->getQuery();
$roles = $query->getResult();
Zend_Debug::dump($roles); die;
What am I doing wrong?
How should it work?
You should be doing this:
$qb = $this->_em->createQueryBuilder()
->select('i, o')
->from('Client_Model_Client', 'i')
->join('i.contact_data', 'o');
Related
I have a problem. I moved website to another hosting(so I exported DB and imported to new hosting).
But now, when I trying to add new record to database. I have an error stn like ID 1 already exist. But I have almost 500 records in the table.
What can I do to fix it?
Sorry I've should provide more information:
I exported and imported database through phppgadmin I didn't use migrations bundle.
Here is my entity:
class Products
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="serial_number", type="string", length=255, nullable=true)
*/
private $serial;
/**
* #var string
* #Expose
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
* #Expose
* #ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* #var string
* #ORM\Column(name="note", type="text", nullable=true)
*/
private $note;
/**
* #var int
*
* #ORM\Column(name="views", type="bigint", nullable=true)
*/
private $views;
/**
* #ORM\ManyToMany(targetEntity="Models")
* #ORM\JoinTable(name="products_models",
* joinColumns={#ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="model_id", referencedColumnName="id")}
* )
*/
private $models;
/**
* #ORM\OneToOne(targetEntity="ProductDetails", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="details_id", referencedColumnName="id")
*/
private $details;
/**
* #var File
* #Expose
* #ORM\OneToMany(targetEntity="ProductImages", mappedBy="product", cascade={"persist", "remove"})
* #ORM\OrderBy({"id" = "ASC"})
*
*/
private $images;
/**
* #var File
*
* #ORM\OneToMany(targetEntity="Cart", mappedBy="productId", cascade={"persist"})
*
*/
private $cart;
/**
* #var string
*
* #ORM\Column(name="price", type="integer", length=255, nullable=true)
*/
private $price;
/**
* #var string
*
* #ORM\Column(name="bought_price", type="integer", length=255, nullable=true)
*/
private $boughtPrice;
/**
* #var string
*
* #ORM\Column(name="old_price", type="integer", length=255, nullable=true)
*/
private $oldPrice;
/**
* #var bool
*
* #ORM\Column(name="is_active", type="boolean", nullable=true)
*/
private $isActive;
/**
* #var bool
*
* #ORM\Column(name="is_accessory", type="boolean", nullable=true)
*/
private $isAccessory;
/**
* #ORM\ManyToOne(targetEntity="AccessoryCategory")
* #ORM\JoinColumn(name="accessory_category_id", referencedColumnName="id")
*/
private $accessoryCategory;
/**
* #var bool
*
* #ORM\Column(name="is_special", type="boolean", nullable=true)
*/
private $isSpecial;
/**
* #var integer
*
* #ORM\Column(name="quantity", type="integer", nullable=true)
*/
private $quantity;
/**
* created Time/Date
*
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
protected $createdAt;
/**
* updated Time/Date
*
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
protected $updatedAt;
/**
* #var boolean
*
* #ORM\Column(name="seller", type="boolean", length=255, nullable=true)
*/
private $seller;
/**
* Set createdAt
*
* #ORM\PrePersist
*/
public function setCreatedAt()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #ORM\PreUpdate
*/
public function setUpdatedAt()
{
$this->updatedAt = new \DateTime();
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Products
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set url
*
* #param string $url
*
* #return Products
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set note
*
* #param string $note
*
* #return Products
*/
public function setNote($note)
{
$this->note = $note;
return $this;
}
/**
* Get note
*
* #return string
*/
public function getNote()
{
return $this->note;
}
/**
* Set views
*
* #param integer $views
*
* #return Products
*/
public function setViews($views)
{
$this->views = $views;
return $this;
}
/**
* Get views
*
* #return integer
*/
public function getViews()
{
return $this->views;
}
/**
* Set price
*
* #param integer $price
*
* #return Products
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return integer
*/
public function getPrice()
{
return $this->price;
}
/**
* Set boughtPrice
*
* #param integer $boughtPrice
*
* #return Products
*/
public function setBoughtPrice($boughtPrice)
{
$this->boughtPrice = $boughtPrice;
return $this;
}
/**
* Get boughtPrice
*
* #return integer
*/
public function getBoughtPrice()
{
return $this->boughtPrice;
}
/**
* Set isActive
*
* #param boolean $isActive
*
* #return Products
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* #return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set isAccessory
*
* #param boolean $isAccessory
*
* #return Products
*/
public function setIsAccessory($isAccessory)
{
$this->isAccessory = $isAccessory;
return $this;
}
/**
* Get isAccessory
*
* #return boolean
*/
public function getIsAccessory()
{
return $this->isAccessory;
}
/**
* Set quantity
*
* #param integer $quantity
*
* #return Products
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* #return integer
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set details
*
* #param \Web\AdminBundle\Entity\ProductDetails $details
*
* #return Products
*/
public function setDetails(\Web\AdminBundle\Entity\ProductDetails $details = null)
{
$this->details = $details;
return $this;
}
/**
* Get details
*
* #return \Web\AdminBundle\Entity\ProductDetails
*/
public function getDetails()
{
return $this->details;
}
/**
* Add image
*
* #param \Web\AdminBundle\Entity\ProductImages $image
*
* #return Products
*/
public function addImage(\Web\AdminBundle\Entity\ProductImages $image)
{
$this->images[] = $image;
return $this;
}
/**
* Remove image
*
* #param \Web\AdminBundle\Entity\ProductImages $image
*/
public function removeImage(\Web\AdminBundle\Entity\ProductImages $image)
{
$this->images->removeElement($image);
}
/**
* Get images
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
/**
* Add cart
*
* #param \Web\AdminBundle\Entity\Cart $cart
*
* #return Products
*/
public function addCart(\Web\AdminBundle\Entity\Cart $cart)
{
$this->cart[] = $cart;
return $this;
}
/**
* Remove cart
*
* #param \Web\AdminBundle\Entity\Cart $cart
*/
public function removeCart(\Web\AdminBundle\Entity\Cart $cart)
{
$this->cart->removeElement($cart);
}
/**
* Get cart
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCart()
{
return $this->cart;
}
/**
* Set isSpecial
*
* #param boolean $isSpecial
*
* #return Products
*/
public function setIsSpecial($isSpecial)
{
$this->isSpecial = $isSpecial;
return $this;
}
/**
* Get isSpecial
*
* #return boolean
*/
public function getIsSpecial()
{
return $this->isSpecial;
}
/**
* Set accessoryCategory
*
* #param \Web\AdminBundle\Entity\AccessoryCategory $accessoryCategory
*
* #return Products
*/
public function setAccessoryCategory(\Web\AdminBundle\Entity\AccessoryCategory $accessoryCategory = null)
{
$this->accessoryCategory = $accessoryCategory;
return $this;
}
/**
* Get accessoryCategory
*
* #return \Web\AdminBundle\Entity\AccessoryCategory
*/
public function getAccessoryCategory()
{
return $this->accessoryCategory;
}
/**
* Set oldPrice
*
* #param integer $oldPrice
*
* #return Products
*/
public function setOldPrice($oldPrice)
{
$this->oldPrice = $oldPrice;
return $this;
}
/**
* Get oldPrice
*
* #return integer
*/
public function getOldPrice()
{
return $this->oldPrice;
}
/**
* Set serial
*
* #param string $serial
*
* #return Products
*/
public function setSerial($serial)
{
$this->serial = $serial;
return $this;
}
/**
* Get serial
*
* #return string
*/
public function getSerial()
{
return $this->serial;
}
/**
* Constructor
*/
public function __construct()
{
$this->models = new \Doctrine\Common\Collections\ArrayCollection();
$this->images = new \Doctrine\Common\Collections\ArrayCollection();
$this->cart = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add model
*
* #param \Web\AdminBundle\Entity\Models $model
*
* #return Products
*/
public function addModel(\Web\AdminBundle\Entity\Models $model)
{
$this->models[] = $model;
return $this;
}
/**
* Remove model
*
* #param \Web\AdminBundle\Entity\Models $model
*/
public function removeModel(\Web\AdminBundle\Entity\Models $model)
{
$this->models->removeElement($model);
}
/**
* Get models
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getModels()
{
return $this->models;
}
/**
* Set seller
*
* #param boolean $seller
*
* #return Products
*/
public function setSeller($seller)
{
$this->seller = $seller;
return $this;
}
/**
* Get seller
*
* #return boolean
*/
public function getSeller()
{
return $this->seller;
}
I'm adding new like this:
$em = $this->getDoctrine()->getManager();
$products = new Products();
$article->setTitle('here is the title');
....
$em->persist($products);
$em->flush();
this should set id to like 488 but it's trying to set it to 1. And I don't know why? Might be some cache? But php bin/console cache:clear doesn't change anything.
Doctrine use sequence with the auto strategy and Postgres.
Maybe sequence values has been lost when export/import db.
Identify the sequence used by your ID and try to execute :
ALTER SEQUENCE sequence_name RESTART WITH your_next_free_id;
hi i have got the same problem when i Import my database the probléme s like they say sequence.
But because i have more than 100 table i can't reset the sequence one by one So i Found this query it create for all your table an sql query to update the sequence by the max Id, you have just to copy the result and execute it
SELECT 'SELECT SETVAL(' ||
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
FROM pg_class AS S,
pg_depend AS D,
pg_class AS T,
pg_attribute AS C,
pg_tables AS PGT
WHERE S.relkind = 'S'
AND S.oid = D.objid
AND D.refobjid = T.oid
AND D.refobjid = C.attrelid
AND D.refobjsubid = C.attnum
AND T.relname = PGT.tablename
ORDER BY S.relname;
I think as other have suggested its not the issue with symfony or doctrine , instead its postgresql common issue, you can check a solution here
Make sure you are using same version, I am not sure if this behavior differs from version to version.
You can change the current value of the auto increment manually like this :
ALTER TABLE products AUTO_INCREMENT=501;
I know this question has already been asked more than once, but I still can't figure what goes wrong.
I'm currently trying to learn Symfony2 (using version 2.7.4), and I'm getting the following error when I'm trying to reach a page that's supposed to have an image in it :
The target-entity Entity\Image cannot be found in 'OC\PlatformBundle\Entity\Advert#Image'.
Here is my code:
**advert.php**
<?php
namespace OC\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="OC\PlatformBundle\Entity\AdvertRepository")
*/
class Advert
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="published", type="boolean")
*/
private $published = true;
/**
* #ORM\OneToOne(targetEntity="Entity\Image", cascade={"persist"})
*/
private $image;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* Set image
*
* #param \OC\PlatformBundle\Entity\Image $image
*
* #return Advert
*/
public function setImage(\OC\PlatformBundle\Entity\Image $image = null)
{
$this->image = $image;
return $this;
}
/**
* Get Image
*
* #return \OC\PlatformBundle\Entity\Image
*/
public function getImage()
{
return $this->Image;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function __construct()
{
// Par défaut, la date de l'annonce est la date d'aujourd'hui
$this->date = new \Datetime();
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Advert
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set title
*
* #param string $title
*
* #return Advert
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set author
*
* #param string $author
*
* #return Advert
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set content
*
* #param string $content
*
* #return Advert
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set published
*
* #param boolean $published
*
* #return Advert
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* #return boolean
*/
public function getPublished()
{
return $this->published;
}
}
file Image :
<?php
// src/OC/PlatformBundle/Entity/Image
namespace OC\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Image
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* #ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
}
I understood that this problem mainly comes from a typo in the call of the entity, but I triple-checked everything, and it seems alright to me... Is there something I didn't see ?
Thank you in advance
You should fix your annotation:
/**
* #ORM\OneToOne(targetEntity="OC\PlatformBundle\Entity\Image", cascade={"persist"})
*/
private $image;
I have 3 tables Chat(id + info) User(id + some info) and relation table chat_invited(chatId+userId)
I am using ZF2 and Doctrine2
I have Entity chat:
<?php
namespace Chat\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Chat
*
* #ORM\Table(name="chat")
* #ORM\Entity
*/
class Chat
{
/**
* #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=256, nullable=true)
*/
private $title;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime", nullable=false)
*/
private $created;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Chat\Entity\User", inversedBy="chat")
* #ORM\JoinTable(name="chat_invited",
* joinColumns={
* #ORM\JoinColumn(name="chat", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="user", referencedColumnName="id")
* }
* )
*/
private $user;
/**
* #var \Chat\Entity\ChatCategory
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\ChatCategory")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category", referencedColumnName="id")
* })
*/
private $category;
/**
* #var \Chat\Entity\User
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="owner", referencedColumnName="id")
* })
*/
private $owner;
/**
* #var \Chat\Entity\Building
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\Building")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="building", referencedColumnName="id")
* })
*/
private $building;
/**
* #var \Chat\Entity\Company
*
* #ORM\ManyToOne(targetEntity="Chat\Entity\Company")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="company", referencedColumnName="id")
* })
*/
private $company;
/**
* Constructor
*/
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add user
*
* #param \Chat\Entity\User $user
* #return Chat
*/
public function addUser(\Chat\Entity\User $user)
{
$this->user[] = $user;
return $this;
}
/**
* Remove user
*
* #param \Chat\Entity\User $user
*/
public function removeUser(\Chat\Entity\User $user)
{
$this->user->removeElement($user);
}
/**
* Get user
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
........................................
}
How find chat which have invited User1 and User2 to him?
Its worked for me
$qb = $em->createQueryBuilder();
$query = $qb->select('chats, user')
->from('Chat\Entity\Chat', 'chats')
->join('chats.user', 'user')
->join('chats.user', 'user2')
->where('user.id = :user AND user2.id = :inv ')
->setParameter('user', <User1.id>)
->setParameter('inv',<User2.id>)
->setMaxResults(1)
->getQuery();
Here are my entities :
/**
* Message_User
*
* #ORM\Table(name="message_user")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
*/
class Message_User
{
/**
* #ORM\ManyToOne(targetEntity="Learnpack\UserBundle\Entity\User", inversedBy="mu")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* #ORM\Id
*/
protected $user;
/**
* #ORM\ManyToOne(targetEntity="Learnpack\MessageBundle\Entity\Message", inversedBy="mu")
* #ORM\JoinColumn(name="message_id", referencedColumnName="id")
* #ORM\Id
*/
protected $message;
/**
* #var string $status
*
* #ORM\Column(name="status", type="string", length=45)
*/
private $status;
}
/**
* Message
*
* #ORM\Table(name="message")
* #ORM\Entity
*/
class Message
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="subject", type="string", length=100)
*/
private $subject;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="status", type="string", length=45)
*/
private $status;
/**
* #var string
*
* #ORM\Column(name="file", type="string", length=100)
*/
private $file;
/**
* #var \DateTime
*
* #ORM\Column(name="send", type="datetime")
*/
private $send;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* #var \DateTime
*
* #ORM\Column(name="modified", type="datetime")
*/
private $modified;
/**
* #ORM\ManyToOne(targetEntity="Learnpack\UserBundle\Entity\User", inversedBy="user")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $sender;
Now I would like to make a form with basic message sending. There will be three fields:
Recipient, subject and content.
My problem is that I do not know how to add a field "input" with ajax for the recipient.
I'm trying to do a join between 2 tables, but I get this error:
Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
This is the code:
Page model
public function __construct()
{
$this->pages_meta = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #var integer $id
* #Id #Column(type="integer")
* #GeneratedValue
*/
private $id;
/**
* #var integer $layout
* #Column(type="string")
*/
private $layout;
/**
* #var string $name
* #Column(type="string")
*/
private $name;
/**
* #var string $title
* #Column(type="string")
*/
private $title;
/**
* #var string $slug
* #Column(type="string")
*/
private $slug;
/**
* #var string $options
* #Column(type="integer")
*/
private $content_id;
/**
* #var integer $user_id
* #Column(type="integer")
*/
private $user_id;
/**
* #var string $created_at
* #Column(type="datetime")
*/
private $created_at;
/**
* #var string $language
* #Column(type="string")
*/
private $language;
/**
* #OneToMany(targetEntity="Default_Model_PageMeta", mappedBy="page_id")
* #JoinColumn(name="id", referencedColumnName="page_id")
*/
private $meta;
... (getters and setters)
PageMeta Model
/**
* #var integer $id
* #Id #Column(type="integer")
* #GeneratedValue
*/
private $id;
/**
* #var integer $page_id
* #Column(type="integer")
*/
private $page_id;
/**
* #var integer $key
* #Column(type="string")
*/
private $key;
/**
* #var integer $value
* #Column(type="string")
*/
private $value;
... (getters and setters)
The join Syntax
$doctrine = Zend_Registry::get('doctrine');
$request = Zend_Controller_Front::getInstance()->getRequest();
$qb = $doctrine->_em->createQueryBuilder()
->select('p, m')
->from('Default_Model_Page', 'p')
->join('p.meta', 'm');
$query = $qb->getQuery();
$page = $query->getResult();
Zend_Debug::dump($page); die;
Any idea what I'm doing wrong?
Thanks in advance!
It doesn't look like you have told Doctrine how to associate a Page model with a PageMeta model. I can guess that the foreign key in the table for PageMeta is page_id, but that doesn't appear anywhere in your annotations.
In the PageMeta model, try:
/**
* The page
*
* #var Default_Model_Page
* #OneToOne(targetEntity="Default_Model_Page")
* #JoinColumn(name="page_id", referencedColumnName="id")
*/
private $page;
Then in your query, you should be able to do:
$qb = $doctrine->_em->createQueryBuilder()
->select('p, m')
->from('Default_Model_PageMeta', 'm')
->join('m.page', 'p');
Not tested, but something like this should work.
Don't map the db columns you need for a relation as both #Column and #ManyToOne (or OneToOne), but always choose one. For most relations you'll want the association mappings (#ManyToOne, etc) and need to drop the #Column.
Doctrine will behave very erratic if you use both.
/**
* #Entity
*/
class Page
{
/**
* #OneToMany(targetEntity="PageMeta", mappedBy="page")
*/
private $meta;
public function __construct() {
$this->meta = new \Doctrine\Common\Collections\ArrayCollection();
}
}
/**
* #Entity
*/
class PageMeta
{
// Remove the property $page_id
/**
* #ManyToOne(targetEntity="Page", inversedBy="meta")
* #JoinColumn(name="page_id", referencedColumnName="id")
*/
private $page;
}
You can find more info on assiciations here.