开发者

Join tables with Doctrine2

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开发者_JS百科 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜