在坚持和同花顺之后,学说关系消失了

时间:2012-02-05 22:42:25

标签: php mysql zend-framework doctrine

我在记录笔记的实体和用户发表评论的实体之间有多对一的关系。即一个用户可以发表很多评论。

第一次查看页面时,评论显示正常,$ note-> employee-> user根据与notes表的多对一关系从employee表中获取用户名。通过发布表单添加新笔记时:

$activity = new \S2\Entity\Crmnotes;
$activity->solicitorid = $solicitorid;
$activity->note = $activityDescription;
$activity->employeeid = 2;
$activity->type = $activityType;
$this->_em->persist($activity);
$this->_em->flush();

然后页面将显示来自Notes实体的数据,但不显示来自其任何关系的数据,即$ notes-> note,$ notes-> type display,但$ notes-> employee-> user会导致a试图获取非对象错误的属性。

下次访问该页面时,所有数据都会照常显示。

修改

作为附加点,在插入记录后,datetime对象$ note-> lastmodified也无法显示。当我尝试格式化日期时,我在非对象上收到调用成员函数format()的错误,例如$ note-> lastmodified->格式('d / m / Y H:i');

修改 是的,这是学说2。 根据要求:

namespace S2\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Crmnotes
 *
 * @Table(name="crmnotes")
 * @Entity
 */
class Crmnotes extends \S2_Doctrine_Entity
{
/**
 *
 * @ORM\ManyToOne(targetEntity="S2\Entity\Tblsolicitor", inversedBy="crmnotes")
 * @JoinColumn(name="SolicitorID", referencedColumnName="SolicitorID")
 * 
 */
 private $solicitor;

/**
 *
 * @ORM\ManyToOne(cascade={"persist"}, targetEntity="S2\Entity\Tblemployees", inversedBy="crmnotes")
 * @JoinColumn(name="EmployeeID", referencedColumnName="EmployeeID")
 * 
 */
 private $employee;

/**
 * @var integer $notesid
 *
 * @Column(name="notesid", type="integer", nullable=false)
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 */
private $notesid;

/**
 * @var integer $type
 *
 * @Column(name="type", type="integer", nullable=false)
 */
private $type;

/**
 * @var integer $solicitorid
 *
 * @Column(name="SolicitorID", type="integer", nullable=false)
 */
private $solicitorid;

/**
 * @var text $note
 *
 * @Column(name="Note", type="text", nullable=false)
 */
private $note;

/**
 * @var integer $employeeid
 *
 * @Column(name="EmployeeID", type="integer", nullable=false)
 */
private $employeeid;

/**
 * @var datetime $lastmodified
 *
 * @Column(name="LastModified", type="datetime", nullable=false)
 */
private $lastmodified;

public function __get($name) 
{
    if (isset($this->$name)){
        return $this->$name;
    } else {
        return '';
    }
}

public function __set($name, $value) 
{
    $this->$name = $value;
}


}

修改

使用对象而不是手动插入外键时仍然没有乐趣:

$activity = new \S2\Entity\Crmnotes;
$activity->note = $activityDescription;
$activity->type = $activityType;
$solicitor = $this->_em->getRepository('\S2\Entity\Tblsolicitor')->find($solicitorid);
$solicitor->crmnotes->add($activity);
$activity->solicitor = $solicitor;
$employee = $this->_em->getRepository('\S2\Entity\Tblemployees')->find(2);
$this->view->debug = $solicitor->crmnotes[0]->note;
$employee->crmnotes->add($activity);
$activity->employee = $employee;
$this->_em->persist($activity);
$this->_em->flush();

1 个答案:

答案 0 :(得分:0)

尝试删除

/**
 * @var integer $employeeid
 *
 * @Column(name="EmployeeID", type="integer", nullable=false)
 */
private $employeeid;

当更新的学说在使用同一列时也存在关系时会抱怨它。

* @JoinColumn(name="EmployeeID", referencedColumnName="EmployeeID")

可能是引用ID而不是EmployeeID?

  if (isset($this->$name)){
        return $this->$name;
    } else {
        return '';
    }

无需检查,如果空

,则Doctrine返回null

我没有看到你设置LastModified的任何地方?

$entity->lastmodified = new \DateTime();