2013-10-07 93 views
0

我有这样的例子:映射不起作用ORM

Log.php

<?php 
// src/Mailing/MailingBundle/Entity/Log.php 

namespace Mailing\MailingBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="log") 
*/ 

class Log 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 


    /** 
    * @ORM\Column(type="string", length=20) 
    */ 
    protected $action; 

    public function getAction() { 
     return $this->action; 
    } 

    public function setAction($action) { 
     $this->action = $action; 
    } 

    /** 
    * @ORM\Column(type="datetime") 
    */ 
    protected $date; 

    public function getDate() { 
     return $this->date; 
    } 

    public function setDate($date) { 
     $this->date = $date; 
    } 

    /** 
    * @ORM\Column(type="integer", name="mail_id") 
    * @ORM\oneToOne(targetEntity="Mail") 
    */ 
    protected $mail; 

    public function getMail() { 
     return $this->mail; 
    } 

    public function setMail($mail) { 
     $this->mail = $mail; 
    } 

    /** 
    * @ORM\Column(type="integer", name="template_id") 
    * @ORM\oneToOne(targetEntity="Template") 
    */ 
    protected $template; 

    public function getTemplate() { 
     return $this->template; 
    } 

    public function setTemplate($template) { 
     $this->template = $template; 
    } 
} 

Mail.php

<?php 
// src/Mailing/MailingBundle/Entity/Mail.php 

namespace Mailing\MailingBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="mail") 
*/ 

class Mail 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    public function getId() { 
     return $this->id; 
    } 

    /** 
    * @ORM\Column(type="string", length=200) 
    */ 
    protected $address; 

    /** 
    * 
    * @return type 
    */ 
    public function getAddress() { 
     return $this->address; 
    } 

    /** 
    * 
    * @param \Mailing\MailingBundle\Entity\Mail $surname 
    */ 
    public function setAddress($address) 
    { 
     $this->address = $address; 
    } 

    /** 
    * @ORM\Column(type="string", length=200) 
    */ 
    protected $name; 

    /** 
    * 
    * @return type 
    */ 
    public function getName() { 
     return $this->name; 
    } 

    /** 
    * 
    * @param \Mailing\MailingBundle\Entity\Mail $surname 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 
    } 

    /** 
    * @ORM\Column(type="string", length=200) 
    */ 
    protected $surname; 

    /** 
    * 
    * @return type 
    */ 
    public function getSurname() { 
     return $this->surname; 
    } 

    /** 
    * 
    * @param \Mailing\MailingBundle\Entity\Mail $surname 
    */ 
    public function setSurname($surname) 
    { 
     $this->surname = $surname; 
    } 

    /** 
    * @ORM\Column(type="boolean") 
    */ 
    protected $subscribed; 

    /** 
    * 
    * @return type 
    */ 
    public function getSubscribed() { 
     return $this->subscribed; 
    } 

    /** 
    * 
    * @param \Mailing\MailingBundle\Entity\Mail $subscribed 
    */ 
    public function setSubscribed($subscribed) 
    { 
     $this->subscribed = $subscribed; 
    } 


    /** 
    * @ORM\manyToMany(targetEntity="Inventory", inversedBy="articles") 
    * @ORM\joinTable(
    *  name="mail_inventory", 
    *  joinColumns={ 
    *   @ORM\joinColumn(name="mail_id", referencedColumnName="id") 
    *  }, 
    *  inverseJoinColumns={ 
    *   @ORM\joinColumn(name="iventory_id", referencedColumnName="id") 
    *  } 
    *) 
    */ 
    protected $inventories; 

    /** 
    * @return DoctrineCommonCollectionsCollection; 
    */ 
    public function getInventories() 
    { 
     return $this->inventories; 
    } 

    /** 
    * @ORM\Column(type="string", length=128) 
    */ 
    protected $hash; 

    public function getHash() { 
     return $this->hash; 
    } 

    public function setHash($hash) { 
     $this->hash = $hash; 
    } 

} 

LogController.php

<?php 
// src/Mailing/MailingBundle/Controller/LogController.php 

namespace Mailing\MailingBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class LogController extends Controller 
{ 
    public function indexAction() 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $records = $em->getRepository('MailingBundle:Log')->findAll();   
     return $this->render('MailingBundle:Log:index.html.twig', array('logs' =>$records)); 
    } 
} 

模板:

{# src/Mailing/MailingBundle/Resources/views/Log/index.html.twig #} 
{% extends 'MailingBundle::layout.html.twig' %} 

{% block title %} 
    Log 
{% endblock %} 

{% block headline %} 
    Log 
{% endblock %} 

{% block content %} 
    {% if logs %} 
    <table> 
     <tr><th>Date</th><th>E-mail</th><th>Template</th></tr> 
     {% for log in logs %} 
       <tr><td>{{ log.date|date('H:i j.n.Y') }}</td><td>{{ log.action }}</td><td>{{ log.mail.address }}</td><td>{{ log.template.name }}</td></tr> 
     {% endfor %} 
    </table> 
    {% else %} 
     No records to show... 
    {% endif %} 
{% endblock %} 

它生成此错误:

Impossible to access an attribute ("address") on a integer variable ("1") in MailingBundle:Log:index.html.twig at line 17

这是DB模式

log 
id int(11) NO PRI NULL auto_increment 
action varchar(20) YES  NULL  
date datetime YES  NULL  
mail_id int(11) NO MUL NULL  
template_id int(11) YES MUL NULL 

mail 
Field Type Null Key  Default  Extra 
id int(11) NO PRI NULL auto_increment 
address varchar(200) NO  NULL  
name varchar(200) YES  NULL  
surname varchar(200) YES  NULL  
subscribed tinyint(1) YES  1 
hash varchar(128) NO  NULL 

谢谢你的帮助。

回答

2

根据文档:doctrine doc

你错了申报备案:

/** 
* @ORM\Column(type="integer", name="mail_id") 
* @ORM\oneToOne(targetEntity="Mail") 
*/ 
protected $mail; 

而必须是:

/** 
* @OneToOne(targetEntity="Mail") 
* @JoinColumn(name="mail_id", referencedColumnName="id") 
*/ 
protected $mail; 

$template同样的事情。

编辑:

如果你不能让它工作,你可以尝试的双向关联:onetoone bidirectional

Log.php

/** 
* @OneToOne(targetEntity="Mail", inversedBy="log") 
* @JoinColumn(name="mail_id", referencedColumnName="id") 
*/ 
protected $mail; 

Mail.php

/** 
* @OneToOne(targetEntity="Log", mappedBy="mail") 
**/ 
private $log; 
+0

这是默认生成的注解。我试图添加它,它是一样的。谢谢。 – Figa

+0

您已更新数据库并删除了缓存? 这是文档的声明,它适用于我。 如果您无法使其工作,您可以尝试双向绑定,请参阅我的更新。 – Lughino

+0

我真的不知道为什么它不工作:(我清除缓存,但没有成功。我不想有双向。它没有任何意义。 – Figa